55 "net/url"
66 "os"
77 "path/filepath"
8+ "strings"
89
910 "github.com/humio/cli/api"
1011 "github.com/humio/cli/prompt"
@@ -26,16 +27,23 @@ In the config file already exists, the settings will be merged into the existing
2627 var addr , token string
2728 var err error
2829
29- fmt .Println ("This will guide you through setting up the Humio CLI." )
30- fmt .Println ("" )
30+ prompt .Output ("" )
31+ owl := "[purple]" + prompt .Owl () + "[reset]"
32+ fmt .Print ((prompt .Colorize (owl )))
33+ prompt .Output ("" )
34+ prompt .Title ("Welcome to Humio" )
35+ prompt .Output ("" )
36+ prompt .Description ("This will guide you through setting up the Humio CLI." )
37+ prompt .Output ("" )
38+
39+ prompt .Info ("Which Humio instance should we talk to?" ) // INFO
40+ prompt .Output ("" )
41+ prompt .Description ("If you are not using Humio Cloud enter the address of your Humio installation," )
42+ prompt .Description ("e.g. http://localhost:8080/ or https://humio.example.com/" )
3143
3244 for true {
33- fmt .Println ("1. Which Humio instance should we talk to?" ) // INFO
34- fmt .Println ("" )
35- fmt .Println ("If you are not using Humio Cloud enter the address out your Humio installation," )
36- fmt .Println ("e.g. http://localhost:8080/ or https://humio.example.com/" )
37- fmt .Println ("" )
38- fmt .Println ("Default: https://cloud.humio.com/ [Hit Enter]" )
45+ prompt .Output ("" )
46+ prompt .Output ("Default: https://cloud.humio.com/ [Hit Enter]" )
3947 addr , err = prompt .Ask ("Humio Address" )
4048
4149 if addr == "" {
@@ -48,13 +56,16 @@ In the config file already exists, the settings will be merged into the existing
4856
4957 // Make sure it is a valid URL and that
5058 // we always end in a slash.
51- addrUrl , urlErr := url .Parse (addr )
59+ _ , urlErr := url .ParseRequestURI (addr )
5260
5361 if urlErr != nil {
54- fmt .Println ("The valus must be a valid URL." ) // ERROR
62+ prompt .Error ("The valus must be a valid URL." )
63+ continue
5564 }
5665
57- addr = fmt .Sprintf ("%v" , addrUrl )
66+ if ! strings .HasSuffix (addr , "/" ) {
67+ addr = addr + "/"
68+ }
5869
5970 clientConfig := api .DefaultConfig ()
6071 clientConfig .Address = addr
@@ -64,38 +75,44 @@ In the config file already exists, the settings will be merged into the existing
6475 return (fmt .Errorf ("error initializing the http client: %s" , apiErr ))
6576 }
6677
78+ prompt .Output ("" )
79+ fmt .Print ("==> Testing Connection..." )
80+
6781 status , statusErr := client .Status ()
6882
6983 if statusErr != nil {
70- fmt .Println (fmt .Errorf ("Could not connect to the Humio server: %s\n Is the address connect and reachable?" , statusErr )) // ERROR
84+ fmt .Println (prompt .Colorize ("[[red]Failed[reset]]" ))
85+ prompt .Output ("" )
86+ prompt .Error (fmt .Sprintf ("Could not connect to the Humio server: %s\n Is the address connect and reachable?" , statusErr ))
7187 continue
7288 }
7389
7490 if status .Status != "ok" {
91+ fmt .Println (prompt .Colorize ("[[red]Failed[reset]]" ))
7592 return (fmt .Errorf ("The server reported that is is malfunctioning, status: %s" , status .Status ))
93+ } else {
94+ fmt .Println (prompt .Colorize ("[[green]Ok[reset]]" ))
7695 }
7796
78- fmt .Println ("" )
79- fmt .Println ("==> Connection Successful" ) // INFO
8097 fmt .Println ("" )
8198 break
8299 }
83100
84- fmt .Println ("" )
85- fmt .Println ("2. Paste in your API Token" ) // INFO
86- fmt .Println ("" )
87- fmt .Println ("To use Humio's CLI you will need to get a copy of your API Token." )
88- fmt .Println ("The API token can be found in your 'Account Settings' section of the UI." )
89- fmt .Println ("If you are running Humio without authorization just leave the API Token field empty." )
90- fmt .Println ("" )
91- prompt .Ask ("We will now open the account page in a browser window. [Hit Any Key]" )
101+ prompt .Info ("Paste in your Personal API Token" )
102+ prompt .Description ("" )
103+ prompt .Description ("To use Humio's CLI you will need to get a copy of your API Token." )
104+ prompt .Description ("The API token can be found in your 'Account Settings' section of the UI." )
105+ prompt .Description ("If you are running Humio without authorization just leave the API Token field empty." )
106+ prompt .Description ("" )
92107
93- open .Start (fmt .Sprintf ("%ssettings" , addr ))
108+ if prompt .Confirm ("Would you like us to open a browser on the account page?" ) {
109+ open .Start (fmt .Sprintf ("%ssettings" , addr ))
94110
95- fmt .Println ("" )
96- fmt .Println (fmt .Sprintf ("If the browser did not open, you can manually visit:" ))
97- fmt .Println (fmt .Sprintf ("%ssettings" , addr ))
98- fmt .Println ("" )
111+ prompt .Description ("" )
112+ prompt .Description (fmt .Sprintf ("If the browser did not open, you can manually visit:" ))
113+ prompt .Description (fmt .Sprintf ("%ssettings" , addr ))
114+ prompt .Description ("" )
115+ }
99116
100117 for true {
101118 token , err = prompt .AskSecret ("API Token" )
@@ -117,28 +134,25 @@ In the config file already exists, the settings will be merged into the existing
117134 username , apiErr := client .Viewer ().Username ()
118135
119136 if apiErr != nil {
120- fmt . Println ( fmt . Errorf ( "authorization failed, try another token" )) // ERROR
137+ prompt . Error ( "authorization failed, try another token" )
121138 continue
122139 }
123140
124141 fmt .Println ("" )
125- fmt .Println (fmt .Sprintf ("==> Login successful '%s' 🎉" , username )) // INFO
142+ fmt .Println ("" )
143+ fmt .Println (prompt .Colorize (fmt .Sprintf ("==> Logged in as: [purple]%s[reset]" , username )))
126144 fmt .Println ("" )
127145 break
128146 }
129147
130148 viper .Set ("address" , addr )
131149 viper .Set ("token" , token )
132150
133- // viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))
134- // viper.BindPFlag("projectbase", rootCmd.PersistentFlags().Lookup("projectbase"))
135- // viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))
136-
137151 configFile := viper .ConfigFileUsed ()
138152
139- fmt .Println ("==> Writing settings to: " + configFile ) // INFO
153+ fmt .Println (prompt . Colorize ( "==> Writing settings to: [purple] " + configFile + "[reset]" ))
140154
141- if writeErr := viper .MergeInConfig (); writeErr != nil {
155+ if writeErr := viper .WriteConfig (); writeErr != nil {
142156 if os .IsNotExist (writeErr ) {
143157 dirName := filepath .Dir (configFile )
144158 if dirErr := os .MkdirAll (dirName , 0700 ); dirErr != nil {
@@ -151,7 +165,7 @@ In the config file already exists, the settings will be merged into the existing
151165 }
152166
153167 fmt .Println ("" )
154- fmt . Println ("Bye bye now!" )
168+ prompt . Output ("Bye bye now! 🎉 " )
155169 fmt .Println ("" )
156170
157171 return nil
0 commit comments