Skip to content

Commit

Permalink
Fix global switch functionality to work outside Git repositories
Browse files Browse the repository at this point in the history
- Revise switchUser function to correctly handle --global flag
- Allow global identity switches from any directory
- Provide more specific error messages for missing global or local identities
  • Loading branch information
SergioAtTech9 committed Aug 20, 2024
1 parent a24a05d commit 3959658
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,25 +276,26 @@ func switchUser(c *cli.Context) error {
alias := c.Args().First()
isGlobal := c.Bool("global")

if !isGlobal && !isGitRepository() {
return fmt.Errorf("not in a Git repository. Use --global flag to switch globally")
// Check if we're in a Git repository only if we're not switching globally
if !isGlobal {
if !isGitRepository() {
return fmt.Errorf("not in a Git repository. Use --global flag to switch globally")
}
}

var config UserConfig
var ok bool

if isGlobal {
config, ok = configs.Global[alias]
if !ok {
return fmt.Errorf("no global identity found for alias '%s'", alias)
}
} else {
config, ok = configs.Local[alias]
}

if !ok {
scopeType := "local"
if isGlobal {
scopeType = "global"
if !ok {
return fmt.Errorf("no local identity found for alias '%s'", alias)
}
return fmt.Errorf("no %s identity found for alias '%s'", scopeType, alias)
}

// Clear existing SSH keys from the agent
Expand Down

0 comments on commit 3959658

Please sign in to comment.