diff options
author | Michael Muré <batolettre@gmail.com> | 2020-02-15 02:55:19 +0100 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-02-15 02:55:19 +0100 |
commit | fe3d5c95e4be5874066402b5463ada34894c7f01 (patch) | |
tree | 29f04fae0dc3d5d4883d4989012c26109ba754dc /bridge/gitlab/config.go | |
parent | 2df72942f2b057956c7873f908b64880ab647331 (diff) | |
download | git-bug-fe3d5c95e4be5874066402b5463ada34894c7f01.tar.gz |
bridges: massive refactor
- automatic flag validation and warning
- generalized prompt
- cleanups
Diffstat (limited to 'bridge/gitlab/config.go')
-rw-r--r-- | bridge/gitlab/config.go | 208 |
1 files changed, 43 insertions, 165 deletions
diff --git a/bridge/gitlab/config.go b/bridge/gitlab/config.go index 9bd9c3c7..94026635 100644 --- a/bridge/gitlab/config.go +++ b/bridge/gitlab/config.go @@ -1,18 +1,13 @@ package gitlab import ( - "bufio" "fmt" "net/url" - "os" "path" "regexp" - "sort" "strconv" "strings" - "time" - text "github.com/MichaelMure/go-term-text" "github.com/pkg/errors" "github.com/xanzy/go-gitlab" @@ -21,22 +16,23 @@ import ( "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/input" "github.com/MichaelMure/git-bug/repository" - "github.com/MichaelMure/git-bug/util/colors" ) var ( ErrBadProjectURL = errors.New("bad project url") ) -func (g *Gitlab) Configure(repo *cache.RepoCache, params core.BridgeParams) (core.Configuration, error) { - if params.Project != "" { - fmt.Println("warning: --project is ineffective for a gitlab bridge") - } - if params.Owner != "" { - fmt.Println("warning: --owner is ineffective for a gitlab bridge") +func (g *Gitlab) ValidParams() map[string]interface{} { + return map[string]interface{}{ + "URL": nil, + "BaseURL": nil, + "Login": nil, + "CredPrefix": nil, + "TokenRaw": nil, } +} - conf := make(core.Configuration) +func (g *Gitlab) Configure(repo *cache.RepoCache, params core.BridgeParams) (core.Configuration, error) { var err error var baseUrl string @@ -44,7 +40,7 @@ func (g *Gitlab) Configure(repo *cache.RepoCache, params core.BridgeParams) (cor case params.BaseURL != "": baseUrl = params.BaseURL default: - baseUrl, err = promptBaseUrlOptions() + baseUrl, err = input.PromptDefault("Gitlab server URL", "URL", defaultBaseURL, input.Required, input.IsURL) if err != nil { return nil, errors.Wrap(err, "base url prompt") } @@ -117,9 +113,10 @@ func (g *Gitlab) Configure(repo *cache.RepoCache, params core.BridgeParams) (cor return nil, errors.Wrap(err, "project validation") } + conf := make(core.Configuration) conf[core.ConfigKeyTarget] = target - conf[keyProjectID] = strconv.Itoa(id) - conf[keyGitlabBaseUrl] = baseUrl + conf[confKeyProjectID] = strconv.Itoa(id) + conf[confKeyGitlabBaseUrl] = baseUrl err = g.ValidateConfig(conf) if err != nil { @@ -143,107 +140,35 @@ func (g *Gitlab) ValidateConfig(conf core.Configuration) error { } else if v != target { return fmt.Errorf("unexpected target name: %v", v) } - if _, ok := conf[keyGitlabBaseUrl]; !ok { - return fmt.Errorf("missing %s key", keyGitlabBaseUrl) + if _, ok := conf[confKeyGitlabBaseUrl]; !ok { + return fmt.Errorf("missing %s key", confKeyGitlabBaseUrl) } - if _, ok := conf[keyProjectID]; !ok { - return fmt.Errorf("missing %s key", keyProjectID) + if _, ok := conf[confKeyProjectID]; !ok { + return fmt.Errorf("missing %s key", confKeyProjectID) } return nil } -func promptBaseUrlOptions() (string, error) { - index, err := input.PromptChoice("Gitlab base url", []string{ - "https://gitlab.com", - "enter your own base url", - }) - +func promptTokenOptions(repo repository.RepoConfig, login, baseUrl string) (auth.Credential, error) { + creds, err := auth.List(repo, + auth.WithTarget(target), + auth.WithKind(auth.KindToken), + auth.WithMeta(auth.MetaKeyLogin, login), + auth.WithMeta(auth.MetaKeyBaseURL, baseUrl), + ) if err != nil { - return "", err - } - - if index == 0 { - return defaultBaseURL, nil - } else { - return promptBaseUrl() - } -} - -func promptBaseUrl() (string, error) { - validator := func(name string, value string) (string, error) { - u, err := url.Parse(value) - if err != nil { - return err.Error(), nil - } - if u.Scheme == "" { - return "missing scheme", nil - } - if u.Host == "" { - return "missing host", nil - } - return "", nil + return nil, err } - return input.Prompt("Base url", "url", input.Required, validator) -} - -func promptTokenOptions(repo repository.RepoConfig, login, baseUrl string) (auth.Credential, error) { - for { - creds, err := auth.List(repo, - auth.WithTarget(target), - auth.WithKind(auth.KindToken), - auth.WithMeta(auth.MetaKeyLogin, login), - auth.WithMeta(auth.MetaKeyBaseURL, baseUrl), - ) - if err != nil { - return nil, err - } - - // if we don't have existing token, fast-track to the token prompt - if len(creds) == 0 { - return promptToken(baseUrl) - } - - fmt.Println() - fmt.Println("[1]: enter my token") - - fmt.Println() - fmt.Println("Existing tokens for Gitlab:") - - sort.Sort(auth.ById(creds)) - for i, cred := range creds { - token := cred.(*auth.Token) - fmt.Printf("[%d]: %s => %s (%s)\n", - i+2, - colors.Cyan(token.ID().Human()), - colors.Red(text.TruncateMax(token.Value, 10)), - token.CreateTime().Format(time.RFC822), - ) - } - - fmt.Println() - fmt.Print("Select option: ") - - line, err := bufio.NewReader(os.Stdin).ReadString('\n') - fmt.Println() - if err != nil { - return nil, err - } - - line = strings.TrimSpace(line) - index, err := strconv.Atoi(line) - if err != nil || index < 1 || index > len(creds)+1 { - fmt.Println("invalid input") - continue - } - - switch index { - case 1: - return promptToken(baseUrl) - default: - return creds[index-2], nil - } + cred, err := input.PromptCredential(target, "token", creds) + switch err { + case nil: + return cred, nil + case input.ErrDirectPrompt: + return promptToken(baseUrl) + default: + return nil, err } } @@ -285,64 +210,12 @@ func promptToken(baseUrl string) (*auth.Token, error) { } func promptProjectURL(repo repository.RepoCommon, baseUrl string) (string, error) { - // remote suggestions - remotes, err := repo.GetRemotes() + validRemotes, err := getValidGitlabRemoteURLs(repo, baseUrl) if err != nil { - return "", errors.Wrap(err, "getting remotes") - } - - validRemotes := getValidGitlabRemoteURLs(baseUrl, remotes) - if len(validRemotes) > 0 { - for { - fmt.Println("\nDetected projects:") - - // print valid remote gitlab urls - for i, remote := range validRemotes { - fmt.Printf("[%d]: %v\n", i+1, remote) - } - - fmt.Printf("\n[0]: Another project\n\n") - fmt.Printf("Select option: ") - - line, err := bufio.NewReader(os.Stdin).ReadString('\n') - if err != nil { - return "", err - } - - line = strings.TrimSpace(line) - - index, err := strconv.Atoi(line) - if err != nil || index < 0 || index > len(validRemotes) { - fmt.Println("invalid input") - continue - } - - // if user want to enter another project url break this loop - if index == 0 { - break - } - - return validRemotes[index-1], nil - } + return "", err } - // manually enter gitlab url - for { - fmt.Print("Gitlab project URL: ") - - line, err := bufio.NewReader(os.Stdin).ReadString('\n') - if err != nil { - return "", err - } - - projectURL := strings.TrimSpace(line) - if projectURL == "" { - fmt.Println("URL is empty") - continue - } - - return projectURL, nil - } + return input.PromptURLWithRemote("Gitlab project URL", "URL", validRemotes, input.Required) } func getProjectPath(baseUrl, projectUrl string) (string, error) { @@ -364,7 +237,12 @@ func getProjectPath(baseUrl, projectUrl string) (string, error) { return objectUrl.Path[1:], nil } -func getValidGitlabRemoteURLs(baseUrl string, remotes map[string]string) []string { +func getValidGitlabRemoteURLs(repo repository.RepoCommon, baseUrl string) ([]string, error) { + remotes, err := repo.GetRemotes() + if err != nil { + return nil, err + } + urls := make([]string, 0, len(remotes)) for _, u := range remotes { path, err := getProjectPath(baseUrl, u) @@ -375,7 +253,7 @@ func getValidGitlabRemoteURLs(baseUrl string, remotes map[string]string) []strin urls = append(urls, fmt.Sprintf("%s/%s", baseUrl, path)) } - return urls + return urls, nil } func validateProjectURL(baseUrl, url string, token *auth.Token) (int, error) { |