From f831c02e121b14f89971dcdfc8f4c616075a7e64 Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Fri, 25 Feb 2022 12:18:55 +0100 Subject: [PATCH] Extract client host option setting --- main.go | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/main.go b/main.go index c353c05..afbd452 100644 --- a/main.go +++ b/main.go @@ -52,23 +52,7 @@ func main() { return } - var opts []client.Opt - if *h != "" { - if !strings.Contains(*h, "//") { - *h = "tcp://" + *h - } - if u, err := client.ParseHostURL(*h); err == nil { - if u.Port() != "" { - opts = append(opts, client.WithHost(*h)) - } else { - opts = append(opts, client.WithHost(*h+":2375")) - } - } else { - log.Fatal(err) - } - } - - dc, err := client.NewClientWithOpts(opts...) + dc, err := client.NewClientWithOpts(getClientOpts(h)...) if err != nil { log.Fatalln(err) return @@ -157,3 +141,23 @@ func contains(uids []string, uid string) bool { } return false } + +// Get client opts from host param +func getClientOpts(h *string) []client.Opt { + var opts []client.Opt + if *h != "" { + if !strings.Contains(*h, "//") { + *h = "tcp://" + *h + } + if u, err := client.ParseHostURL(*h); err == nil { + if u.Port() != "" { + opts = append(opts, client.WithHost(*h)) + } else { + opts = append(opts, client.WithHost(*h+":2375")) + } + } else { + log.Fatal(err) + } + } + return opts +}