From 2c476b8d2a7521b3d1e52dad25fe0ec0cba206c8 Mon Sep 17 00:00:00 2001 From: daveberrys <84567826+daveberrys@users.noreply.github.com> Date: Wed, 15 Jul 2026 22:00:30 +0800 Subject: [PATCH] create linking repo, cleanupo code --- src/cmd/create/branch.go | 6 +++++- src/cmd/create/repo.go | 19 ++++++++++++++++++ src/cmd/create/root.go | 1 + src/cmd/switch/account.go | 42 ++++++++++++++++++++++----------------- src/cmd/switch/branch.go | 16 ++++++++++++--- src/cmd/switch/root.go | 20 +------------------ 6 files changed, 63 insertions(+), 41 deletions(-) create mode 100644 src/cmd/create/repo.go diff --git a/src/cmd/create/branch.go b/src/cmd/create/branch.go index 4587dde..b7a2a07 100644 --- a/src/cmd/create/branch.go +++ b/src/cmd/create/branch.go @@ -1,6 +1,8 @@ package createcmd import ( + "fmt" + "github.com/daveberrys/guh/src/utils" "github.com/spf13/cobra" ) @@ -10,6 +12,8 @@ var createBranchCmd = &cobra.Command{ Short: "Create and switch to a branch", Args: cobra.ExactArgs(1), RunE: func(c *cobra.Command, args []string) error { - return utils.RunGitSequence(false, []string{"switch", "-c", args[0]}) + utils.RunGitSequence(false, []string{"switch", "-c", args[0]}) + fmt.Printf("Created branch to: %s\n", args[0]) + return nil }, } diff --git a/src/cmd/create/repo.go b/src/cmd/create/repo.go new file mode 100644 index 0000000..8343cfa --- /dev/null +++ b/src/cmd/create/repo.go @@ -0,0 +1,19 @@ +package createcmd + +import ( + "fmt" + + "github.com/daveberrys/guh/src/utils" + "github.com/spf13/cobra" +) + +var linkRepoCmd = &cobra.Command{ + Use: "repo [url]", + Short: "Link a repository", + Args: cobra.ExactArgs(1), + RunE: func(c *cobra.Command, args []string) error { + utils.RunGitSequence(false, []string{"remote", "set-url", "origin", args[0]}) + fmt.Printf("Linked repository to: %s\n", args[0]) + return nil + }, +} \ No newline at end of file diff --git a/src/cmd/create/root.go b/src/cmd/create/root.go index d26f98d..f85233f 100644 --- a/src/cmd/create/root.go +++ b/src/cmd/create/root.go @@ -9,6 +9,7 @@ func init() { cmd.RootCmd.AddCommand(createCmd) createCmd.AddCommand(createBranchCmd) createCmd.AddCommand(createAccountCmd) + createCmd.AddCommand(linkRepoCmd) } var createCmd = &cobra.Command{ diff --git a/src/cmd/switch/account.go b/src/cmd/switch/account.go index 7372a5e..2cf2a5a 100644 --- a/src/cmd/switch/account.go +++ b/src/cmd/switch/account.go @@ -6,23 +6,29 @@ import ( "path/filepath" "github.com/daveberrys/guh/src/utils" + "github.com/spf13/cobra" ) -func performAccountSwitch(username string) error { - account, err := utils.FindAccount(username) - 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 -} +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 + }, +} \ No newline at end of file diff --git a/src/cmd/switch/branch.go b/src/cmd/switch/branch.go index 4530312..fd3eefe 100644 --- a/src/cmd/switch/branch.go +++ b/src/cmd/switch/branch.go @@ -1,9 +1,19 @@ package switchcmd import ( + "fmt" + "github.com/daveberrys/guh/src/utils" + "github.com/spf13/cobra" ) -func performBranchSwitch(branchName string) error { - return utils.RunGitSequence(false, []string{"switch", branchName}) -} +var switchBranchCmd = &cobra.Command{ + Use: "branch [name]", + Short: "Switch to a specific branch", + Args: cobra.ExactArgs(1), + RunE: func(c *cobra.Command, args []string) error { + utils.RunGitequence(false, []string{"switch", args[0]}) + fmt.Printf("Switched to branch: %s\n", args[0]) + return nil + }, +} \ No newline at end of file diff --git a/src/cmd/switch/root.go b/src/cmd/switch/root.go index 211f806..e76b82a 100644 --- a/src/cmd/switch/root.go +++ b/src/cmd/switch/root.go @@ -14,22 +14,4 @@ func init() { var switchCmd = &cobra.Command{ Use: "switch", Short: "Switch branches or accounts", -} - -var switchBranchCmd = &cobra.Command{ - Use: "branch [name]", - Short: "Switch to a specific branch", - Args: cobra.ExactArgs(1), - RunE: func(c *cobra.Command, args []string) error { - return performBranchSwitch(args[0]) - }, -} - -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 { - return performAccountSwitch(args[0]) - }, -} +} \ No newline at end of file