mirror of
https://github.com/daveberrys/guh.git
synced 2026-11-29 00:26:46 +08:00
34 lines
987 B
Go
34 lines
987 B
Go
package switchcmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/daveberrys/guh/src/utils"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var switchAccountCmd = &cobra.Command{
|
|
Use: "account [username]",
|
|
Short: "Switch to a specific user account",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(c *cobra.Command, args []string) error {
|
|
account, err := utils.FindAccount(args[0])
|
|
if err != nil { return err }
|
|
|
|
if err := utils.RunGitSequence(false,
|
|
[]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@%s\n", account.Username, account.ClassicToken, account.Platform)
|
|
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
|
|
},
|
|
} |