diff --git a/cmd/check.go b/cmd/check.go index 2290a81..d34004a 100644 --- a/cmd/check.go +++ b/cmd/check.go @@ -31,7 +31,7 @@ func runCheck(*cobra.Command, []string) { name, err := internal.GetUserName() if err != nil { - fmt.Println("error: not a git repository or username not set") + fmt.Printf("error: %v\n", err) } else { fmt.Printf("Current name: %s\n", name) } @@ -39,7 +39,7 @@ func runCheck(*cobra.Command, []string) { email, err := internal.GetUserEmail() if err != nil { - fmt.Println("error: not a git repository or email not set") + fmt.Printf("error: %v\n", err) } else { fmt.Printf("Current email: %s\n", email) } diff --git a/cmd/init.go b/cmd/init.go index 2dc0147..0420076 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -150,10 +150,7 @@ func CredentialsAlreadySet(profile models.ProfileConfig) bool { currentName, _ := internal.GetUserName() currentEmail, _ := internal.GetUserEmail() - if profile.Name == currentName && profile.Email == currentEmail { - return true - } - return false + return profile.Name == currentName && profile.Email == currentEmail } func init() { diff --git a/cmd/set.go b/cmd/set.go index fb0ab0b..a46c180 100644 --- a/cmd/set.go +++ b/cmd/set.go @@ -16,12 +16,15 @@ package cmd import ( "bufio" + "errors" "fmt" + "github.com/Shieldine/git-profile/custom_errors" + "os" + "strings" + "github.com/Shieldine/git-profile/internal" "github.com/Shieldine/git-profile/models" "github.com/spf13/cobra" - "os" - "strings" ) var setCmd = &cobra.Command{ @@ -67,9 +70,13 @@ func runSet(cmd *cobra.Command, args []string) { currentName, err := internal.GetUserName() currentEmail, _ := internal.GetUserEmail() + if err != nil { - fmt.Println(err) - os.Exit(1) + var notSetErr *custom_errors.NotSetError + if !errors.As(err, ¬SetErr) { + fmt.Println("error: ", err) + os.Exit(1) + } } if profile.Name == currentName && profile.Email == currentEmail { diff --git a/cmd/tempset.go b/cmd/tempset.go index 73d8a1b..6a6ab8d 100644 --- a/cmd/tempset.go +++ b/cmd/tempset.go @@ -16,17 +16,21 @@ package cmd import ( "bufio" + "errors" "fmt" - "github.com/Shieldine/git-profile/internal" - "github.com/spf13/cobra" "os" "strings" + + "github.com/Shieldine/git-profile/custom_errors" + "github.com/Shieldine/git-profile/internal" + "github.com/spf13/cobra" ) var tempSetCmd = &cobra.Command{ Use: "tempset", Short: "Set credentials without defining a profile", - Long: `Set git credentials for the current repository without saving them in a profile. + Long: ` +Set git credentials for the current repository without saving them in a profile. The credentials can be passed as flags right away. If you don't pass them, you will be asked to provide a name and an email. `, @@ -41,25 +45,35 @@ func runTempSet(*cobra.Command, []string) { currentName, err := internal.GetUserName() if err != nil { - fmt.Println(err) - os.Exit(1) + var notSetErr *custom_errors.NotSetError + + if !errors.As(err, ¬SetErr) { + fmt.Printf("error: %v\n", err) + os.Exit(1) + } + } + + if currentName != "" { + fmt.Printf("Name (enter to keep %s): ", currentName) + } else { + fmt.Print("Name: ") } - fmt.Printf("Name (enter to keep %s): ", currentName) name, _ = reader.ReadString('\n') name = strings.TrimSpace(name) if name != "" { err = internal.SetUserName(name) if err != nil { - fmt.Printf("Error setting user name: %s\n", err) + fmt.Printf("Error while setting user name: %s\n", err) os.Exit(1) } } } else { err := internal.SetUserName(name) + if err != nil { - fmt.Printf("Error setting user name: %s\n", err) + fmt.Printf("Error while setting user name: %s\n", err) os.Exit(1) } } @@ -68,25 +82,33 @@ func runTempSet(*cobra.Command, []string) { currentEmail, err := internal.GetUserEmail() if err != nil { - fmt.Println(err) - os.Exit(1) + var notSetErr *custom_errors.NotSetError + + if !errors.As(err, ¬SetErr) { + fmt.Printf("error: %v\n", err) + os.Exit(1) + } } - fmt.Printf("E-mail (enter to keep %s): ", currentEmail) + if currentEmail != "" { + fmt.Printf("Name (enter to keep %s): ", currentEmail) + } else { + fmt.Print("E-Mail: ") + } email, _ = reader.ReadString('\n') email = strings.TrimSpace(email) if email != "" { err = internal.SetUserEmail(email) if err != nil { - fmt.Printf("Error setting user email: %s\n", err) + fmt.Printf("Error while setting user email: %s\n", err) os.Exit(1) } } } else { err := internal.SetUserEmail(email) if err != nil { - fmt.Printf("Error setting user email: %s\n", err) + fmt.Printf("Error while setting user email: %s\n", err) os.Exit(1) } } diff --git a/custom_errors/NotSet.go b/custom_errors/NotSet.go new file mode 100644 index 0000000..0b27f22 --- /dev/null +++ b/custom_errors/NotSet.go @@ -0,0 +1,11 @@ +package custom_errors + +import "fmt" + +type NotSetError struct { + ConfigName string +} + +func (e *NotSetError) Error() string { + return fmt.Sprintf("no local %s set", e.ConfigName) +} diff --git a/go.mod b/go.mod index c91d320..2e510b0 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,11 @@ module github.com/Shieldine/git-profile go 1.22.4 require ( - github.com/BurntSushi/toml v1.4.0 // indirect + github.com/BurntSushi/toml v1.4.0 + github.com/spf13/cobra v1.8.1 +) + +require ( github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/spf13/cobra v1.8.1 // indirect github.com/spf13/pflag v1.0.5 // indirect ) diff --git a/internal/git.go b/internal/git.go index c15db9c..bdd21dc 100644 --- a/internal/git.go +++ b/internal/git.go @@ -19,6 +19,8 @@ import ( "os" "os/exec" "strings" + + "github.com/Shieldine/git-profile/custom_errors" ) func CheckGitRepo() bool { @@ -94,9 +96,17 @@ func GetUserName() (string, error) { return "", errors.New("not a git repository") } cmd := exec.Command("git", "config", "--get", "--local", "user.name") - output, err := cmd.Output() + output, err := cmd.CombinedOutput() + if err != nil { - return "", err + var exitError *exec.ExitError + + ok := errors.As(err, &exitError) + if ok && exitError.ExitCode() == 1 && err.Error() == "exit status 1" { + return "", &custom_errors.NotSetError{ConfigName: "username"} + } else { + return "", err + } } return strings.TrimSpace(string(output)), nil } @@ -116,9 +126,17 @@ func GetUserEmail() (string, error) { return "", errors.New("not a git repository") } cmd := exec.Command("git", "config", "--get", "--local", "user.email") - output, err := cmd.Output() + output, err := cmd.CombinedOutput() + if err != nil { - return "", err + var exitError *exec.ExitError + + ok := errors.As(err, &exitError) + if ok && exitError.ExitCode() == 1 && err.Error() == "exit status 1" { + return "", &custom_errors.NotSetError{ConfigName: "email"} + } else { + return "", err + } } return strings.TrimSpace(string(output)), nil }