From f2508d0bc88fea185fdd30516aff3a8f77cff68b Mon Sep 17 00:00:00 2001 From: Daveberry <84567826+daveberrys@users.noreply.github.com> Date: Tue, 9 Jun 2026 19:06:30 +0800 Subject: [PATCH] implement everything stable, account switching feature --- .gitignore | 2 + src/cmd/commit.go | 21 ++++++++++ src/cmd/create.go | 52 +++++++++++++++++++++++ src/cmd/diff.go | 19 +++++++++ src/cmd/pull.go | 28 +++++-------- src/cmd/push.go | 21 +++------- src/cmd/save.go | 31 -------------- src/cmd/stash.go | 19 +++++++++ src/cmd/switch.go | 47 ++++++++++++++++----- src/utils/git.go | 27 ++++++++++++ src/utils/json.go | 104 ++++++++++++++++++++++++++++++++++++++++++++-- 11 files changed, 292 insertions(+), 79 deletions(-) create mode 100644 .gitignore create mode 100644 src/cmd/commit.go create mode 100644 src/cmd/create.go create mode 100644 src/cmd/diff.go delete mode 100644 src/cmd/save.go create mode 100644 src/cmd/stash.go create mode 100644 src/utils/git.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a48cc5d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +guh +guh.exe \ No newline at end of file diff --git a/src/cmd/commit.go b/src/cmd/commit.go new file mode 100644 index 0000000..f769873 --- /dev/null +++ b/src/cmd/commit.go @@ -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 + }, +} diff --git a/src/cmd/create.go b/src/cmd/create.go new file mode 100644 index 0000000..d17fe31 --- /dev/null +++ b/src/cmd/create.go @@ -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 + }, +} diff --git a/src/cmd/diff.go b/src/cmd/diff.go new file mode 100644 index 0000000..446d015 --- /dev/null +++ b/src/cmd/diff.go @@ -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"}) + }, +} diff --git a/src/cmd/pull.go b/src/cmd/pull.go index 3022aba..bdab7f8 100644 --- a/src/cmd/pull.go +++ b/src/cmd/pull.go @@ -1,10 +1,7 @@ package cmd import ( - "fmt" - "os/exec" - "os" - + "github.com/daveberrys/guh/src/utils" "github.com/spf13/cobra" ) @@ -13,18 +10,13 @@ func init() { } var pullCmd = &cobra.Command{ - Use: "pull [source]", - Short: "Pull updates from a source", - Args: cobra.MaximumNArgs(1), - Run: func(cmd *cobra.Command, args []string) { - - out, err := exec.Command("git", "fetch").CombinedOutput() - if err != nil { fmt.Fprintf(os.Stderr, "error: %s\n", err) } - - out, err = exec.Command("git", "pull").CombinedOutput() - if err != nil { fmt.Fprintf(os.Stderr, "error: %s\n", err) } - - fmt.Print(string(out)) -}, - + Use: "pull", + Short: "Fetch and pull updates", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + return utils.RunGitSequence( + []string{"fetch"}, + []string{"pull"}, + ) + }, } diff --git a/src/cmd/push.go b/src/cmd/push.go index 2e3cfab..d435191 100644 --- a/src/cmd/push.go +++ b/src/cmd/push.go @@ -1,10 +1,7 @@ package cmd import ( - "fmt" - "os" - "os/exec" - + "github.com/daveberrys/guh/src/utils" "github.com/spf13/cobra" ) @@ -13,16 +10,10 @@ func init() { } var pushCmd = &cobra.Command{ - Use: "push [destination]", - Short: "Push changes to a destination", - Args: cobra.MaximumNArgs(1), - Run: func(cmd *cobra.Command, args []string) { - dest := "main" - 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)) + Use: "push", + Short: "Add current changes and push", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + return utils.RunGitSequence([]string{"push"}) }, } diff --git a/src/cmd/save.go b/src/cmd/save.go deleted file mode 100644 index fdf80fe..0000000 --- a/src/cmd/save.go +++ /dev/null @@ -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) -} \ No newline at end of file diff --git a/src/cmd/stash.go b/src/cmd/stash.go new file mode 100644 index 0000000..f77370a --- /dev/null +++ b/src/cmd/stash.go @@ -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"}) + }, +} diff --git a/src/cmd/switch.go b/src/cmd/switch.go index 2378e7d..8770033 100644 --- a/src/cmd/switch.go +++ b/src/cmd/switch.go @@ -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) -} \ No newline at end of file +func performBranchSwitch(branchName string) error { + return utils.RunGitSequence([]string{"switch", branchName}) +} diff --git a/src/utils/git.go b/src/utils/git.go new file mode 100644 index 0000000..5766af7 --- /dev/null +++ b/src/utils/git.go @@ -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 +} diff --git a/src/utils/json.go b/src/utils/json.go index f948682..24cb7cf 100644 --- a/src/utils/json.go +++ b/src/utils/json.go @@ -1,7 +1,103 @@ package utils -import ("fmt") +import ( + "encoding/json" + "errors" + "fmt" + "os" + "path/filepath" +) -func init() { - fmt.Println("json read, save, delete logic goes here") -} \ No newline at end of file +const appDirName = "dev.pages.codedave.guh" +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) +}