From 1a4eb9e3503baef91e4f48b251629eb5fc652215 Mon Sep 17 00:00:00 2001 From: Disco Date: Wed, 20 Apr 2022 02:45:49 +0100 Subject: [PATCH 1/6] Sanitised and refined file package. --- pkg/file/file.go | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/pkg/file/file.go b/pkg/file/file.go index d6aba5c..321f562 100644 --- a/pkg/file/file.go +++ b/pkg/file/file.go @@ -1,24 +1,16 @@ package file import ( - "errors" + "github.com/mitchellh/go-homedir" "io/ioutil" "os" "path/filepath" ) -func HomeDir() string { - home := os.Getenv("HOME") - if home == "" { - home = os.Getenv("USERPROFILE") // windows - } - return home -} - func PrevProfileFile() (string, error) { - home := HomeDir() - if home == "" { - return "", errors.New("HOME or USERPROFILE environment variable not set") + home, err := homedir.Dir() + if err != nil { + return "", err } return filepath.Join(home, ".gcps"), nil } @@ -28,10 +20,11 @@ func WriteLastProfile(value string) error { if err != nil { return err } - dir := filepath.Dir(path) - if err := os.MkdirAll(dir, 0750); err != nil { + + if err := os.MkdirAll(filepath.Dir(path), 0750); err != nil { return err } + return ioutil.WriteFile(path, []byte(value), 0600) } @@ -40,10 +33,11 @@ func ReadLastProfile() (string, error) { if err != nil { return "", err } + b, err := ioutil.ReadFile(filepath.Clean(path)) if os.IsNotExist(err) { return "", nil } - filepath.Clean(path) + return string(b), err } From 7dcf30b7276d7c0e47528af68a6904b26900ebb6 Mon Sep 17 00:00:00 2001 From: Disco Date: Wed, 20 Apr 2022 02:52:54 +0100 Subject: [PATCH 2/6] Inverted want for failing test case. --- pkg/gcp/gcp.go | 9 +++++---- pkg/gcp/gcp_test.go | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/pkg/gcp/gcp.go b/pkg/gcp/gcp.go index 24bc31c..6b42e76 100644 --- a/pkg/gcp/gcp.go +++ b/pkg/gcp/gcp.go @@ -7,7 +7,7 @@ import ( "github.com/daftcreations/gcps/pkg/types" ) -// get list of profile +// GetProfileList gets a list of Google cloud profiles. func GetProfileList() ([]types.List, error) { // run cmd to get list of profiles out, err := exec.Command("gcloud", "config", "configurations", "list", "--format=json").Output() @@ -16,7 +16,7 @@ func GetProfileList() ([]types.List, error) { } // list of profiles - list := []types.List{} + var list []types.List // unmarshal to get the desired value if err := json.Unmarshal(out, &list); err != nil { @@ -26,7 +26,7 @@ func GetProfileList() ([]types.List, error) { return list, nil } -// set the given profile +// SetProfile sets the given Google cloud profile. func SetProfile(profile string) error { if err := exec.Command("gcloud", "config", "configurations", "activate", profile).Run(); err != nil { return err @@ -34,7 +34,7 @@ func SetProfile(profile string) error { return nil } -// Check if the given profile exists in given array +// ContainsProfile checks if the given Google cloud profile exists in given array. func ContainsProfile(arr []types.List, profile string) bool { for _, n := range arr { if n.Name == profile { @@ -44,6 +44,7 @@ func ContainsProfile(arr []types.List, profile string) bool { return false } +// GetActiveProfile gets the active Google Cloud profile. func GetActiveProfile(list []types.List) string { for _, n := range list { if n.IsActive { diff --git a/pkg/gcp/gcp_test.go b/pkg/gcp/gcp_test.go index 98f633d..76b4fc6 100644 --- a/pkg/gcp/gcp_test.go +++ b/pkg/gcp/gcp_test.go @@ -55,7 +55,7 @@ func TestContainsProfile(t *testing.T) { }, }, profile: "abc", - want: false, + want: true, }, } From 39d04a0e14b9894d949487494a7640e24d00d1b0 Mon Sep 17 00:00:00 2001 From: Disco Date: Wed, 20 Apr 2022 02:53:15 +0100 Subject: [PATCH 3/6] Updated typo in main --- main.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index b945797..403ab12 100644 --- a/main.go +++ b/main.go @@ -20,8 +20,8 @@ func main() { return } - // store seleted profile - var seletedProfile string + // store selected profile + var selectedProfile string list, err := gcp.GetProfileList() if err != nil { @@ -45,12 +45,12 @@ func main() { } else if lastProfile == "" { fmt.Println("No previous profile found. Please select profile from below") } else { - seletedProfile = lastProfile + selectedProfile = lastProfile } } else { isContain := gcp.ContainsProfile(list, os.Args[1]) if isContain { - seletedProfile = os.Args[1] + selectedProfile = os.Args[1] } else { log.Printf("Given profile %s is invalide please select from below!\n", os.Args[1]) } @@ -58,8 +58,8 @@ func main() { } } - if len(seletedProfile) == 0 { - seletedProfile, err = cmd.GetProfileFromUser(list) + if len(selectedProfile) == 0 { + selectedProfile, err = cmd.GetProfileFromUser(list) if err != nil { log.Fatalf("Error while setting up cli: %v", err) } @@ -67,12 +67,12 @@ func main() { activeProfile := gcp.GetActiveProfile(list) if activeProfile == "" { - err := file.WriteLastProfile(seletedProfile) + err := file.WriteLastProfile(selectedProfile) if err != nil { log.Fatalf("Error while writing last activate profile: %v", err) } - } else if activeProfile == seletedProfile { - fmt.Printf("Activate profile %s. No need to switch again\n", seletedProfile) + } else if activeProfile == selectedProfile { + fmt.Printf("Activate profile %s. No need to switch again\n", selectedProfile) return } else { err := file.WriteLastProfile(activeProfile) @@ -81,9 +81,9 @@ func main() { } } - if err := gcp.SetProfile(seletedProfile); err != nil { + if err := gcp.SetProfile(selectedProfile); err != nil { log.Fatalf("Error setting up the profile: %v", err) } - fmt.Printf("Switched profile to %s successfully!!\n", seletedProfile) + fmt.Printf("Switched profile to %s successfully!!\n", selectedProfile) } From 0f46d86ca9dfa3344312ec70c9db66f94f1ae22d Mon Sep 17 00:00:00 2001 From: Disco Date: Wed, 20 Apr 2022 02:54:15 +0100 Subject: [PATCH 4/6] Sanitised comment for List --- pkg/types/types.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/types/types.go b/pkg/types/types.go index d9cb982..13ba704 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -1,6 +1,6 @@ package types -// to store gcloud configurations json +// List stores gcloud configurations type List struct { Name string `json:"name"` IsActive bool `json:"is_active"` From 2c791ad4f0ffd3aa2e8d3e331d6fd5a8a9cc65c7 Mon Sep 17 00:00:00 2001 From: Disco Date: Wed, 20 Apr 2022 02:56:13 +0100 Subject: [PATCH 5/6] Updated comment for GetProfileFromUser --- pkg/cmd/cmd.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/cmd/cmd.go b/pkg/cmd/cmd.go index 74d68b8..5c165f3 100644 --- a/pkg/cmd/cmd.go +++ b/pkg/cmd/cmd.go @@ -7,6 +7,7 @@ import ( "github.com/manifoldco/promptui" ) +// GetProfileFromUser prompts user to select desires profile from provided Google Cloud profiles list. func GetProfileFromUser(list []types.List) (string, error) { templates := &promptui.SelectTemplates{ Label: "{{ . }}?", From 4fe1ca366da8878c45cc840f956fffbf033caddb Mon Sep 17 00:00:00 2001 From: Disco Date: Wed, 20 Apr 2022 02:56:33 +0100 Subject: [PATCH 6/6] Go mod update. --- go.mod | 2 ++ go.sum | 2 ++ 2 files changed, 4 insertions(+) diff --git a/go.mod b/go.mod index 0c061c4..de85e1d 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,8 @@ module github.com/daftcreations/gcps go 1.17 +require github.com/mitchellh/go-homedir v1.1.0 + require ( github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect github.com/davecgh/go-spew v1.1.1 // indirect diff --git a/go.sum b/go.sum index 0d6dfec..5918ad2 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA= github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=