mirror of
https://github.com/daveberrys/guh.git
synced 2026-11-29 00:46:45 +08:00
create linking repo, cleanupo code
This commit is contained in:
1 parent
e2cc0e1006
commit
2c476b8d2a
6 files changed
+56
-34
No files matched your search
@@ -1,6 +1,8 @@
|
|||||||
package createcmd
|
package createcmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/daveberrys/guh/src/utils"
|
"github.com/daveberrys/guh/src/utils"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@@ -10,6 +12,8 @@ var createBranchCmd = &cobra.Command{
|
|||||||
Short: "Create and switch to a branch",
|
Short: "Create and switch to a branch",
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
RunE: func(c *cobra.Command, args []string) error {
|
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
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -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
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ func init() {
|
|||||||
cmd.RootCmd.AddCommand(createCmd)
|
cmd.RootCmd.AddCommand(createCmd)
|
||||||
createCmd.AddCommand(createBranchCmd)
|
createCmd.AddCommand(createBranchCmd)
|
||||||
createCmd.AddCommand(createAccountCmd)
|
createCmd.AddCommand(createAccountCmd)
|
||||||
|
createCmd.AddCommand(linkRepoCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
var createCmd = &cobra.Command{
|
var createCmd = &cobra.Command{
|
||||||
|
|||||||
+19
-13
@@ -6,23 +6,29 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/daveberrys/guh/src/utils"
|
"github.com/daveberrys/guh/src/utils"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
func performAccountSwitch(username string) error {
|
var switchAccountCmd = &cobra.Command{
|
||||||
account, err := utils.FindAccount(username)
|
Use: "account [username]",
|
||||||
if err != nil { return err }
|
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,
|
if err := utils.RunGitSequence(false,
|
||||||
[]string{"config", "--global", "user.name", account.Username},
|
[]string{"config", "--global", "user.name", account.Username},
|
||||||
[]string{"config", "--global", "user.email", account.Email},
|
[]string{"config", "--global", "user.email", account.Email},
|
||||||
); err != nil { return err }
|
); err != nil { return err }
|
||||||
|
|
||||||
homeDir, err := os.UserHomeDir()
|
homeDir, err := os.UserHomeDir()
|
||||||
if err != nil { return err }
|
if err != nil { return err }
|
||||||
|
|
||||||
credentials := fmt.Sprintf("https://%s:%s@%s\n", account.Username, account.ClassicToken, account.Platform)
|
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 }
|
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)
|
fmt.Printf("Switched to account: %s\n", account.Username)
|
||||||
return nil
|
return nil
|
||||||
|
},
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,19 @@
|
|||||||
package switchcmd
|
package switchcmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/daveberrys/guh/src/utils"
|
"github.com/daveberrys/guh/src/utils"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
func performBranchSwitch(branchName string) error {
|
var switchBranchCmd = &cobra.Command{
|
||||||
return utils.RunGitSequence(false, []string{"switch", branchName})
|
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
|
||||||
|
},
|
||||||
}
|
}
|
||||||
@@ -15,21 +15,3 @@ var switchCmd = &cobra.Command{
|
|||||||
Use: "switch",
|
Use: "switch",
|
||||||
Short: "Switch branches or accounts",
|
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