From e917f5d7840773a3cc48a838fc72c432eb0c7b89 Mon Sep 17 00:00:00 2001 From: Dmitry Chepurovskiy Date: Fri, 28 Jun 2019 01:59:22 +0300 Subject: [PATCH] Added list command --- cmd/add.go | 6 ++---- cmd/helpers.go | 18 +++++++++++++++++- cmd/list.go | 40 ++++++++++++++++++++++++++++++++++++++++ cmd/remove.go | 7 ++----- cmd/root.go | 2 +- 5 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 cmd/list.go diff --git a/cmd/add.go b/cmd/add.go index 18bde90..04ec194 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -3,7 +3,6 @@ package cmd import ( "fmt" "os" - "path" "github.com/dm3ch/git-profile-manager/profile" @@ -33,9 +32,8 @@ var addCmd = &cobra.Command{ promptGitUser(&profile.User) } - path := path.Join(configDir, profile.Name+".profile") - _, err = os.Stat(path) - profileExists := !os.IsNotExist(err) + path := getProfilePath(configDir, profile.Name) + profileExists := isFileExist(path) if profileExists && !force { force = promptYesNo(fmt.Sprintf("Override existing %s profile", profile.Name)) diff --git a/cmd/helpers.go b/cmd/helpers.go index e8c3860..68d8cc8 100644 --- a/cmd/helpers.go +++ b/cmd/helpers.go @@ -4,12 +4,17 @@ import ( "bufio" "fmt" "os" + "path/filepath" "github.com/dm3ch/git-profile-manager/profile" "github.com/mitchellh/go-homedir" "github.com/spf13/viper" ) +const ( + profileExtention = "profile" +) + // Create directory if it doesn't exists func CreateDirIfNotExist(dir string) error { if _, err := os.Stat(dir); os.IsNotExist(err) { @@ -37,7 +42,7 @@ func prompt(label string) string { fmt.Printf("%s: ", label) str, err := reader.ReadString('\n') if err != nil { - fmt.Println("Prompt failed") + fmt.Println("Prompt failed:") fmt.Println(err) os.Exit(1) } @@ -56,3 +61,14 @@ func promptYesNo(label string) bool { answer := prompt(label + " [y/N]") return (answer == "y" || answer == "Y") } + +// Get profile file path +func getProfilePath(configDir, profileName string) string { + return filepath.Join(configDir, profileName+"."+profileExtention) +} + +// Check if file exists +func isFileExist(path string) bool { + _, err := os.Stat(path) + return !os.IsNotExist(err) +} diff --git a/cmd/list.go b/cmd/list.go new file mode 100644 index 0000000..c1e2643 --- /dev/null +++ b/cmd/list.go @@ -0,0 +1,40 @@ +package cmd + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/spf13/cobra" +) + +var listCmd = &cobra.Command{ + Use: "list", + Short: "List git profiles", + Run: func(cmd *cobra.Command, args []string) { + configDir, err := GetConfigDirAbsolutePath() + if err != nil { + fmt.Println("Can't get configuration directory absolute path:") + fmt.Println(err) + os.Exit(1) + } + + profiles, err := filepath.Glob(getProfilePath(configDir, "*")) + if err != nil { + fmt.Println("Can't list profiles:") + fmt.Println(err) + os.Exit(1) + } + + fmt.Println("Existing profiles:") + for _, profile := range profiles { + profileName := filepath.Base(profile) + profileName = profileName[:len(profileName)-len(profileExtention)-1] + fmt.Println(profileName) + } + }, +} + +func init() { + rootCmd.AddCommand(listCmd) +} diff --git a/cmd/remove.go b/cmd/remove.go index 4ef4ac8..8ad7a0e 100644 --- a/cmd/remove.go +++ b/cmd/remove.go @@ -3,7 +3,6 @@ package cmd import ( "fmt" "os" - "path" "github.com/spf13/cobra" ) @@ -23,10 +22,8 @@ var removeCmd = &cobra.Command{ os.Exit(1) } - path := path.Join(configDir, profileName+".profile") - _, err = os.Stat(path) - profileExists := !os.IsNotExist(err) - + path := getProfilePath(configDir, profileName) + profileExists := isFileExist(path) if !profileExists { fmt.Printf("Profile %s does not exists\n", profileName) return diff --git a/cmd/root.go b/cmd/root.go index 682adb0..21cbe29 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -28,7 +28,7 @@ user profiles in your git configurations`, err = CreateDirIfNotExist(configDir) if err != nil { - fmt.Println("Can't create config directory") + fmt.Println("Can't create config directory:") fmt.Println(err) os.Exit(1) }