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>
51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
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] + "****"
|
|
}
|