1
0
mirror of https://gitea.com/jolheiser/sip synced 2024-11-22 19:51:58 +01:00

Clean up and polish (#1)

Move issues validator

Signed-off-by: jolheiser <john.olheiser@gmail.com>

Comments

Signed-off-by: jolheiser <john.olheiser@gmail.com>

Package cleanup

Signed-off-by: jolheiser <john.olheiser@gmail.com>

Co-authored-by: jolheiser <john.olheiser@gmail.com>
Reviewed-on: https://gitea.com/jolheiser/sip/pulls/1
This commit is contained in:
John Olheiser 2020-02-18 05:27:52 +00:00
parent 20df4b7f69
commit 427ecdb7f1
10 changed files with 70 additions and 54 deletions

@ -2,13 +2,11 @@ package cmd
import ( import (
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
"errors"
"fmt" "fmt"
"gitea.com/jolheiser/sip/modules/config" "gitea.com/jolheiser/sip/modules/config"
"gitea.com/jolheiser/sip/modules/git" "gitea.com/jolheiser/sip/modules/git"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"strings"
"sync" "sync"
) )
@ -109,12 +107,3 @@ func getOriginRepo() []string {
func fullName(ctx *cli.Context) string { func fullName(ctx *cli.Context) string {
return ctx.String("owner") + "/" + ctx.String("repo") 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
}

@ -2,11 +2,12 @@ package cmd
import ( import (
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
"errors"
"fmt" "fmt"
"gitea.com/jolheiser/beaver"
"gitea.com/jolheiser/beaver/color" "gitea.com/jolheiser/beaver/color"
"gitea.com/jolheiser/sip/modules/markdown" "gitea.com/jolheiser/sip/modules/markdown"
"gitea.com/jolheiser/sip/modules/sdk" "gitea.com/jolheiser/sip/modules/sdk"
"gitea.com/jolheiser/sip/modules/stdout"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"strconv" "strconv"
@ -41,7 +42,7 @@ func issuesSearch(ctx *cli.Context, pulls bool) (*gitea.Issue, error) {
} }
if len(issues) == 0 { if len(issues) == 0 {
stdout.Red("No " + typ + " found") beaver.Errorf("No %s found!", typ)
return nil, nil return nil, nil
} }
@ -125,3 +126,12 @@ func queryIssues(ctx *cli.Context, client *gitea.Client, pulls bool) ([]*gitea.I
return filtered, nil 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
}

@ -32,11 +32,19 @@ func doRepo(ctx *cli.Context) error {
issueCount++ issueCount++
} }
beaver.Infof("Repository: %s", color.New(color.FgYellow).Format(ctx.String("owner")+"/"+ctx.String("repo"))) forks, err := sdk.GetForks(client, ctx.String("owner"), ctx.String("repo"))
beaver.Infof("URL: %s", color.New(color.FgYellow).Format(ctx.String("url")+"/"+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.Info()
beaver.Infof("Open Issues: %s", color.New(color.FgYellow).Format(strconv.Itoa(issueCount))) beaver.Infof("Open Issues: %s", yellow.Format(strconv.Itoa(issueCount)))
beaver.Infof("Open Pulls: %s", color.New(color.FgYellow).Format(strconv.Itoa(pullCount))) beaver.Infof("Open Pulls: %s", yellow.Format(strconv.Itoa(pullCount)))
beaver.Info()
beaver.Infof("Forks: %s", yellow.Format(strconv.Itoa(len(forks))))
return nil return nil
} }

@ -2,6 +2,7 @@ package cmd
import ( import (
"gitea.com/jolheiser/beaver" "gitea.com/jolheiser/beaver"
"gitea.com/jolheiser/beaver/color"
"gitea.com/jolheiser/sip/modules/config" "gitea.com/jolheiser/sip/modules/config"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@ -18,6 +19,9 @@ var Tokens = cli.Command{
} }
func doTokenList(ctx *cli.Context) error { 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 { for idx, token := range config.Tokens {
beaver.Infof("%d. %s (%s)", idx+1, token.Name, token.URL) beaver.Infof("%d. %s (%s)", idx+1, token.Name, token.URL)
} }

@ -10,9 +10,10 @@ import (
) )
var TokensAdd = cli.Command{ var TokensAdd = cli.Command{
Name: "add", Name: "add",
Usage: "Add a new access token", Aliases: []string{"create"},
Action: doTokenAdd, Usage: "Add a new access token",
Action: doTokenAdd,
} }
func doTokenAdd(ctx *cli.Context) error { 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"}, Prompt: &survey.Input{Message: "Local nickname for this token", Default: "gitea"},
Validate: validateTokenName, Validate: validateTokenName,
}, },
{
Name: "token",
Prompt: &survey.Input{Message: "Name for this token in Gitea", Default: "sip"},
Validate: survey.Required,
},
{ {
Name: "url", Name: "url",
Prompt: &survey.Input{Message: "URL for the Gitea instance", Default: ctx.String("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 { answers := struct {
Name string Name string
Token string URL string
URL string
}{} }{}
if err := survey.Ask(questions, &answers); err != nil { if err := survey.Ask(questions, &answers); err != nil {
@ -45,7 +40,13 @@ func doTokenAdd(ctx *cli.Context) error {
} }
if token == "" { 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", Name: "username",
Prompt: &survey.Input{Message: "Username for the Gitea instance"}, Prompt: &survey.Input{Message: "Username for the Gitea instance"},
@ -57,18 +58,19 @@ func doTokenAdd(ctx *cli.Context) error {
Validate: survey.Required, Validate: survey.Required,
}, },
} }
authAnswers := struct { giteaAnswers := struct {
Token string
Username string Username string
Password string Password string
}{} }{}
if err := survey.Ask(authQuestions, &authAnswers); err != nil { if err := survey.Ask(giteaQuestions, &giteaAnswers); err != nil {
return err return err
} }
client := gitea.NewClient(answers.URL, "") 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 { if err != nil {
return err return err
} }

@ -9,9 +9,10 @@ import (
) )
var TokensRemove = cli.Command{ var TokensRemove = cli.Command{
Name: "remove", Name: "remove",
Usage: "Remove access tokens", Aliases: []string{"delete"},
Action: doTokenRemove, Usage: "Remove access tokens",
Action: doTokenRemove,
} }
func doTokenRemove(ctx *cli.Context) error { func doTokenRemove(ctx *cli.Context) error {

@ -10,6 +10,9 @@ import (
var Version = "develop" var Version = "develop"
func main() { func main() {
// config loads on init
app := cli.NewApp() app := cli.NewApp()
app.Name = "Sip" app.Name = "Sip"
app.Usage = "Command line tool to interact with Gitea" app.Usage = "Command line tool to interact with Gitea"

@ -13,6 +13,7 @@ var (
configPath string configPath string
cfg *config cfg *config
// Config items
Origin string Origin string
Upstream string Upstream string
Tokens []Token Tokens []Token
@ -30,6 +31,7 @@ type Token struct {
Token string `toml:"token"` Token string `toml:"token"`
} }
// Load on init so that CLI contexts are correctly populated
func init() { func init() {
home, err := homedir.Dir() home, err := homedir.Dir()
if err != nil { if err != nil {

16
modules/sdk/repo.go Normal file

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

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