diff --git a/cmd/cmd.go b/cmd/cmd.go index 22ea32b..17e4d2e 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -2,13 +2,11 @@ package cmd import ( "code.gitea.io/sdk/gitea" - "errors" "fmt" "gitea.com/jolheiser/sip/modules/config" "gitea.com/jolheiser/sip/modules/git" "github.com/AlecAivazis/survey/v2" "github.com/urfave/cli/v2" - "strings" "sync" ) @@ -109,12 +107,3 @@ func getOriginRepo() []string { func fullName(ctx *cli.Context) string { return ctx.String("owner") + "/" + ctx.String("repo") } - -func validateFullName(ans interface{}) error { - fullName := ans.(string) - ownerRepo := strings.Split(fullName, "/") - if len(ownerRepo) != 2 { - return errors.New("full repo name should be in form `owner/repo`") - } - return nil -} diff --git a/cmd/issues.go b/cmd/issues.go index b16642d..9bd6407 100644 --- a/cmd/issues.go +++ b/cmd/issues.go @@ -2,11 +2,12 @@ package cmd import ( "code.gitea.io/sdk/gitea" + "errors" "fmt" + "gitea.com/jolheiser/beaver" "gitea.com/jolheiser/beaver/color" "gitea.com/jolheiser/sip/modules/markdown" "gitea.com/jolheiser/sip/modules/sdk" - "gitea.com/jolheiser/sip/modules/stdout" "github.com/AlecAivazis/survey/v2" "github.com/urfave/cli/v2" "strconv" @@ -41,7 +42,7 @@ func issuesSearch(ctx *cli.Context, pulls bool) (*gitea.Issue, error) { } if len(issues) == 0 { - stdout.Red("No " + typ + " found") + beaver.Errorf("No %s found!", typ) return nil, nil } @@ -125,3 +126,12 @@ func queryIssues(ctx *cli.Context, client *gitea.Client, pulls bool) ([]*gitea.I return filtered, nil } + +func validateFullName(ans interface{}) error { + fullName := ans.(string) + ownerRepo := strings.Split(fullName, "/") + if len(ownerRepo) != 2 { + return errors.New("full repo name should be in form `owner/repo`") + } + return nil +} diff --git a/cmd/repo.go b/cmd/repo.go index e50bc58..e8890a4 100644 --- a/cmd/repo.go +++ b/cmd/repo.go @@ -32,11 +32,19 @@ func doRepo(ctx *cli.Context) error { issueCount++ } - beaver.Infof("Repository: %s", color.New(color.FgYellow).Format(ctx.String("owner")+"/"+ctx.String("repo"))) - beaver.Infof("URL: %s", color.New(color.FgYellow).Format(ctx.String("url")+"/"+ctx.String("owner")+"/"+ctx.String("repo"))) + forks, err := sdk.GetForks(client, ctx.String("owner"), ctx.String("repo")) + if err != nil { + return err + } + + yellow := color.New(color.FgYellow) + beaver.Infof("Repository: %s", yellow.Format(ctx.String("owner")+"/"+ctx.String("repo"))) + beaver.Infof("URL: %s", yellow.Format(ctx.String("url")+"/"+ctx.String("owner")+"/"+ctx.String("repo"))) beaver.Info() - beaver.Infof("Open Issues: %s", color.New(color.FgYellow).Format(strconv.Itoa(issueCount))) - beaver.Infof("Open Pulls: %s", color.New(color.FgYellow).Format(strconv.Itoa(pullCount))) + beaver.Infof("Open Issues: %s", yellow.Format(strconv.Itoa(issueCount))) + beaver.Infof("Open Pulls: %s", yellow.Format(strconv.Itoa(pullCount))) + beaver.Info() + beaver.Infof("Forks: %s", yellow.Format(strconv.Itoa(len(forks)))) return nil } diff --git a/cmd/token.go b/cmd/token.go index 6f32e9d..f79e0d1 100644 --- a/cmd/token.go +++ b/cmd/token.go @@ -2,6 +2,7 @@ package cmd import ( "gitea.com/jolheiser/beaver" + "gitea.com/jolheiser/beaver/color" "gitea.com/jolheiser/sip/modules/config" "github.com/urfave/cli/v2" ) @@ -18,6 +19,9 @@ var Tokens = cli.Command{ } func doTokenList(ctx *cli.Context) error { + if len(config.Tokens) == 0 { + beaver.Errorf("No tokens found! Add one with %s", color.New(color.FgMagenta).Format("sip token create")) + } for idx, token := range config.Tokens { beaver.Infof("%d. %s (%s)", idx+1, token.Name, token.URL) } diff --git a/cmd/token_add.go b/cmd/token_add.go index 73858ac..b937875 100644 --- a/cmd/token_add.go +++ b/cmd/token_add.go @@ -10,9 +10,10 @@ import ( ) var TokensAdd = cli.Command{ - Name: "add", - Usage: "Add a new access token", - Action: doTokenAdd, + Name: "add", + Aliases: []string{"create"}, + Usage: "Add a new access token", + Action: doTokenAdd, } func doTokenAdd(ctx *cli.Context) error { @@ -23,11 +24,6 @@ func doTokenAdd(ctx *cli.Context) error { Prompt: &survey.Input{Message: "Local nickname for this token", Default: "gitea"}, Validate: validateTokenName, }, - { - Name: "token", - Prompt: &survey.Input{Message: "Name for this token in Gitea", Default: "sip"}, - Validate: survey.Required, - }, { Name: "url", Prompt: &survey.Input{Message: "URL for the Gitea instance", Default: ctx.String("url")}, @@ -35,9 +31,8 @@ func doTokenAdd(ctx *cli.Context) error { }, } answers := struct { - Name string - Token string - URL string + Name string + URL string }{} if err := survey.Ask(questions, &answers); err != nil { @@ -45,7 +40,13 @@ func doTokenAdd(ctx *cli.Context) error { } if token == "" { - authQuestions := []*survey.Question{ + giteaQuestions := []*survey.Question{ + + { + Name: "token", + Prompt: &survey.Input{Message: "Name for this token in Gitea", Default: "sip"}, + Validate: survey.Required, + }, { Name: "username", Prompt: &survey.Input{Message: "Username for the Gitea instance"}, @@ -57,18 +58,19 @@ func doTokenAdd(ctx *cli.Context) error { Validate: survey.Required, }, } - authAnswers := struct { + giteaAnswers := struct { + Token string Username string Password string }{} - if err := survey.Ask(authQuestions, &authAnswers); err != nil { + if err := survey.Ask(giteaQuestions, &giteaAnswers); err != nil { return err } client := gitea.NewClient(answers.URL, "") - t, err := client.CreateAccessToken(authAnswers.Username, authAnswers.Password, gitea.CreateAccessTokenOption{Name: answers.Token}) + t, err := client.CreateAccessToken(giteaAnswers.Username, giteaAnswers.Password, gitea.CreateAccessTokenOption{Name: giteaAnswers.Token}) if err != nil { return err } diff --git a/cmd/token_remove.go b/cmd/token_remove.go index 5d9fbba..3a43c3a 100644 --- a/cmd/token_remove.go +++ b/cmd/token_remove.go @@ -9,9 +9,10 @@ import ( ) var TokensRemove = cli.Command{ - Name: "remove", - Usage: "Remove access tokens", - Action: doTokenRemove, + Name: "remove", + Aliases: []string{"delete"}, + Usage: "Remove access tokens", + Action: doTokenRemove, } func doTokenRemove(ctx *cli.Context) error { diff --git a/main.go b/main.go index 1cb3dfb..deaf50c 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,9 @@ import ( var Version = "develop" func main() { + + // config loads on init + app := cli.NewApp() app.Name = "Sip" app.Usage = "Command line tool to interact with Gitea" diff --git a/modules/config/config.go b/modules/config/config.go index ffda306..049e379 100644 --- a/modules/config/config.go +++ b/modules/config/config.go @@ -13,6 +13,7 @@ var ( configPath string cfg *config + // Config items Origin string Upstream string Tokens []Token @@ -30,6 +31,7 @@ type Token struct { Token string `toml:"token"` } +// Load on init so that CLI contexts are correctly populated func init() { home, err := homedir.Dir() if err != nil { diff --git a/modules/sdk/repo.go b/modules/sdk/repo.go new file mode 100644 index 0000000..f7fa00f --- /dev/null +++ b/modules/sdk/repo.go @@ -0,0 +1,16 @@ +package sdk + +import "code.gitea.io/sdk/gitea" + +// GetForks returns all of a repository's forks +// TODO Update to handle paging when SDK supports +func GetForks(client *gitea.Client, owner, repo string) ([]*gitea.Repository, error) { + repos, err := client.ListForks(owner, repo) + if err != nil { + return nil, err + } + return repos, nil +} + +// TODO List Stargazers when SDK supports +// TODO List Watchers when SDK supports diff --git a/modules/stdout/stdout.go b/modules/stdout/stdout.go deleted file mode 100644 index e9185c1..0000000 --- a/modules/stdout/stdout.go +++ /dev/null @@ -1,19 +0,0 @@ -package stdout - -import ( - "fmt" - "gitea.com/jolheiser/beaver/color" -) - -var ( - red = color.New(color.FgRed) - green = color.New(color.FgGreen) -) - -func Red(format string, args ...interface{}) { - fmt.Println(red.Format(fmt.Sprintf(format, args...))) -} - -func Green(format string, args ...interface{}) { - fmt.Println(green.Format(fmt.Sprintf(format, args...))) -}