Add clone and list commands
gtea clone <repo> or <owner/repo> — clones with auth baked in gtea list — shows all your repos with visibility and URL Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
50
cmd/clone.go
Normal file
50
cmd/clone.go
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
gogitea "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var cloneCmd = &cobra.Command{
|
||||||
|
Use: "clone <repo>",
|
||||||
|
Short: "Clone a Gitea repo (use owner/repo or just repo for your own)",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
cfg, err := loadConfig()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
owner, repoName := parseOwnerRepo(args[0], cfg.Username)
|
||||||
|
|
||||||
|
client, err := gogitea.NewClient(cfg.URL, gogitea.SetToken(cfg.Token))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("connecting to Gitea: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
repo, _, err := client.GetRepo(owner, repoName)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("repo %s/%s not found: %w", owner, repoName, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cloneURL := authCloneURL(cfg, repo.CloneURL)
|
||||||
|
fmt.Printf("Cloning %s/%s...\n", owner, repoName)
|
||||||
|
return runGit(".", "clone", cloneURL, repoName)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseOwnerRepo splits "owner/repo" or defaults owner to the current user.
|
||||||
|
func parseOwnerRepo(arg, defaultOwner string) (string, string) {
|
||||||
|
for i, c := range arg {
|
||||||
|
if c == '/' {
|
||||||
|
return arg[:i], arg[i+1:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return defaultOwner, arg
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(cloneCmd)
|
||||||
|
}
|
||||||
51
cmd/list.go
Normal file
51
cmd/list.go
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
gogitea "code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var listCmd = &cobra.Command{
|
||||||
|
Use: "list",
|
||||||
|
Short: "List your Gitea repos",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
cfg, err := loadConfig()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gogitea.NewClient(cfg.URL, gogitea.SetToken(cfg.Token))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("connecting to Gitea: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
repos, _, err := client.ListMyRepos(gogitea.ListReposOptions{
|
||||||
|
ListOptions: gogitea.ListOptions{PageSize: 50},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("listing repos: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(repos) == 0 {
|
||||||
|
fmt.Println("No repos found.")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("%-30s %-8s %s\n", "NAME", "PRIVATE", "URL")
|
||||||
|
fmt.Printf("%-30s %-8s %s\n", "----", "-------", "---")
|
||||||
|
for _, r := range repos {
|
||||||
|
priv := "public"
|
||||||
|
if r.Private {
|
||||||
|
priv = "private"
|
||||||
|
}
|
||||||
|
fmt.Printf("%-30s %-8s %s\n", r.Name, priv, r.HTMLURL)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(listCmd)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user