1
0
mirror of https://gitea.com/jolheiser/sip synced 2024-11-26 07:33:48 +01:00
sip/cmd/cmd.go
John Olheiser 427ecdb7f1 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
2020-02-18 05:27:52 +00:00

110 lines
2.3 KiB
Go

package cmd
import (
"code.gitea.io/sdk/gitea"
"fmt"
"gitea.com/jolheiser/sip/modules/config"
"gitea.com/jolheiser/sip/modules/git"
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"sync"
)
var (
Flags = []cli.Flag{
&cli.StringFlag{
Name: "origin",
Usage: "The origin remote",
Value: config.Origin,
},
&cli.StringFlag{
Name: "upstream",
Usage: "The upstream remote",
Value: config.Upstream,
},
&cli.StringFlag{
Name: "url",
Aliases: []string{"u"},
Usage: "The base URL to the Gitea instance",
Value: getUpstreamRepo()[0],
},
&cli.StringFlag{
Name: "owner",
Aliases: []string{"o"},
Usage: "The owner to target",
Value: getUpstreamRepo()[1],
},
&cli.StringFlag{
Name: "repo",
Aliases: []string{"r"},
Usage: "The repo to target",
Value: getUpstreamRepo()[2],
},
&cli.StringFlag{
Name: "token",
Aliases: []string{"t"},
Usage: "The access token to use (by name)",
},
}
originOnce sync.Once
originRepo []string
upstreamOnce sync.Once
upstreamRepo []string
)
func requireToken(ctx *cli.Context) (string, error) {
if ctx.IsSet("token") {
return getToken(ctx.String("token")), nil
}
tokenMap := make(map[string]config.Token)
opts := make([]string, len(config.Tokens))
for idx, token := range config.Tokens {
key := fmt.Sprintf("%s (%s)", token.Name, token.URL)
tokenMap[key] = token
opts[idx] = key
}
question := &survey.Select{Message: "This action requires an access token", Options: opts}
var answer string
if err := survey.AskOne(question, &answer); err != nil {
return "", err
}
return tokenMap[answer].Token, nil
}
func getToken(name string) string {
for _, token := range config.Tokens {
if name == token.Name {
return token.Token
}
}
return ""
}
func getClient(ctx *cli.Context) *gitea.Client {
return gitea.NewClient(ctx.String("url"), getToken(ctx.String("token")))
}
func getUpstreamRepo() []string {
upstreamOnce.Do(func() {
upstreamRepo = git.GetRepo(config.Upstream)
if upstreamRepo == nil {
upstreamRepo = git.GetRepo(config.Origin)
}
})
return upstreamRepo
}
func getOriginRepo() []string {
originOnce.Do(func() {
originRepo = git.GetRepo(config.Origin)
})
return originRepo
}
func fullName(ctx *cli.Context) string {
return ctx.String("owner") + "/" + ctx.String("repo")
}