mirror of
https://github.com/daveberrys/guh.git
synced 2026-11-29 00:26:46 +08:00
organized files
This commit is contained in:
1 parent
d5f6dc6fa0
commit
2e8eceb1e2
9 files changed
+116
-86
No files matched your search
@@ -1,3 +1,9 @@
|
||||
package main
|
||||
import "github.com/daveberrys/guh/src/cmd"
|
||||
func main() { cmd.Execute() }
|
||||
|
||||
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() }
|
||||
@@ -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],
|
||||
@@ -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]})
|
||||
},
|
||||
}
|
||||
@@ -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",
|
||||
}
|
||||
@@ -16,6 +16,8 @@ var rootCmd = &cobra.Command{
|
||||
},
|
||||
}
|
||||
|
||||
var RootCmd = rootCmd
|
||||
|
||||
func Execute() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
|
||||
@@ -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})
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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})
|
||||
}
|
||||
@@ -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])
|
||||
},
|
||||
}
|
||||
Reference in new issue
Block a user