Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitaiton & Clean Up. #30

Merged
merged 7 commits into from Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -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=
Expand Down
22 changes: 11 additions & 11 deletions main.go
Expand Up @@ -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 {
Expand All @@ -45,34 +45,34 @@ 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])
}

}
}

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)
}
}

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)
Expand All @@ -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)
}
1 change: 1 addition & 0 deletions pkg/cmd/cmd.go
Expand Up @@ -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: "{{ . }}?",
Expand Down
24 changes: 9 additions & 15 deletions 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
}
Expand All @@ -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)
}

Expand All @@ -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
}
9 changes: 5 additions & 4 deletions pkg/gcp/gcp.go
Expand Up @@ -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()
Expand All @@ -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 {
Expand All @@ -26,15 +26,15 @@ 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
}
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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/gcp/gcp_test.go
Expand Up @@ -55,7 +55,7 @@ func TestContainsProfile(t *testing.T) {
},
},
profile: "abc",
want: false,
want: true,
},
}

Expand Down
2 changes: 1 addition & 1 deletion 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"`
Expand Down