Skip to content

Commit

Permalink
Added list command
Browse files Browse the repository at this point in the history
  • Loading branch information
dm3ch committed Jul 3, 2019
1 parent 9fc9690 commit e917f5d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 11 deletions.
6 changes: 2 additions & 4 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"os"
"path"

"github.com/dm3ch/git-profile-manager/profile"

Expand Down Expand Up @@ -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))
Expand Down
18 changes: 17 additions & 1 deletion cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
40 changes: 40 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -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)
}
7 changes: 2 additions & 5 deletions cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"os"
"path"

"github.com/spf13/cobra"
)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit e917f5d

Please sign in to comment.