2020-02-14 21:46:02 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-02-16 06:51:14 +01:00
|
|
|
"code.gitea.io/sdk/gitea"
|
2020-02-16 23:31:01 +01:00
|
|
|
"fmt"
|
2020-02-17 21:50:02 +01:00
|
|
|
"gitea.com/jolheiser/sip/modules/config"
|
|
|
|
"gitea.com/jolheiser/sip/modules/git"
|
2020-02-16 23:31:01 +01:00
|
|
|
"github.com/AlecAivazis/survey/v2"
|
2020-02-14 21:46:02 +01:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
Flags = []cli.Flag{
|
2020-02-17 06:28:11 +01:00
|
|
|
&cli.StringFlag{
|
2020-02-17 22:07:00 +01:00
|
|
|
Name: "origin",
|
|
|
|
Usage: "The origin remote",
|
|
|
|
Value: config.Origin,
|
2020-02-17 06:28:11 +01:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2020-02-17 22:07:00 +01:00
|
|
|
Name: "upstream",
|
|
|
|
Usage: "The upstream remote",
|
|
|
|
Value: config.Upstream,
|
2020-02-17 06:28:11 +01:00
|
|
|
},
|
2020-02-14 21:46:02 +01:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "url",
|
|
|
|
Aliases: []string{"u"},
|
|
|
|
Usage: "The base URL to the Gitea instance",
|
2020-02-16 23:31:01 +01:00
|
|
|
Value: getUpstreamRepo()[0],
|
2020-02-14 21:46:02 +01:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "owner",
|
|
|
|
Aliases: []string{"o"},
|
|
|
|
Usage: "The owner to target",
|
2020-02-16 23:31:01 +01:00
|
|
|
Value: getUpstreamRepo()[1],
|
2020-02-14 21:46:02 +01:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "repo",
|
|
|
|
Aliases: []string{"r"},
|
|
|
|
Usage: "The repo to target",
|
2020-02-16 23:31:01 +01:00
|
|
|
Value: getUpstreamRepo()[2],
|
2020-02-14 21:46:02 +01:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2020-02-18 03:57:51 +01:00
|
|
|
Name: "token",
|
|
|
|
Aliases: []string{"t"},
|
|
|
|
Usage: "The access token to use (by name)",
|
2020-02-14 21:46:02 +01:00
|
|
|
},
|
|
|
|
}
|
2020-02-16 23:31:01 +01:00
|
|
|
originOnce sync.Once
|
|
|
|
originRepo []string
|
|
|
|
upstreamOnce sync.Once
|
|
|
|
upstreamRepo []string
|
2020-02-14 21:46:02 +01:00
|
|
|
)
|
|
|
|
|
2020-02-16 23:31:01 +01:00
|
|
|
func requireToken(ctx *cli.Context) (string, error) {
|
2020-02-18 03:57:51 +01:00
|
|
|
if ctx.IsSet("token") {
|
|
|
|
return getToken(ctx.String("token")), nil
|
2020-02-16 23:31:01 +01:00
|
|
|
}
|
2020-02-18 03:57:51 +01:00
|
|
|
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
|
2020-02-16 23:31:01 +01:00
|
|
|
opts[idx] = key
|
|
|
|
}
|
|
|
|
|
2020-02-18 03:57:51 +01:00
|
|
|
question := &survey.Select{Message: "This action requires an access token", Options: opts}
|
2020-02-16 23:31:01 +01:00
|
|
|
var answer string
|
|
|
|
|
|
|
|
if err := survey.AskOne(question, &answer); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-02-18 03:57:51 +01:00
|
|
|
return tokenMap[answer].Token, nil
|
2020-02-16 23:31:01 +01:00
|
|
|
}
|
|
|
|
|
2020-02-16 06:51:14 +01:00
|
|
|
func getToken(name string) string {
|
2020-02-18 03:57:51 +01:00
|
|
|
for _, token := range config.Tokens {
|
|
|
|
if name == token.Name {
|
|
|
|
return token.Token
|
2020-02-14 21:46:02 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-16 06:51:14 +01:00
|
|
|
return ""
|
2020-02-14 21:46:02 +01:00
|
|
|
}
|
|
|
|
|
2020-02-16 06:51:14 +01:00
|
|
|
func getClient(ctx *cli.Context) *gitea.Client {
|
2020-02-18 03:57:51 +01:00
|
|
|
return gitea.NewClient(ctx.String("url"), getToken(ctx.String("token")))
|
2020-02-14 21:46:02 +01:00
|
|
|
}
|
|
|
|
|
2020-02-16 23:31:01 +01:00
|
|
|
func getUpstreamRepo() []string {
|
|
|
|
upstreamOnce.Do(func() {
|
|
|
|
upstreamRepo = git.GetRepo(config.Upstream)
|
2020-02-17 06:28:11 +01:00
|
|
|
if upstreamRepo == nil {
|
|
|
|
upstreamRepo = git.GetRepo(config.Origin)
|
|
|
|
}
|
2020-02-14 21:46:02 +01:00
|
|
|
})
|
2020-02-16 23:31:01 +01:00
|
|
|
return upstreamRepo
|
|
|
|
}
|
2020-02-14 21:46:02 +01:00
|
|
|
|
2020-02-16 23:31:01 +01:00
|
|
|
func getOriginRepo() []string {
|
|
|
|
originOnce.Do(func() {
|
|
|
|
originRepo = git.GetRepo(config.Origin)
|
|
|
|
})
|
|
|
|
return originRepo
|
2020-02-14 21:46:02 +01:00
|
|
|
}
|
|
|
|
|
2020-02-16 06:51:14 +01:00
|
|
|
func fullName(ctx *cli.Context) string {
|
|
|
|
return ctx.String("owner") + "/" + ctx.String("repo")
|
2020-02-14 21:46:02 +01:00
|
|
|
}
|