-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow multiple accounts to be saved in config
- Loading branch information
Showing
4 changed files
with
274 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/humio/cli/prompt" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// usersCmd represents the users command | ||
func newLoginChooseCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "choose <account-name>", | ||
Short: "Choose one of your saved accounts to be the active one.", | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
accounts := viper.GetStringMap("accounts") | ||
accountName := args[0] | ||
|
||
account := accounts[accountName] | ||
|
||
if account == nil { | ||
cmd.Println(fmt.Errorf("unknown account %s", accountName)) | ||
os.Exit(1) | ||
} | ||
|
||
address := getMapKey(account, "address") | ||
token := getMapKey(account, "token") | ||
username := getMapKey(account, "username") | ||
|
||
viper.Set("address", address) | ||
viper.Set("token", token) | ||
|
||
if saveErr := saveConfig(); saveErr != nil { | ||
fmt.Println(fmt.Errorf("error saving config: %s", saveErr)) | ||
os.Exit(1) | ||
} | ||
prompt.Info(fmt.Sprintf("Switched to account: '%s'", accountName)) | ||
cmd.Println() | ||
prompt.Output("Address: " + address) | ||
if username != "" { | ||
prompt.Output("Username: " + username) | ||
} | ||
cmd.Println() | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/humio/cli/prompt" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// usersCmd represents the users command | ||
func newLoginListCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "list [flags]", | ||
Short: "Lists saved accounts.", | ||
Long: `Lists accounts shored in your ~/.humio/config.yaml file.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
accounts := viper.GetStringMap("accounts") | ||
|
||
for name, data := range accounts { | ||
login := mapToLogin(data) | ||
if isCurrentAccount(login.address, login.token) { | ||
cmd.Println(prompt.Colorize(fmt.Sprintf("[purple]%s (%s) - %s[reset]", name, login.username, login.address))) | ||
} else { | ||
cmd.Println(fmt.Sprintf("%s (%s) - %s", name, login.username, login.address)) | ||
} | ||
} | ||
|
||
if len(accounts) == 0 { | ||
cmd.Println("You have no saved accounts") | ||
} | ||
|
||
cmd.Println() | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func isCurrentAccount(addr string, token string) bool { | ||
return viper.GetString("address") == addr && viper.GetString("token") == token | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters