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
+289 -76

No files matched your search

+2
View File
@@ -0,0 +1,2 @@
guh
guh.exe
+21
View File
@@ -0,0 +1,21 @@
package cmd
import (
"github.com/daveberrys/guh/src/utils"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(commitCmd)
}
var commitCmd = &cobra.Command{
Use: "commit",
Short: "Add current changes and commit",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
utils.RunGitSequence([]string{"commit", "-m", args[0]})
utils.RunGitSequence([]string{"add", "."})
return nil
},
}
+52
View File
@@ -0,0 +1,52 @@
package cmd
import (
"fmt"
"strings"
"github.com/daveberrys/guh/src/utils"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(createCmd)
createCmd.AddCommand(createBranchCmd)
createCmd.AddCommand(createAccountCmd)
}
var createCmd = &cobra.Command{
Use: "create",
Short: "Create branches or accounts",
}
var createBranchCmd = &cobra.Command{
Use: "branch [branch]",
Short: "Create and switch to a branch",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return utils.RunGitSequence([]string{"switch", "-c", args[0]})
},
}
var createAccountCmd = &cobra.Command{
Use: "account [username] [email] [classicToken]",
Short: "Create or update an account",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
account := utils.Account{
Username: args[0],
Email: args[1],
ClassicToken: args[2],
}
if strings.TrimSpace(account.Username) == "" || strings.TrimSpace(account.Email) == "" || strings.TrimSpace(account.ClassicToken) == "" {
return fmt.Errorf("username, email, and classicToken are required")
}
if err := utils.UpsertAccount(account); err != nil {
return err
}
fmt.Printf("Saved account: %s\n", account.Username)
return nil
},
}
+19
View File
@@ -0,0 +1,19 @@
package cmd
import (
"github.com/daveberrys/guh/src/utils"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(diffCmd)
}
var diffCmd = &cobra.Command{
Use: "diff",
Short: "Show working tree changes",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return utils.RunGitSequence([]string{"diff"})
},
}
+9 -17
View File
@@ -1,10 +1,7 @@
package cmd package cmd
import ( import (
"fmt" "github.com/daveberrys/guh/src/utils"
"os/exec"
"os"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@@ -13,18 +10,13 @@ func init() {
} }
var pullCmd = &cobra.Command{ var pullCmd = &cobra.Command{
Use: "pull [source]", Use: "pull",
Short: "Pull updates from a source", Short: "Fetch and pull updates",
Args: cobra.MaximumNArgs(1), Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) { RunE: func(cmd *cobra.Command, args []string) error {
return utils.RunGitSequence(
out, err := exec.Command("git", "fetch").CombinedOutput() []string{"fetch"},
if err != nil { fmt.Fprintf(os.Stderr, "error: %s\n", err) } []string{"pull"},
)
out, err = exec.Command("git", "pull").CombinedOutput()
if err != nil { fmt.Fprintf(os.Stderr, "error: %s\n", err) }
fmt.Print(string(out))
}, },
} }
+6 -15
View File
@@ -1,10 +1,7 @@
package cmd package cmd
import ( import (
"fmt" "github.com/daveberrys/guh/src/utils"
"os"
"os/exec"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@@ -13,16 +10,10 @@ func init() {
} }
var pushCmd = &cobra.Command{ var pushCmd = &cobra.Command{
Use: "push [destination]", Use: "push",
Short: "Push changes to a destination", Short: "Add current changes and push",
Args: cobra.MaximumNArgs(1), Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) { RunE: func(cmd *cobra.Command, args []string) error {
dest := "main" return utils.RunGitSequence([]string{"push"})
if len(args) > 0 { dest = args[0] }
fmt.Printf("Pushing changes to: %s...\n", dest)
out, err := exec.Command("git", "push", "origin", dest).CombinedOutput()
if err != nil { fmt.Fprintf(os.Stderr, "error: %s\n", err) }
fmt.Print(string(out))
}, },
} }
-31
View File
@@ -1,31 +0,0 @@
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// COMMAND LINE INTERFACE GOES HERE
func init() {
rootCmd.AddCommand(saveCmd)
saveCmd.AddCommand(accountSaveCmd)
}
var saveCmd = &cobra.Command{
Use: "save",
Short: "Save accounts,",
}
var accountSaveCmd = &cobra.Command{
Use: "account [username]",
Short: "Save a specific user account",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
performAccountSave(args[0])
},
}
// PERFORMERS GO HERE
func performAccountSave(username string) {
fmt.Printf("Saved account: %s\n", username)
}
+19
View File
@@ -0,0 +1,19 @@
package cmd
import (
"github.com/daveberrys/guh/src/utils"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(stashCmd)
}
var stashCmd = &cobra.Command{
Use: "stash",
Short: "Stash current changes",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return utils.RunGitSequence([]string{"stash"})
},
}
+35 -10
View File
@@ -2,10 +2,13 @@ package cmd
import ( import (
"fmt" "fmt"
"os"
"path/filepath"
"github.com/daveberrys/guh/src/utils"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
// COMMAND LINE INTERFACE GOES HERE
func init() { func init() {
rootCmd.AddCommand(switchCmd) rootCmd.AddCommand(switchCmd)
switchCmd.AddCommand(switchBranchCmd) switchCmd.AddCommand(switchBranchCmd)
@@ -21,8 +24,8 @@ var switchBranchCmd = &cobra.Command{
Use: "branch [name]", Use: "branch [name]",
Short: "Switch to a specific branch", Short: "Switch to a specific branch",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) { RunE: func(cmd *cobra.Command, args []string) error {
performBranchSwitch(args[0]) return performBranchSwitch(args[0])
}, },
} }
@@ -30,16 +33,38 @@ var switchAccountCmd = &cobra.Command{
Use: "account [username]", Use: "account [username]",
Short: "Switch to a specific user account", Short: "Switch to a specific user account",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) { RunE: func(cmd *cobra.Command, args []string) error {
performAccountSwitch(args[0]) return performAccountSwitch(args[0])
}, },
} }
// PERFORMERS GO HERE func performAccountSwitch(username string) error {
func performAccountSwitch(username string) { account, err := utils.FindAccount(username)
fmt.Printf("Switched to account: %s\n", username) if err != nil {
return err
} }
func performBranchSwitch(branchName string) { if err := utils.RunGitSequence(
fmt.Printf("Switched to branch: %s\n", branchName) []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) error {
return utils.RunGitSequence([]string{"switch", branchName})
} }
+27
View File
@@ -0,0 +1,27 @@
package utils
import (
"fmt"
"os"
"os/exec"
"strings"
)
func RunGit(args ...string) error {
gitCmd := exec.Command("git", args...)
gitCmd.Stdin = os.Stdin
gitCmd.Stdout = os.Stdout
gitCmd.Stderr = os.Stderr
return gitCmd.Run()
}
func RunGitSequence(commands ...[]string) error {
for _, commandArgs := range commands {
if err := RunGit(commandArgs...); err != nil {
return fmt.Errorf("git %s failed: %w", strings.Join(commandArgs, " "), err)
}
}
return nil
}
+99 -3
View File
@@ -1,7 +1,103 @@
package utils package utils
import ("fmt") import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
)
func init() { const appDirName = "dev.pages.codedave.guh"
fmt.Println("json read, save, delete logic goes here") const accountsFileName = "accounts.json"
type Account struct {
Username string `json:"username"`
Email string `json:"email"`
ClassicToken string `json:"classic_token"`
}
func AccountsFilePath() (string, error) {
configDir, err := os.UserConfigDir()
if err != nil {
return "", err
}
return filepath.Join(configDir, appDirName, accountsFileName), nil
}
func LoadAccounts() ([]Account, error) {
accountsFilePath, err := AccountsFilePath()
if err != nil {
return nil, err
}
contents, err := os.ReadFile(accountsFilePath)
if errors.Is(err, os.ErrNotExist) {
return []Account{}, nil
}
if err != nil {
return nil, err
}
if len(contents) == 0 {
return []Account{}, nil
}
var accounts []Account
if err := json.Unmarshal(contents, &accounts); err != nil {
return nil, fmt.Errorf("read accounts file: %w", err)
}
return accounts, nil
}
func SaveAccounts(accounts []Account) error {
accountsFilePath, err := AccountsFilePath()
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(accountsFilePath), 0700); err != nil {
return err
}
contents, err := json.MarshalIndent(accounts, "", " ")
if err != nil {
return err
}
contents = append(contents, '\n')
return os.WriteFile(accountsFilePath, contents, 0600)
}
func FindAccount(username string) (Account, error) {
accounts, err := LoadAccounts()
if err != nil {
return Account{}, err
}
for _, account := range accounts {
if account.Username == username {
return account, nil
}
}
return Account{}, fmt.Errorf("account %q not found", username)
}
func UpsertAccount(newAccount Account) error {
accounts, err := LoadAccounts()
if err != nil {
return err
}
for index, account := range accounts {
if account.Username == newAccount.Username {
accounts[index] = newAccount
return SaveAccounts(accounts)
}
}
accounts = append(accounts, newAccount)
return SaveAccounts(accounts)
} }