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-14 21:46:02 +01:00
|
|
|
"errors"
|
2020-02-16 23:31:01 +01:00
|
|
|
"fmt"
|
2020-02-14 21:46:02 +01:00
|
|
|
"gitea.com/jolheiser/tea/modules/config"
|
2020-02-16 23:31:01 +01:00
|
|
|
"gitea.com/jolheiser/tea/modules/git"
|
|
|
|
"github.com/AlecAivazis/survey/v2"
|
2020-02-14 21:46:02 +01:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
Flags = []cli.Flag{
|
2020-02-17 06:28:11 +01:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "origin",
|
|
|
|
Usage: "The origin remote",
|
|
|
|
Value: config.Origin,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "upstream",
|
|
|
|
Usage: "The upstream remote",
|
|
|
|
Value: config.Upstream,
|
|
|
|
},
|
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{
|
|
|
|
Name: "login",
|
|
|
|
Aliases: []string{"l"},
|
|
|
|
Usage: "The login token to use (by name)",
|
|
|
|
},
|
|
|
|
}
|
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) {
|
|
|
|
if ctx.IsSet("login") {
|
|
|
|
return getToken(ctx.String("login")), nil
|
|
|
|
}
|
|
|
|
loginMap := make(map[string]config.Login)
|
|
|
|
opts := make([]string, len(config.Logins))
|
|
|
|
for idx, login := range config.Logins {
|
|
|
|
key := fmt.Sprintf("%s (%s)", login.Name, login.URL)
|
|
|
|
loginMap[key] = login
|
|
|
|
opts[idx] = key
|
|
|
|
}
|
|
|
|
|
|
|
|
question := &survey.Select{Message: "This action requires a login token", Options: opts}
|
|
|
|
var answer string
|
|
|
|
|
|
|
|
if err := survey.AskOne(question, &answer); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return loginMap[answer].Token, nil
|
|
|
|
}
|
|
|
|
|
2020-02-16 06:51:14 +01:00
|
|
|
func getToken(name string) string {
|
2020-02-14 21:46:02 +01:00
|
|
|
for _, login := range config.Logins {
|
2020-02-16 06:51:14 +01:00
|
|
|
if name == login.Name {
|
|
|
|
return login.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 {
|
|
|
|
return gitea.NewClient(ctx.String("url"), getToken(ctx.String("login")))
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|