1
0
mirror of https://gitea.com/jolheiser/sip synced 2024-11-22 19:51:58 +01:00
sip/cmd/cmd.go
John Olheiser bb773780df Update Beaver and fix bugs (#6)
Fix CI test

Fix origin names in PR create

Add aliases for new issue/PR and fix issues search

Update beaver and squash some bugs

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

Co-authored-by: jolheiser <john.olheiser@gmail.com>
Reviewed-on: https://gitea.com/jolheiser/sip/pulls/6
2020-02-27 02:53:11 +00:00

127 lines
2.8 KiB
Go

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"
"go.jolheiser.com/beaver/color"
"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
}
if len(config.Tokens) == 0 {
return "", errors.New(color.Error.Wrap("No tokens found! Add one with #{sip token create}", color.New(color.FgMagenta)))
}
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 defRemote(remote, def string) string {
if remote == "" {
return def
}
return remote
}
func getUpstreamRepo() []string {
upstreamOnce.Do(func() {
var err error
upstreamRepo, err = git.GetRepo(defRemote(config.Upstream, "upstream"))
if err != nil {
upstreamRepo = getOriginRepo()
}
})
return upstreamRepo
}
func getOriginRepo() []string {
originOnce.Do(func() {
var err error
originRepo, err = git.GetRepo(defRemote(config.Origin, "origin"))
if err != nil {
originRepo = []string{"https://gitea.com", "jolheiser", "sip"}
}
})
return originRepo
}
func fullName(ctx *cli.Context) string {
return ctx.String("owner") + "/" + ctx.String("repo")
}