implement everything stable, account switching feature

This commit is contained in:
Daveberry committed 2026-06-09 19:06:30 +08:00
1 parent 3e87fa0d53
commit f2508d0bc8
11 files changed
+292 -79

No files matched your search

+36 -11
View File
@@ -2,10 +2,13 @@ package cmd
import (
"fmt"
"os"
"path/filepath"
"github.com/daveberrys/guh/src/utils"
"github.com/spf13/cobra"
)
// COMMAND LINE INTERFACE GOES HERE
func init() {
rootCmd.AddCommand(switchCmd)
switchCmd.AddCommand(switchBranchCmd)
@@ -21,8 +24,8 @@ var switchBranchCmd = &cobra.Command{
Use: "branch [name]",
Short: "Switch to a specific branch",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
performBranchSwitch(args[0])
RunE: func(cmd *cobra.Command, args []string) error {
return performBranchSwitch(args[0])
},
}
@@ -30,16 +33,38 @@ var switchAccountCmd = &cobra.Command{
Use: "account [username]",
Short: "Switch to a specific user account",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
performAccountSwitch(args[0])
RunE: func(cmd *cobra.Command, args []string) error {
return performAccountSwitch(args[0])
},
}
// PERFORMERS GO HERE
func performAccountSwitch(username string) {
fmt.Printf("Switched to account: %s\n", username)
func performAccountSwitch(username string) error {
account, err := utils.FindAccount(username)
if err != nil {
return err
}
if err := utils.RunGitSequence(
[]string{"config", "--global", "user.name", account.Username},
[]string{"config", "--global", "user.email", account.Email},
); err != nil {
return err
}
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}
credentials := fmt.Sprintf("https://%s:%s@github.com\n", account.Username, account.ClassicToken)
if err := os.WriteFile(filepath.Join(homeDir, ".git-credentials"), []byte(credentials), 0600); err != nil {
return err
}
fmt.Printf("Switched to account: %s\n", account.Username)
return nil
}
func performBranchSwitch(branchName string) {
fmt.Printf("Switched to branch: %s\n", branchName)
}
func performBranchSwitch(branchName string) error {
return utils.RunGitSequence([]string{"switch", branchName})
}