143 lines
3.1 KiB
Go
143 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
privateRepo bool
|
|
)
|
|
|
|
func main() {
|
|
var rootCmd = &cobra.Command{
|
|
Use: "teatea",
|
|
Short: "A CLI client for Gitea API",
|
|
}
|
|
|
|
var createCmd = &cobra.Command{
|
|
Use: "create [repository-name]",
|
|
Short: "Create a repository on Gitea",
|
|
Args: cobra.MaximumNArgs(1),
|
|
RunE: runCreate,
|
|
}
|
|
|
|
createCmd.Flags().BoolVarP(&privateRepo, "private", "p", false, "Create a private repository")
|
|
|
|
rootCmd.AddCommand(createCmd)
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func runCreate(cmd *cobra.Command, args []string) error {
|
|
token := os.Getenv("GITEA_TOKEN")
|
|
if token == "" {
|
|
return fmt.Errorf("GITEA_TOKEN environment variable is not set")
|
|
}
|
|
|
|
host := os.Getenv("GITEA_HOST")
|
|
if host == "" {
|
|
return fmt.Errorf("GITEA_HOST environment variable is not set")
|
|
}
|
|
|
|
if !isGitRepository() {
|
|
return fmt.Errorf("not in a git repository")
|
|
}
|
|
|
|
var repoName string
|
|
if len(args) > 0 {
|
|
repoName = args[0]
|
|
} else {
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get current directory: %w", err)
|
|
}
|
|
repoName = filepath.Base(cwd)
|
|
}
|
|
|
|
client, err := gitea.NewClient(host, gitea.SetToken(token))
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create Gitea client: %w", err)
|
|
}
|
|
|
|
user, _, err := client.GetMyUserInfo()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get user info: %w", err)
|
|
}
|
|
|
|
createOpts := gitea.CreateRepoOption{
|
|
Name: repoName,
|
|
Private: privateRepo,
|
|
}
|
|
|
|
repo, _, err := client.CreateRepo(createOpts)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create repository: %w", err)
|
|
}
|
|
|
|
fmt.Printf("Repository created: %s\n", repo.HTMLURL)
|
|
|
|
if err := setupGitRemote(host, user.UserName, repoName); err != nil {
|
|
return fmt.Errorf("failed to setup git remote: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func isGitRepository() bool {
|
|
_, err := os.Stat(".git")
|
|
return err == nil
|
|
}
|
|
|
|
func setupGitRemote(host, username, repoName string) error {
|
|
repoURL := fmt.Sprintf("%s/%s/%s.git", host, username, repoName)
|
|
|
|
originExists := remoteExists("origin")
|
|
giteaExists := remoteExists("gitea")
|
|
|
|
if !originExists {
|
|
if err := addRemote("origin", repoURL); err != nil {
|
|
return err
|
|
}
|
|
fmt.Fprintf(os.Stderr, "Added 'origin' remote: %s\n", repoURL)
|
|
} else if !giteaExists {
|
|
if err := addRemote("gitea", repoURL); err != nil {
|
|
return err
|
|
}
|
|
fmt.Fprintf(os.Stderr, "Added 'gitea' remote: %s\n", repoURL)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func remoteExists(name string) bool {
|
|
cmd := fmt.Sprintf("git remote get-url %s", name)
|
|
output, err := execCommand(cmd)
|
|
return err == nil && output != ""
|
|
}
|
|
|
|
func addRemote(name, url string) error {
|
|
cmd := fmt.Sprintf("git remote add %s %s", name, url)
|
|
_, err := execCommand(cmd)
|
|
return err
|
|
}
|
|
|
|
func execCommand(command string) (string, error) {
|
|
parts := strings.Fields(command)
|
|
if len(parts) == 0 {
|
|
return "", fmt.Errorf("empty command")
|
|
}
|
|
|
|
cmd := exec.Command(parts[0], parts[1:]...)
|
|
output, err := cmd.CombinedOutput()
|
|
return strings.TrimSpace(string(output)), err
|
|
} |