From 2e8eceb1e2f81e0086d028d474f2e9c519aa760e Mon Sep 17 00:00:00 2001 From: Daveberry <84567826+daveberrys@users.noreply.github.com> Date: Fri, 12 Jun 2026 19:05:41 +0800 Subject: [PATCH] organized files --- main.go | 10 +++- src/cmd/{create.go => create/account.go} | 24 +-------- src/cmd/create/branch.go | 15 ++++++ src/cmd/create/root.go | 17 +++++++ src/cmd/root.go | 2 + src/cmd/switch.go | 62 ------------------------ src/cmd/switch/account.go | 28 +++++++++++ src/cmd/switch/branch.go | 9 ++++ src/cmd/switch/root.go | 35 +++++++++++++ 9 files changed, 116 insertions(+), 86 deletions(-) rename src/cmd/{create.go => create/account.go} (56%) create mode 100644 src/cmd/create/branch.go create mode 100644 src/cmd/create/root.go delete mode 100644 src/cmd/switch.go create mode 100644 src/cmd/switch/account.go create mode 100644 src/cmd/switch/branch.go create mode 100644 src/cmd/switch/root.go diff --git a/main.go b/main.go index f44a838..40f19ce 100644 --- a/main.go +++ b/main.go @@ -1,3 +1,9 @@ package main -import "github.com/daveberrys/guh/src/cmd" -func main() { cmd.Execute() } \ No newline at end of file + +import ( + "github.com/daveberrys/guh/src/cmd" + _ "github.com/daveberrys/guh/src/cmd/create" + _ "github.com/daveberrys/guh/src/cmd/switch" +) + +func main() { cmd.Execute() } diff --git a/src/cmd/create.go b/src/cmd/create/account.go similarity index 56% rename from src/cmd/create.go rename to src/cmd/create/account.go index f11ca1e..ec43284 100644 --- a/src/cmd/create.go +++ b/src/cmd/create/account.go @@ -1,4 +1,4 @@ -package cmd +package createcmd import ( "fmt" @@ -8,31 +8,11 @@ import ( "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(false, []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 { + RunE: func(c *cobra.Command, args []string) error { account := utils.Account{ Username: args[0], Email: args[1], diff --git a/src/cmd/create/branch.go b/src/cmd/create/branch.go new file mode 100644 index 0000000..4587dde --- /dev/null +++ b/src/cmd/create/branch.go @@ -0,0 +1,15 @@ +package createcmd + +import ( + "github.com/daveberrys/guh/src/utils" + "github.com/spf13/cobra" +) + +var createBranchCmd = &cobra.Command{ + Use: "branch [branch]", + 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]}) + }, +} diff --git a/src/cmd/create/root.go b/src/cmd/create/root.go new file mode 100644 index 0000000..d26f98d --- /dev/null +++ b/src/cmd/create/root.go @@ -0,0 +1,17 @@ +package createcmd + +import ( + "github.com/daveberrys/guh/src/cmd" + "github.com/spf13/cobra" +) + +func init() { + cmd.RootCmd.AddCommand(createCmd) + createCmd.AddCommand(createBranchCmd) + createCmd.AddCommand(createAccountCmd) +} + +var createCmd = &cobra.Command{ + Use: "create", + Short: "Create branches or accounts", +} diff --git a/src/cmd/root.go b/src/cmd/root.go index a9a1473..0ea7529 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -16,6 +16,8 @@ var rootCmd = &cobra.Command{ }, } +var RootCmd = rootCmd + func Execute() { if err := rootCmd.Execute(); err != nil { fmt.Fprintln(os.Stderr, err) diff --git a/src/cmd/switch.go b/src/cmd/switch.go deleted file mode 100644 index 7100d3f..0000000 --- a/src/cmd/switch.go +++ /dev/null @@ -1,62 +0,0 @@ -package cmd - -import ( - "fmt" - "os" - "path/filepath" - - "github.com/daveberrys/guh/src/utils" - "github.com/spf13/cobra" -) - -func init() { - rootCmd.AddCommand(switchCmd) - switchCmd.AddCommand(switchBranchCmd) - switchCmd.AddCommand(switchAccountCmd) -} - -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(cmd *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(cmd *cobra.Command, args []string) error { - return performAccountSwitch(args[0]) - }, -} - -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@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(false, []string{"switch", branchName}) -} diff --git a/src/cmd/switch/account.go b/src/cmd/switch/account.go new file mode 100644 index 0000000..7fbb0a1 --- /dev/null +++ b/src/cmd/switch/account.go @@ -0,0 +1,28 @@ +package switchcmd + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/daveberrys/guh/src/utils" +) + +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@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 +} diff --git a/src/cmd/switch/branch.go b/src/cmd/switch/branch.go new file mode 100644 index 0000000..4530312 --- /dev/null +++ b/src/cmd/switch/branch.go @@ -0,0 +1,9 @@ +package switchcmd + +import ( + "github.com/daveberrys/guh/src/utils" +) + +func performBranchSwitch(branchName string) error { + return utils.RunGitSequence(false, []string{"switch", branchName}) +} diff --git a/src/cmd/switch/root.go b/src/cmd/switch/root.go new file mode 100644 index 0000000..211f806 --- /dev/null +++ b/src/cmd/switch/root.go @@ -0,0 +1,35 @@ +package switchcmd + +import ( + "github.com/daveberrys/guh/src/cmd" + "github.com/spf13/cobra" +) + +func init() { + cmd.RootCmd.AddCommand(switchCmd) + switchCmd.AddCommand(switchBranchCmd) + switchCmd.AddCommand(switchAccountCmd) +} + +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]) + }, +}