From af281da430cba399be2d4ab8c9128f6ca44da7b2 Mon Sep 17 00:00:00 2001 From: Drasko DRASKOVIC Date: Fri, 17 Aug 2018 04:58:09 +0200 Subject: [PATCH] NOISSUE - Add `insecure` param to cli (#356) Signed-off-by: drasko --- cli/http.go | 4 ++-- cmd/cli/main.go | 27 ++++++++++++++++++--------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/cli/http.go b/cli/http.go index ad899d71..cf3eda8c 100644 --- a/cli/http.go +++ b/cli/http.go @@ -30,8 +30,8 @@ var ( ) // SetServerAddr - set addr using host and port -func SetServerAddr(host string, port int) { - serverAddr = fmt.Sprintf("https://%s", host) +func SetServerAddr(proto string, host string, port int) { + serverAddr = fmt.Sprintf("%s://%s", proto, host) if port != 0 { serverAddr = fmt.Sprintf("%s:%s", serverAddr, strconv.Itoa(port)) diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 3b8b5bae..e53c27c0 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -10,19 +10,29 @@ import ( func main() { conf := struct { - Host string - Port int + host string + port int + insecure bool }{ "localhost", 0, + false, } // Root var rootCmd = &cobra.Command{ Use: "mainflux-cli", PersistentPreRun: func(cmd *cobra.Command, args []string) { - // Set HTTP server address - cli.SetServerAddr(conf.Host, conf.Port) + var proto string + + if conf.insecure { + proto = "http" + } else { + proto = "https" + cli.SetCerts() + } + + cli.SetServerAddr(proto, conf.host, conf.port) }, } @@ -42,9 +52,11 @@ func main() { // Root Flags rootCmd.PersistentFlags().StringVarP( - &conf.Host, "host", "m", conf.Host, "HTTP Host address") + &conf.host, "host", "m", conf.host, "HTTP Host address") rootCmd.PersistentFlags().IntVarP( - &conf.Port, "port", "p", conf.Port, "HTTP Host Port") + &conf.port, "port", "p", conf.port, "HTTP Host Port") + rootCmd.PersistentFlags().BoolVarP( + &conf.insecure, "insecure", "i", false, "do not use TLS") // Client and Channels Flags rootCmd.PersistentFlags().IntVarP( @@ -52,9 +64,6 @@ func main() { rootCmd.PersistentFlags().IntVarP( &cli.Offset, "offset", "o", 0, "offset query parameter") - // Set TLS certificates - cli.SetCerts() - if err := rootCmd.Execute(); err != nil { log.Fatal(err) }