From 50f211800946318ff4e3c40c1fc497c149b0380d Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Tue, 4 Oct 2022 00:09:34 +0530 Subject: Use the macli.toml file to store and get login info automatically --- auth/client.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'auth/client.go') diff --git a/auth/client.go b/auth/client.go index 6ec75ca..5a3e501 100644 --- a/auth/client.go +++ b/auth/client.go @@ -22,15 +22,36 @@ import ( "os" "fmt" "github.com/zalando/go-keyring" + "github.com/spf13/viper" + "errors" ) var clientSuffix string = "-client-id" func getClientId() (string, error) { - return keyring.Get(serviceName + clientSuffix, userName) + var id string + var err error + + + if NoSysKeyring { + id = viper.GetString("auth.client_id") + if id == "" { + err = errors.New("secret not found in keyring") + } + } else { + id, err = keyring.Get(serviceName + clientSuffix, userName) + } + + return id, err } func setClientId(clientId string) { + if NoSysKeyring { + defer viper.WriteConfig() + viper.Set("auth.client_id", clientId) + return + } + err := keyring.Set(serviceName + clientSuffix, userName, clientId) if err != nil { fmt.Println("Error while writing Client ID to keychain", err) @@ -39,9 +60,14 @@ func setClientId(clientId string) { } func deleteClientId() { + if NoSysKeyring { + defer viper.WriteConfig() + viper.Set("auth.client_id", "") + } + err := keyring.Delete(serviceName + clientSuffix, userName) // if secret doesnt exist dont show error - if err != nil { + if err != nil && !NoSysKeyring { if err.Error() != "secret not found in keyring" { fmt.Println("Error while deleting Client ID", err.Error()) os.Exit(1) -- cgit v1.2.3