organized files

This commit is contained in:
Daveberry committed 2026-06-12 19:05:41 +08:00
1 parent d5f6dc6fa0
commit 2e8eceb1e2
9 files changed
+116 -86

No files matched your search

@@ -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],
+15
View File
@@ -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]})
},
}
+17
View File
@@ -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",
}
+2
View File
@@ -16,6 +16,8 @@ var rootCmd = &cobra.Command{
},
}
var RootCmd = rootCmd
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
-62
View File
@@ -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})
}
+28
View File
@@ -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
}
+9
View File
@@ -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})
}
+35
View File
@@ -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])
},
}