mirror of
https://gitea.com/jolheiser/sip
synced 2024-11-22 19:51:58 +01:00
b4d7ff5775
Allow overriding repo and choosing issue/PR Signed-off-by: jolheiser <john.olheiser@gmail.com> Add open command for repo/issue/PR Signed-off-by: jolheiser <john.olheiser@gmail.com> Co-authored-by: jolheiser <john.olheiser@gmail.com> Reviewed-on: https://gitea.com/jolheiser/sip/pulls/21
135 lines
2.9 KiB
Go
135 lines
2.9 KiB
Go
package cmd
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
|
|
"gitea.com/jolheiser/sip/modules/config"
|
|
"gitea.com/jolheiser/sip/modules/git"
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
"github.com/AlecAivazis/survey/v2"
|
|
"github.com/urfave/cli/v2"
|
|
"go.jolheiser.com/beaver/color"
|
|
)
|
|
|
|
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 getToken(name string) string {
|
|
for _, token := range config.Tokens {
|
|
if name == token.Name {
|
|
return token.Token
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func getClient(ctx *cli.Context, requireToken bool) (*gitea.Client, error) {
|
|
if ctx.IsSet("token") {
|
|
return gitea.NewClient(ctx.String("url"), getToken(ctx.String("token"))), nil
|
|
}
|
|
|
|
var token string
|
|
if requireToken {
|
|
if len(config.Tokens) == 0 {
|
|
return nil, 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 nil, err
|
|
}
|
|
|
|
token = tokenMap[answer].Token
|
|
}
|
|
|
|
return gitea.NewClient(ctx.String("url"), token), nil
|
|
}
|
|
|
|
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 fullRepoURL(ctx *cli.Context) string {
|
|
return ctx.String("url") + "/" + fullName(ctx)
|
|
}
|
|
|
|
func fullName(ctx *cli.Context) string {
|
|
return ctx.String("owner") + "/" + ctx.String("repo")
|
|
}
|