Files
guh/src/cmd/switch/account.go
T

35 lines
1.0 KiB
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", "credential.helper", "store"},
[]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
},
}