Skip to content

Commit

Permalink
✨ 🐛 add proper error handling when local config is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Shieldine committed Jul 26, 2024
1 parent 208f75d commit dfc38ec
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 29 deletions.
4 changes: 2 additions & 2 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ 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)
}

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)
}
Expand Down
5 changes: 1 addition & 4 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
15 changes: 11 additions & 4 deletions cmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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, &notSetErr) {
fmt.Println("error: ", err)
os.Exit(1)
}
}

if profile.Name == currentName && profile.Email == currentEmail {
Expand Down
48 changes: 35 additions & 13 deletions cmd/tempset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
`,
Expand All @@ -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, &notSetErr) {
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)
}
}
Expand All @@ -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, &notSetErr) {
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)
}
}
Expand Down
11 changes: 11 additions & 0 deletions custom_errors/NotSet.go
Original file line number Diff line number Diff line change
@@ -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)
}
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
26 changes: 22 additions & 4 deletions internal/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"os"
"os/exec"
"strings"

"github.com/Shieldine/git-profile/custom_errors"
)

func CheckGitRepo() bool {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit dfc38ec

Please sign in to comment.