commit b7ea1677868a8d197a125ab2827501747c1fe684 Author: Spencer Date: Sun May 17 14:11:34 2026 -0400 Initial commit: gtea CLI tool Go CLI that wraps the Gitea API and git to create repos, commit, push, and pull without leaving the terminal. Co-Authored-By: Claude Sonnet 4.6 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..88fcb7c --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +# Binary +gtea.exe +*.exe + +# Go build artifacts +*.o +*.a + +# Go module cache +vendor/ + +# IDE +.vscode/ +.idea/ + +# Claude Code local settings +.claude/settings.local.json diff --git a/cmd/commit.go b/cmd/commit.go new file mode 100644 index 0000000..64895a5 --- /dev/null +++ b/cmd/commit.go @@ -0,0 +1,28 @@ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +var commitMsg string + +var commitCmd = &cobra.Command{ + Use: "commit", + Short: "Stage all changes and commit", + RunE: func(cmd *cobra.Command, args []string) error { + if commitMsg == "" { + return fmt.Errorf("commit message required — use -m \"your message\"") + } + if err := runGit(".", "add", "-A"); err != nil { + return err + } + return runGit(".", "commit", "-m", commitMsg) + }, +} + +func init() { + commitCmd.Flags().StringVarP(&commitMsg, "message", "m", "", "Commit message") + _ = commitCmd.MarkFlagRequired("message") +} diff --git a/cmd/config.go b/cmd/config.go new file mode 100644 index 0000000..ef2fa57 --- /dev/null +++ b/cmd/config.go @@ -0,0 +1,50 @@ +package cmd + +import ( + "bufio" + "fmt" + "os" + "strings" + + "github.com/spf13/cobra" +) + +var configCmd = &cobra.Command{ + Use: "config", + Short: "Set your Gitea URL, username, and API token", + RunE: func(cmd *cobra.Command, args []string) error { + reader := bufio.NewReader(os.Stdin) + + cfg := &Config{} + if existing, err := loadConfig(); err == nil { + *cfg = *existing + } + + cfg.URL = prompt(reader, fmt.Sprintf("Gitea URL [%s]", cfg.URL), cfg.URL) + cfg.Username = prompt(reader, fmt.Sprintf("Username [%s]", cfg.Username), cfg.Username) + cfg.Token = prompt(reader, fmt.Sprintf("API Token [%s]", maskToken(cfg.Token)), cfg.Token) + + if err := saveConfig(cfg); err != nil { + return fmt.Errorf("saving config: %w", err) + } + fmt.Println("Config saved to", configPath()) + return nil + }, +} + +func prompt(reader *bufio.Reader, label, fallback string) string { + fmt.Printf("%s: ", label) + input, _ := reader.ReadString('\n') + input = strings.TrimSpace(input) + if input == "" { + return fallback + } + return input +} + +func maskToken(t string) string { + if len(t) < 4 { + return "****" + } + return t[:4] + "****" +} diff --git a/cmd/init.go b/cmd/init.go new file mode 100644 index 0000000..6131afc --- /dev/null +++ b/cmd/init.go @@ -0,0 +1,91 @@ +package cmd + +import ( + "fmt" + "os" + "path/filepath" + + gogitea "code.gitea.io/sdk/gitea" + "github.com/spf13/cobra" +) + +var ( + repoPrivate bool + repoDesc string +) + +var initCmd = &cobra.Command{ + Use: "init ", + Short: "Create a local project folder and a new Gitea repo", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + name := args[0] + + cfg, err := loadConfig() + if err != nil { + return err + } + + // 1. Create local folder + if err := os.MkdirAll(name, 0755); err != nil { + return fmt.Errorf("creating folder: %w", err) + } + fmt.Println("Created folder:", name) + + // 2. git init with main as the default branch + if err := runGit(name, "init", "-b", "main"); err != nil { + // Older git (<2.28) doesn't support -b; fall back + if err2 := runGit(name, "init"); err2 != nil { + return err2 + } + } + + // 3. Create repo on Gitea via API + client, err := gogitea.NewClient(cfg.URL, gogitea.SetToken(cfg.Token)) + if err != nil { + return fmt.Errorf("connecting to Gitea: %w", err) + } + + repo, _, err := client.CreateRepo(gogitea.CreateRepoOption{ + Name: name, + Description: repoDesc, + Private: repoPrivate, + AutoInit: false, + }) + if err != nil { + return fmt.Errorf("creating Gitea repo: %w", err) + } + fmt.Println("Created Gitea repo:", repo.HTMLURL) + + // 4. Add authenticated remote + remote := authCloneURL(cfg, repo.CloneURL) + if err := runGit(name, "remote", "add", "origin", remote); err != nil { + return err + } + + // 5. Initial commit + readmePath := filepath.Join(name, "README.md") + if err := os.WriteFile(readmePath, []byte("# "+name+"\n"), 0644); err != nil { + return fmt.Errorf("writing README: %w", err) + } + if err := runGit(name, "add", "."); err != nil { + return err + } + if err := runGit(name, "commit", "-m", "Initial commit"); err != nil { + return err + } + + // 6. Push + if err := runGit(name, "push", "-u", "origin", "main"); err != nil { + return err + } + + fmt.Printf("\nDone! cd %s — your repo is live at %s\n", name, repo.HTMLURL) + return nil + }, +} + +func init() { + initCmd.Flags().BoolVarP(&repoPrivate, "private", "p", false, "Make the repo private") + initCmd.Flags().StringVarP(&repoDesc, "desc", "d", "", "Repo description") +} diff --git a/cmd/pull.go b/cmd/pull.go new file mode 100644 index 0000000..a199094 --- /dev/null +++ b/cmd/pull.go @@ -0,0 +1,17 @@ +package cmd + +import ( + "github.com/spf13/cobra" +) + +var pullCmd = &cobra.Command{ + Use: "pull", + Short: "Pull latest changes from Gitea", + RunE: func(cmd *cobra.Command, args []string) error { + branch, err := currentBranch() + if err != nil { + branch = "main" + } + return runGit(".", "pull", "origin", branch) + }, +} diff --git a/cmd/push.go b/cmd/push.go new file mode 100644 index 0000000..8d4c024 --- /dev/null +++ b/cmd/push.go @@ -0,0 +1,17 @@ +package cmd + +import ( + "github.com/spf13/cobra" +) + +var pushCmd = &cobra.Command{ + Use: "push", + Short: "Push current branch to Gitea", + RunE: func(cmd *cobra.Command, args []string) error { + branch, err := currentBranch() + if err != nil { + branch = "main" + } + return runGit(".", "push", "-u", "origin", branch) + }, +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..b64c0b3 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,103 @@ +package cmd + +import ( + "bytes" + "encoding/json" + "fmt" + "os" + "os/exec" + "path/filepath" + "strings" + + "github.com/spf13/cobra" +) + +// Config holds Gitea connection details stored in ~/.gtea.json +type Config struct { + URL string `json:"url"` + Token string `json:"token"` + Username string `json:"username"` +} + +var rootCmd = &cobra.Command{ + Use: "gtea", + Short: "Gitea project and repo manager", + Long: "gtea — create Gitea repos, commit, push, and pull without leaving the terminal.", +} + +func Execute() { + if err := rootCmd.Execute(); err != nil { + os.Exit(1) + } +} + +func init() { + rootCmd.AddCommand(configCmd) + rootCmd.AddCommand(initCmd) + rootCmd.AddCommand(commitCmd) + rootCmd.AddCommand(pushCmd) + rootCmd.AddCommand(pullCmd) + rootCmd.AddCommand(statusCmd) +} + +func configPath() string { + home, _ := os.UserHomeDir() + return filepath.Join(home, ".gtea.json") +} + +func loadConfig() (*Config, error) { + data, err := os.ReadFile(configPath()) + if err != nil { + return nil, fmt.Errorf("no config found — run 'gtea config' first") + } + var cfg Config + if err := json.Unmarshal(data, &cfg); err != nil { + return nil, fmt.Errorf("invalid config: %w", err) + } + if cfg.URL == "" || cfg.Token == "" || cfg.Username == "" { + return nil, fmt.Errorf("incomplete config — run 'gtea config' to fill in missing fields") + } + return &cfg, nil +} + +func saveConfig(cfg *Config) error { + data, err := json.MarshalIndent(cfg, "", " ") + if err != nil { + return err + } + return os.WriteFile(configPath(), data, 0600) +} + +// runGit runs a git command in dir (use "." for current directory). +func runGit(dir string, args ...string) error { + c := exec.Command("git", args...) + c.Dir = dir + c.Stdout = os.Stdout + c.Stderr = os.Stderr + if err := c.Run(); err != nil { + return fmt.Errorf("git %s: %w", strings.Join(args, " "), err) + } + return nil +} + +// currentBranch returns the name of the currently checked-out branch. +func currentBranch() (string, error) { + out, err := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD").Output() + if err != nil { + return "", err + } + return string(bytes.TrimSpace(out)), nil +} + +// authCloneURL injects token credentials into an HTTPS clone URL. +func authCloneURL(cfg *Config, cloneURL string) string { + if strings.HasPrefix(cloneURL, "https://") { + return strings.Replace( + cloneURL, + "https://", + fmt.Sprintf("https://%s:%s@", cfg.Username, cfg.Token), + 1, + ) + } + return cloneURL +} diff --git a/cmd/status.go b/cmd/status.go new file mode 100644 index 0000000..4bccd6b --- /dev/null +++ b/cmd/status.go @@ -0,0 +1,13 @@ +package cmd + +import ( + "github.com/spf13/cobra" +) + +var statusCmd = &cobra.Command{ + Use: "status", + Short: "Show git status of the current directory", + RunE: func(cmd *cobra.Command, args []string) error { + return runGit(".", "status") + }, +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8fb2fb1 --- /dev/null +++ b/go.mod @@ -0,0 +1,18 @@ +module gtea + +go 1.21 + +require ( + code.gitea.io/sdk/gitea v0.19.0 + github.com/spf13/cobra v1.8.0 +) + +require ( + github.com/davidmz/go-pageant v1.0.2 // indirect + github.com/go-fed/httpsig v1.1.0 // indirect + github.com/hashicorp/go-version v1.6.0 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + golang.org/x/crypto v0.22.0 // indirect + golang.org/x/sys v0.19.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b00d303 --- /dev/null +++ b/go.sum @@ -0,0 +1,43 @@ +code.gitea.io/sdk/gitea v0.19.0 h1:8I6s1s4RHgzxiPHhOQdgim1RWIRcr0LVMbHBjBFXq4Y= +code.gitea.io/sdk/gitea v0.19.0/go.mod h1:IG9xZJoltDNeDSW0qiF2Vqx5orMWa7OhVWrjvrd5NpI= +github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davidmz/go-pageant v1.0.2 h1:bPblRCh5jGU+Uptpz6LgMZGD5hJoOt7otgT454WvHn0= +github.com/davidmz/go-pageant v1.0.2/go.mod h1:P2EDDnMqIwG5Rrp05dTRITj9z2zpGcD9efWSkTNKLIE= +github.com/go-fed/httpsig v1.1.0 h1:9M+hb0jkEICD8/cAiNqEB66R87tTINszBRTjwjQzWcI= +github.com/go-fed/httpsig v1.1.0/go.mod h1:RCMrTZvN1bJYtofsG4rd5NaO5obxQ5xBkdiS7xsT7bM= +github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek= +github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= +github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= +golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= +golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= +golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q= +golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..a405ed5 --- /dev/null +++ b/main.go @@ -0,0 +1,7 @@ +package main + +import "gtea/cmd" + +func main() { + cmd.Execute() +}