From 323de704f6ebcd576fa641aad46f4bc0b270c1d8 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Tue, 16 Jul 2019 22:03:26 +0200 Subject: [PATCH] improve command xmpp-check --- cmd/xmpp-check/README.md | 27 ++++++++++++++++++---- cmd/xmpp-check/log.go | 34 ++++++++++++++++++++++++++++ cmd/xmpp-check/main.go | 44 ++++++++++++++++++++++++++++++++++++ cmd/xmpp-check/xmpp-check.go | 42 ---------------------------------- 4 files changed, 101 insertions(+), 46 deletions(-) create mode 100644 cmd/xmpp-check/log.go create mode 100644 cmd/xmpp-check/main.go delete mode 100644 cmd/xmpp-check/xmpp-check.go diff --git a/cmd/xmpp-check/README.md b/cmd/xmpp-check/README.md index dc177aa..d943248 100644 --- a/cmd/xmpp-check/README.md +++ b/cmd/xmpp-check/README.md @@ -12,18 +12,37 @@ $ go get -u gosrc.io/xmpp/cmd/xmpp-check ## Usage +``` +$ xmpp-check --help +Usage: + xmpp-check [flags] + +Examples: +xmpp-check chat.sum7.eu:5222 --domain meckerspace.de + +Flags: + -d, --domain string domain if host handle multiple domains + -h, --help help for xmpp-check +``` + If you server is on standard port and XMPP domains matches the hostname you can simply use: ``` -$ xmpp-check myhost.net -2019/05/16 16:04:36 All checks passed +$ xmpp-check chat.sum7.eu + info All checks passed + ⇢ address="chat.sum7.eu" domain="" + ⇢ main.go:43 main.runCheck + ⇢ 2019-07-16T22:01:39.765+02:00 ``` You can also pass the port and the XMPP domain if different from the server hostname: ``` -$ xmpp-check myhost.net:5222 xmppdomain.net -2019/05/16 16:05:21 All checks passed +$ xmpp-check chat.sum7.eu:5222 --domain meckerspace.de + info All checks passed + ⇢ address="chat.sum7.eu:5222" domain="meckerspace.de" + ⇢ main.go:43 main.runCheck + ⇢ 2019-07-16T22:01:33.270+02:00 ``` Error code will be non-zero in case of error. You can thus use it directly with your usual diff --git a/cmd/xmpp-check/log.go b/cmd/xmpp-check/log.go new file mode 100644 index 0000000..eb67593 --- /dev/null +++ b/cmd/xmpp-check/log.go @@ -0,0 +1,34 @@ +package main + +import ( + "os" + + "github.com/bdlm/log" + stdLogger "github.com/bdlm/std/logger" +) + +type hook struct{} + +func (h *hook) Fire(entry *log.Entry) error { + switch entry.Level { + case log.PanicLevel: + entry.Logger.Out = os.Stderr + case log.FatalLevel: + entry.Logger.Out = os.Stderr + case log.ErrorLevel: + entry.Logger.Out = os.Stderr + case log.WarnLevel: + entry.Logger.Out = os.Stdout + case log.InfoLevel: + entry.Logger.Out = os.Stdout + case log.DebugLevel: + entry.Logger.Out = os.Stdout + default: + } + + return nil +} + +func (h *hook) Levels() []stdLogger.Level { + return log.AllLevels +} diff --git a/cmd/xmpp-check/main.go b/cmd/xmpp-check/main.go new file mode 100644 index 0000000..88faa9b --- /dev/null +++ b/cmd/xmpp-check/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "github.com/bdlm/log" + "github.com/spf13/cobra" + "gosrc.io/xmpp" +) + +func main() { + log.AddHook(&hook{}) + cmd.Execute() +} + +var domain = "" +var cmd = &cobra.Command{ + Use: "xmpp-check ", + Example: "xmpp-check chat.sum7.eu:5222 --domain meckerspace.de", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + runCheck(args[0], domain) + }, +} + +func init() { + cmd.Flags().StringVarP(&domain, "domain", "d", "", "domain if host handle multiple domains") +} + +func runCheck(address, domain string) { + logger := log.WithFields(map[string]interface{}{ + "address": address, + "domain": domain, + }) + client, err := xmpp.NewChecker(address, domain) + + if err != nil { + log.Fatal("Error: ", err) + } + + if err = client.Check(); err != nil { + logger.Fatal("Failed connection check: ", err) + } + + logger.Println("All checks passed") +} diff --git a/cmd/xmpp-check/xmpp-check.go b/cmd/xmpp-check/xmpp-check.go deleted file mode 100644 index 77af605..0000000 --- a/cmd/xmpp-check/xmpp-check.go +++ /dev/null @@ -1,42 +0,0 @@ -package main - -import ( - "log" - "os" - - "gosrc.io/xmpp" -) - -func main() { - args := os.Args[1:] - - if len(args) == 0 { - log.Fatal("usage: xmpp-check host[:port] [domain]") - } - - var address string - var domain string - if len(args) >= 1 { - address = args[0] - } - - if len(args) >= 2 { - domain = args[1] - } - - runCheck(address, domain) -} - -func runCheck(address, domain string) { - client, err := xmpp.NewChecker(address, domain) - - if err != nil { - log.Fatal("Error: ", err) - } - - if err = client.Check(); err != nil { - log.Fatal("Failed connection check: ", err) - } - - log.Println("All checks passed") -}