mirror of
https://github.com/daveberrys/guh.git
synced 2026-11-29 00:26:46 +08:00
implement everything stable, account switching feature
This commit is contained in:
1 parent
3e87fa0d53
commit
f2508d0bc8
11 files changed
+292
-79
No files matched your search
@@ -0,0 +1,2 @@
|
||||
guh
|
||||
guh.exe
|
||||
@@ -0,0 +1,21 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/daveberrys/guh/src/utils"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(commitCmd)
|
||||
}
|
||||
|
||||
var commitCmd = &cobra.Command{
|
||||
Use: "commit",
|
||||
Short: "Add current changes and commit",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
utils.RunGitSequence([]string{"commit", "-m", args[0]})
|
||||
utils.RunGitSequence([]string{"add", "."})
|
||||
return nil
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/daveberrys/guh/src/utils"
|
||||
"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([]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 {
|
||||
account := utils.Account{
|
||||
Username: args[0],
|
||||
Email: args[1],
|
||||
ClassicToken: args[2],
|
||||
}
|
||||
if strings.TrimSpace(account.Username) == "" || strings.TrimSpace(account.Email) == "" || strings.TrimSpace(account.ClassicToken) == "" {
|
||||
return fmt.Errorf("username, email, and classicToken are required")
|
||||
}
|
||||
|
||||
if err := utils.UpsertAccount(account); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("Saved account: %s\n", account.Username)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/daveberrys/guh/src/utils"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(diffCmd)
|
||||
}
|
||||
|
||||
var diffCmd = &cobra.Command{
|
||||
Use: "diff",
|
||||
Short: "Show working tree changes",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return utils.RunGitSequence([]string{"diff"})
|
||||
},
|
||||
}
|
||||
+10
-18
@@ -1,10 +1,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"os"
|
||||
|
||||
"github.com/daveberrys/guh/src/utils"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -13,18 +10,13 @@ func init() {
|
||||
}
|
||||
|
||||
var pullCmd = &cobra.Command{
|
||||
Use: "pull [source]",
|
||||
Short: "Pull updates from a source",
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
out, err := exec.Command("git", "fetch").CombinedOutput()
|
||||
if err != nil { fmt.Fprintf(os.Stderr, "error: %s\n", err) }
|
||||
|
||||
out, err = exec.Command("git", "pull").CombinedOutput()
|
||||
if err != nil { fmt.Fprintf(os.Stderr, "error: %s\n", err) }
|
||||
|
||||
fmt.Print(string(out))
|
||||
},
|
||||
|
||||
Use: "pull",
|
||||
Short: "Fetch and pull updates",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return utils.RunGitSequence(
|
||||
[]string{"fetch"},
|
||||
[]string{"pull"},
|
||||
)
|
||||
},
|
||||
}
|
||||
+6
-15
@@ -1,10 +1,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
"github.com/daveberrys/guh/src/utils"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -13,16 +10,10 @@ func init() {
|
||||
}
|
||||
|
||||
var pushCmd = &cobra.Command{
|
||||
Use: "push [destination]",
|
||||
Short: "Push changes to a destination",
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
dest := "main"
|
||||
if len(args) > 0 { dest = args[0] }
|
||||
fmt.Printf("Pushing changes to: %s...\n", dest)
|
||||
|
||||
out, err := exec.Command("git", "push", "origin", dest).CombinedOutput()
|
||||
if err != nil { fmt.Fprintf(os.Stderr, "error: %s\n", err) }
|
||||
fmt.Print(string(out))
|
||||
Use: "push",
|
||||
Short: "Add current changes and push",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return utils.RunGitSequence([]string{"push"})
|
||||
},
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// COMMAND LINE INTERFACE GOES HERE
|
||||
func init() {
|
||||
rootCmd.AddCommand(saveCmd)
|
||||
saveCmd.AddCommand(accountSaveCmd)
|
||||
}
|
||||
|
||||
var saveCmd = &cobra.Command{
|
||||
Use: "save",
|
||||
Short: "Save accounts,",
|
||||
}
|
||||
|
||||
var accountSaveCmd = &cobra.Command{
|
||||
Use: "account [username]",
|
||||
Short: "Save a specific user account",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
performAccountSave(args[0])
|
||||
},
|
||||
}
|
||||
|
||||
// PERFORMERS GO HERE
|
||||
func performAccountSave(username string) {
|
||||
fmt.Printf("Saved account: %s\n", username)
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/daveberrys/guh/src/utils"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(stashCmd)
|
||||
}
|
||||
|
||||
var stashCmd = &cobra.Command{
|
||||
Use: "stash",
|
||||
Short: "Stash current changes",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return utils.RunGitSequence([]string{"stash"})
|
||||
},
|
||||
}
|
||||
+36
-11
@@ -2,10 +2,13 @@ package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/daveberrys/guh/src/utils"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// COMMAND LINE INTERFACE GOES HERE
|
||||
func init() {
|
||||
rootCmd.AddCommand(switchCmd)
|
||||
switchCmd.AddCommand(switchBranchCmd)
|
||||
@@ -21,8 +24,8 @@ var switchBranchCmd = &cobra.Command{
|
||||
Use: "branch [name]",
|
||||
Short: "Switch to a specific branch",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
performBranchSwitch(args[0])
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return performBranchSwitch(args[0])
|
||||
},
|
||||
}
|
||||
|
||||
@@ -30,16 +33,38 @@ var switchAccountCmd = &cobra.Command{
|
||||
Use: "account [username]",
|
||||
Short: "Switch to a specific user account",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
performAccountSwitch(args[0])
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return performAccountSwitch(args[0])
|
||||
},
|
||||
}
|
||||
|
||||
// PERFORMERS GO HERE
|
||||
func performAccountSwitch(username string) {
|
||||
fmt.Printf("Switched to account: %s\n", username)
|
||||
func performAccountSwitch(username string) error {
|
||||
account, err := utils.FindAccount(username)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := utils.RunGitSequence(
|
||||
[]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) {
|
||||
fmt.Printf("Switched to branch: %s\n", branchName)
|
||||
}
|
||||
func performBranchSwitch(branchName string) error {
|
||||
return utils.RunGitSequence([]string{"switch", branchName})
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func RunGit(args ...string) error {
|
||||
gitCmd := exec.Command("git", args...)
|
||||
gitCmd.Stdin = os.Stdin
|
||||
gitCmd.Stdout = os.Stdout
|
||||
gitCmd.Stderr = os.Stderr
|
||||
|
||||
return gitCmd.Run()
|
||||
}
|
||||
|
||||
func RunGitSequence(commands ...[]string) error {
|
||||
for _, commandArgs := range commands {
|
||||
if err := RunGit(commandArgs...); err != nil {
|
||||
return fmt.Errorf("git %s failed: %w", strings.Join(commandArgs, " "), err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
+100
-4
@@ -1,7 +1,103 @@
|
||||
package utils
|
||||
|
||||
import ("fmt")
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func init() {
|
||||
fmt.Println("json read, save, delete logic goes here")
|
||||
}
|
||||
const appDirName = "dev.pages.codedave.guh"
|
||||
const accountsFileName = "accounts.json"
|
||||
|
||||
type Account struct {
|
||||
Username string `json:"username"`
|
||||
Email string `json:"email"`
|
||||
ClassicToken string `json:"classic_token"`
|
||||
}
|
||||
|
||||
func AccountsFilePath() (string, error) {
|
||||
configDir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return filepath.Join(configDir, appDirName, accountsFileName), nil
|
||||
}
|
||||
|
||||
func LoadAccounts() ([]Account, error) {
|
||||
accountsFilePath, err := AccountsFilePath()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
contents, err := os.ReadFile(accountsFilePath)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return []Account{}, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(contents) == 0 {
|
||||
return []Account{}, nil
|
||||
}
|
||||
|
||||
var accounts []Account
|
||||
if err := json.Unmarshal(contents, &accounts); err != nil {
|
||||
return nil, fmt.Errorf("read accounts file: %w", err)
|
||||
}
|
||||
|
||||
return accounts, nil
|
||||
}
|
||||
|
||||
func SaveAccounts(accounts []Account) error {
|
||||
accountsFilePath, err := AccountsFilePath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(accountsFilePath), 0700); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
contents, err := json.MarshalIndent(accounts, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
contents = append(contents, '\n')
|
||||
|
||||
return os.WriteFile(accountsFilePath, contents, 0600)
|
||||
}
|
||||
|
||||
func FindAccount(username string) (Account, error) {
|
||||
accounts, err := LoadAccounts()
|
||||
if err != nil {
|
||||
return Account{}, err
|
||||
}
|
||||
|
||||
for _, account := range accounts {
|
||||
if account.Username == username {
|
||||
return account, nil
|
||||
}
|
||||
}
|
||||
|
||||
return Account{}, fmt.Errorf("account %q not found", username)
|
||||
}
|
||||
|
||||
func UpsertAccount(newAccount Account) error {
|
||||
accounts, err := LoadAccounts()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for index, account := range accounts {
|
||||
if account.Username == newAccount.Username {
|
||||
accounts[index] = newAccount
|
||||
return SaveAccounts(accounts)
|
||||
}
|
||||
}
|
||||
|
||||
accounts = append(accounts, newAccount)
|
||||
return SaveAccounts(accounts)
|
||||
}
|
||||
Reference in new issue
Block a user