organize file system, update docs

This commit is contained in:
daveberrys committed 2026-07-19 11:12:14 +08:00
1 parent 932823fdde
commit 7a71d0e89d
21 files changed
+293 -256

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
package account
import (
"github.com/daveberrys/guh/src/cmd"
"github.com/spf13/cobra"
)
func init() {
cmd.RootCmd.AddCommand(accountCmd)
accountCmd.AddCommand(saveCmd)
accountCmd.AddCommand(switchCmd)
}
var accountCmd = &cobra.Command{
Use: "account",
Short: "Manage accounts",
}
@@ -1,4 +1,4 @@
package createcmd
package account
import (
"fmt"
@@ -8,9 +8,9 @@ import (
"github.com/spf13/cobra"
)
var createAccountCmd = &cobra.Command{
Use: "account [username] [email] [classicToken] [platform]",
Short: "Create or update an account",
var saveCmd = &cobra.Command{
Use: "save [username] [email] [classicToken] [platform]",
Short: "Save a new account",
Args: cobra.ExactArgs(4),
RunE: func(c *cobra.Command, args []string) error {
account := utils.Account{
@@ -23,7 +23,9 @@ var createAccountCmd = &cobra.Command{
return fmt.Errorf("username, email, classicToken, platform are required")
}
if err := utils.UpsertAccount(account); err != nil { return err }
if err := utils.UpsertAccount(account); err != nil {
return err
}
fmt.Printf("Saved account: %s\n", account.Username)
return nil
@@ -1,4 +1,4 @@
package switchcmd
package account
import (
"fmt"
@@ -9,27 +9,35 @@ import (
"github.com/spf13/cobra"
)
var switchAccountCmd = &cobra.Command{
Use: "account [username]",
Short: "Switch to a specific user account",
var switchCmd = &cobra.Command{
Use: "switch [username]",
Short: "Switch to a saved 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 != nil {
return err
}
if err := utils.RunGitSequence(false,
[]string{"config", "--global", "credential.helper", "store"},
[]string{"config", "--global", "credential.helper", "store"},
[]string{"config", "--global", "user.name", account.Username},
[]string{"config", "--global", "user.email", account.Email},
); err != nil { return err }
); err != nil {
return err
}
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)
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)
return nil
},
}
}
@@ -1,15 +1,15 @@
package createcmd
package branch
import (
"fmt"
"fmt"
"github.com/daveberrys/guh/src/utils"
"github.com/spf13/cobra"
)
var createBranchCmd = &cobra.Command{
Use: "branch [branch]",
Short: "Create and switch to a branch",
var createCmd = &cobra.Command{
Use: "create [name]",
Short: "Create and switch to a new branch",
Args: cobra.ExactArgs(1),
RunE: func(c *cobra.Command, args []string) error {
utils.RunGitSequence(false, []string{"switch", "-c", args[0]})
+19
View File
@@ -0,0 +1,19 @@
package branch
import (
"fmt"
"github.com/daveberrys/guh/src/utils"
"github.com/spf13/cobra"
)
var renameCmd = &cobra.Command{
Use: "rename [name]",
Short: "Rename the current branch",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
utils.RunGitSequence(false, []string{"branch", "-m", args[0]})
fmt.Println("Successfully renamed branch to", args[0])
return nil
},
}
+18
View File
@@ -0,0 +1,18 @@
package branch
import (
"github.com/daveberrys/guh/src/cmd"
"github.com/spf13/cobra"
)
func init() {
cmd.RootCmd.AddCommand(branchCmd)
branchCmd.AddCommand(createCmd)
branchCmd.AddCommand(switchCmd)
branchCmd.AddCommand(renameCmd)
}
var branchCmd = &cobra.Command{
Use: "branch",
Short: "Manage branches",
}
@@ -1,4 +1,4 @@
package switchcmd
package branch
import (
"fmt"
@@ -7,18 +7,17 @@ import (
"github.com/spf13/cobra"
)
var switchBranchCmd = &cobra.Command{
Use: "branch [name]",
Short: "Switch to a specific branch",
var switchCmd = &cobra.Command{
Use: "switch [name]",
Short: "Switch to a branch, or list branches with 'list'",
Args: cobra.ExactArgs(1),
RunE: func(c *cobra.Command, args []string) error {
if args[0] == "list" {
if args[0] == "list" {
utils.RunGit(false, "branch")
} else {
} else {
utils.RunGitSequence(false, []string{"switch", args[0]})
fmt.Printf("Switched to branch: %s\n", args[0])
fmt.Printf("Switched to branch: %s\n", args[0])
}
return nil
return nil
},
}
}
+5 -13
View File
@@ -1,21 +1,22 @@
package cmd
package commit
import (
"encoding/json"
"fmt"
"strings"
"github.com/daveberrys/guh/src/cmd"
"github.com/daveberrys/guh/src/utils"
"github.com/spf13/cobra"
)
func init() { rootCmd.AddCommand(commitCmd) }
func init() { cmd.RootCmd.AddCommand(commitCmd) }
var commitCmd = &cobra.Command{
Use: "commit [files] [message] [description]",
Short: "Add specified files and commit",
Args: cobra.RangeArgs(0, 5),
RunE: func(cmd *cobra.Command, args []string) error {
Args: cobra.RangeArgs(0, 5),
RunE: func(cobraCmd *cobra.Command, args []string) error {
if len(args) == 0 {
return utils.RunGitSequence(false, []string{"status", "--short"})
}
@@ -46,13 +47,8 @@ var commitCmd = &cobra.Command{
commitArgs := []string{"commit", "-m", args[1]}
afterPush, remoteName := false, "origin"
// case 1 -> guh commit [files] [message] [desc] push[:optional] [remote_name:optional(origin)]
// case 2 -> guh commit [files] [message] push[:optional] [remote_name:optional(origin)]
// case 3 -> guh commit [files] [message] [desc]
// case 4 -> guh commit [files] [message]
switch len(args) {
case 5:
// [files] [message] [desc] push [remote] (case 1)
commitArgs = append(commitArgs, "-m", args[2])
afterPush = true
if args[4] != "" {
@@ -60,22 +56,18 @@ var commitCmd = &cobra.Command{
}
case 4:
if args[2] == "push" {
// [files] [message] push [remote] (case 2)
afterPush = true
if args[3] != "" {
remoteName = args[3]
}
} else {
// [files] [message] [desc] push (case 1)
commitArgs = append(commitArgs, "-m", args[2])
afterPush = true
}
case 3:
if args[2] == "push" {
// [files] [message] push (case 2)
afterPush = true
} else {
// [files] [message] [desc] (case 3)
commitArgs = append(commitArgs, "-m", args[2])
}
}
-17
View File
@@ -1,17 +0,0 @@
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",
}
+7 -4
View File
@@ -1,18 +1,21 @@
package cmd
package diff
import (
"github.com/daveberrys/guh/src/cmd"
"github.com/daveberrys/guh/src/utils"
"github.com/spf13/cobra"
)
func init() { rootCmd.AddCommand(diffCmd) }
func init() { cmd.RootCmd.AddCommand(diffCmd) }
var diffCmd = &cobra.Command{
Use: "diff [file:optional]",
Short: "Show working tree changes",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 1 { return utils.RunGitSequence(false, []string{"diff", args[0]}) }
RunE: func(cobraCmd *cobra.Command, args []string) error {
if len(args) == 1 {
return utils.RunGitSequence(false, []string{"diff", args[0]})
}
return utils.RunGitSequence(false, []string{"diff"})
},
}
+4 -3
View File
@@ -1,17 +1,18 @@
package cmd
package initcmd
import (
"github.com/daveberrys/guh/src/cmd"
"github.com/daveberrys/guh/src/utils"
"github.com/spf13/cobra"
)
func init() { rootCmd.AddCommand(initCmd) }
func init() { cmd.RootCmd.AddCommand(initCmd) }
var initCmd = &cobra.Command{
Use: "init",
Short: "Initialize a new repository",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cobraCmd *cobra.Command, args []string) error {
return utils.RunGitSequence(false, []string{"init"})
},
}
-20
View File
@@ -1,20 +0,0 @@
package cmd
import (
"github.com/daveberrys/guh/src/utils"
"github.com/spf13/cobra"
)
func init() { rootCmd.AddCommand(logCmd) }
var logCmd = &cobra.Command{
Use: "logs [commits]",
Short: "Show commits logs.",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return utils.RunGitSequence(false, []string{"log"})
}
return utils.RunGitSequence(false, []string{"log", "-"+args[0]})
},
}
+21
View File
@@ -0,0 +1,21 @@
package logs
import (
"github.com/daveberrys/guh/src/cmd"
"github.com/daveberrys/guh/src/utils"
"github.com/spf13/cobra"
)
func init() { cmd.RootCmd.AddCommand(logCmd) }
var logCmd = &cobra.Command{
Use: "logs [commits]",
Short: "Show commits logs.",
Args: cobra.MaximumNArgs(1),
RunE: func(cobraCmd *cobra.Command, args []string) error {
if len(args) == 0 {
return utils.RunGitSequence(false, []string{"log"})
}
return utils.RunGitSequence(false, []string{"log", "-" + args[0]})
},
}
+4 -3
View File
@@ -1,17 +1,18 @@
package cmd
package pull
import (
"github.com/daveberrys/guh/src/cmd"
"github.com/daveberrys/guh/src/utils"
"github.com/spf13/cobra"
)
func init() { rootCmd.AddCommand(pullCmd) }
func init() { cmd.RootCmd.AddCommand(pullCmd) }
var pullCmd = &cobra.Command{
Use: "pull",
Short: "Fetch and pull updates",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cobraCmd *cobra.Command, args []string) error {
return utils.RunGitSequence(false,
[]string{"fetch"},
[]string{"pull"},
+4 -3
View File
@@ -1,19 +1,20 @@
package cmd
package push
import (
"strings"
"github.com/daveberrys/guh/src/cmd"
"github.com/daveberrys/guh/src/utils"
"github.com/spf13/cobra"
)
func init() { rootCmd.AddCommand(pushCmd) }
func init() { cmd.RootCmd.AddCommand(pushCmd) }
var pushCmd = &cobra.Command{
Use: "push [remote]",
Short: "Add current changes and push",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cobraCmd *cobra.Command, args []string) error {
branch, _ := utils.RunGit(true, "rev-parse", "--abbrev-ref", "HEAD")
if len(args) == 0 {
-33
View File
@@ -1,33 +0,0 @@
package cmd
import (
"fmt"
"github.com/daveberrys/guh/src/utils"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(renameCmd)
renameCmd.AddCommand(renameBranchCmd)
}
var renameCmd = &cobra.Command{
Use: "rename",
Short: "Rename branches",
}
var renameBranchCmd = &cobra.Command{
Use: "branch [name]",
Short: "Rename a specific branch",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return renameBranch(args[0])
},
}
func renameBranch(branchName string) error {
utils.RunGitSequence(false, []string{"branch", "-m", branchName})
fmt.Println("Successfully renamed branch to", branchName)
return nil
}
+4 -3
View File
@@ -1,17 +1,18 @@
package cmd
package stash
import (
"github.com/daveberrys/guh/src/cmd"
"github.com/daveberrys/guh/src/utils"
"github.com/spf13/cobra"
)
func init() { rootCmd.AddCommand(stashCmd) }
func init() { cmd.RootCmd.AddCommand(stashCmd) }
var stashCmd = &cobra.Command{
Use: "stash",
Short: "Stash current changes",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cobraCmd *cobra.Command, args []string) error {
return utils.RunGitSequence(false, []string{"stash"})
},
}
-17
View File
@@ -1,17 +0,0 @@
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",
}
+12 -10
View File
@@ -1,24 +1,26 @@
package cmd
package undo
import (
"github.com/spf13/cobra"
"github.com/daveberrys/guh/src/cmd"
"github.com/daveberrys/guh/src/utils"
"github.com/spf13/cobra"
)
func init() { RootCmd.AddCommand(undoCmd) }
func init() { cmd.RootCmd.AddCommand(undoCmd) }
var undoCmd = &cobra.Command{
Use: "undo [commits] [flavour]",
Short: "Undo the last LOCAL commit. Flavour can be hard, mixed, or soft. It's optional.",
Args: cobra.MinimumNArgs(1),
Args: cobra.MinimumNArgs(1),
RunE: func(c *cobra.Command, args []string) error {
var flavour string
switch args[1] {
case "hard":
flavour = "--hard"
case "mixed":
flavour = "--mixed"
default:
flavour = "--soft"
case "hard":
flavour = "--hard"
case "mixed":
flavour = "--mixed"
default:
flavour = "--soft"
}
utils.RunGitSequence(false, []string{"reset", flavour, "HEAD~" + args[0]})