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 <noreply@anthropic.com>
This commit is contained in:
2026-05-17 14:11:34 -04:00
commit b7ea167786
11 changed files with 404 additions and 0 deletions

28
cmd/commit.go Normal file
View File

@@ -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")
}

50
cmd/config.go Normal file
View File

@@ -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] + "****"
}

91
cmd/init.go Normal file
View File

@@ -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 <project-name>",
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")
}

17
cmd/pull.go Normal file
View File

@@ -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)
},
}

17
cmd/push.go Normal file
View File

@@ -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)
},
}

103
cmd/root.go Normal file
View File

@@ -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
}

13
cmd/status.go Normal file
View File

@@ -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")
},
}