git-bug/bridge/launchpad/config.go
Sascha 1939949fcd
CLI: Add non-interactive option to interactive commands (#651)
* Add option to skip the AvatarURL input request

Using an empty string for the avatar cli flag e.g. `git-bug user create
-a ""` will still result in a prompt. As the avatar URL is an optional
option, it should be possible to skip asking for it entirely.
Otherwise automated user creation via a script must make use of pipe hacks.

* Add global --non-interactive cmdline option

* Replace --skipAvatar for --non-interactive option

* Cmd BugAdd: respect non-interactive option

* Cmd bridge configure: respect non-interactive opt

* Cmd CommentAdd: respect non-interactive option

* Cmd CommentEdit: respect non-interactive option

* Cmd TermUI: respect non-interactive option

* Cmd TitleEdit: respect non-interactive option

* Remove global non-interactive option

* Cmd UserCreate: Use local non-interactive option

* Cmd BugAdd: Use local non-interactive option

* Cmd BridgeConfigure: Use local non-interactive option

* Cmd CommentAdd: Use local non-interactive option

* Cmd CommentEdit: Use local non-interactive option

* Cmd TermUI: Drop non-interactive option

It should be obviouse that the termui is an interactive command.

* Cmd TitleEdit: Use local non-interactive option

* Update docs

* Bridge GitHub: respect non-interactive option

* Bridge GitLab: respect non-interactive option

* Bridge Jira: respect non-interactive and token opt

* Fix failing compilation

* Bridge launchpad: respect non-interactive option

* bridge: isNonInteractive --> interactive

Co-authored-by: Michael Muré <batolettre@gmail.com>
2021-05-09 11:14:45 +02:00

107 lines
2.3 KiB
Go

package launchpad
import (
"errors"
"fmt"
"net/http"
"regexp"
"github.com/MichaelMure/git-bug/bridge/core"
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/input"
)
var ErrBadProjectURL = errors.New("bad Launchpad project URL")
func (Launchpad) ValidParams() map[string]interface{} {
return map[string]interface{}{
"URL": nil,
"Project": nil,
}
}
func (l *Launchpad) Configure(repo *cache.RepoCache, params core.BridgeParams, interactive bool) (core.Configuration, error) {
var err error
var project string
switch {
case params.Project != "":
project = params.Project
case params.URL != "":
// get project name from url
project, err = splitURL(params.URL)
default:
if !interactive {
return nil, fmt.Errorf("Non-interactive-mode is active. Please specify the project name with the --project option.")
}
// get project name from terminal prompt
project, err = input.Prompt("Launchpad project name", "project name", input.Required)
}
if err != nil {
return nil, err
}
// verify project
ok, err := validateProject(project)
if err != nil {
return nil, err
}
if !ok {
return nil, fmt.Errorf("project doesn't exist")
}
conf := make(core.Configuration)
conf[core.ConfigKeyTarget] = target
conf[confKeyProject] = project
err = l.ValidateConfig(conf)
if err != nil {
return nil, err
}
return conf, nil
}
func (*Launchpad) ValidateConfig(conf core.Configuration) error {
if v, ok := conf[core.ConfigKeyTarget]; !ok {
return fmt.Errorf("missing %s key", core.ConfigKeyTarget)
} else if v != target {
return fmt.Errorf("unexpected target name: %v", v)
}
if _, ok := conf[confKeyProject]; !ok {
return fmt.Errorf("missing %s key", confKeyProject)
}
return nil
}
func validateProject(project string) (bool, error) {
url := fmt.Sprintf("%s/%s", apiRoot, project)
client := &http.Client{
Timeout: defaultTimeout,
}
resp, err := client.Get(url)
if err != nil {
return false, err
}
_ = resp.Body.Close()
return resp.StatusCode == http.StatusOK, nil
}
// extract project name from url
func splitURL(url string) (string, error) {
re := regexp.MustCompile(`launchpad\.net[\/:]([^\/]*[a-z]+)`)
res := re.FindStringSubmatch(url)
if res == nil {
return "", ErrBadProjectURL
}
return res[1], nil
}