aboutsummaryrefslogtreecommitdiffstats
path: root/bridge
diff options
context:
space:
mode:
Diffstat (limited to 'bridge')
-rw-r--r--bridge/core/bridge.go9
-rw-r--r--bridge/github/config.go40
-rw-r--r--bridge/gitlab/config.go20
-rw-r--r--bridge/launchpad/config.go8
4 files changed, 69 insertions, 8 deletions
diff --git a/bridge/core/bridge.go b/bridge/core/bridge.go
index 9161b418..6fd28b03 100644
--- a/bridge/core/bridge.go
+++ b/bridge/core/bridge.go
@@ -31,10 +31,11 @@ var bridgeImpl map[string]reflect.Type
// BridgeParams holds parameters to simplify the bridge configuration without
// having to make terminal prompts.
type BridgeParams struct {
- Owner string
- Project string
- URL string
- Token string
+ Owner string
+ Project string
+ URL string
+ Token string
+ TokenStdin bool
}
// Bridge is a wrapper around a BridgeImpl that will bind low-level
diff --git a/bridge/github/config.go b/bridge/github/config.go
index 45c078f6..16abfa09 100644
--- a/bridge/github/config.go
+++ b/bridge/github/config.go
@@ -21,6 +21,7 @@ import (
"github.com/MichaelMure/git-bug/bridge/core"
"github.com/MichaelMure/git-bug/repository"
+ "github.com/MichaelMure/git-bug/util/interrupt"
)
const (
@@ -37,13 +38,18 @@ var (
ErrBadProjectURL = errors.New("bad project url")
)
-func (*Github) Configure(repo repository.RepoCommon, params core.BridgeParams) (core.Configuration, error) {
+func (g *Github) Configure(repo repository.RepoCommon, params core.BridgeParams) (core.Configuration, error) {
conf := make(core.Configuration)
var err error
var token string
var owner string
var project string
+ if (params.Token != "" || params.TokenStdin) &&
+ (params.URL == "" && (params.Project == "" || params.Owner == "")) {
+ return nil, fmt.Errorf("you must provide a project URL or Owner/Name to configure this bridge with a token")
+ }
+
// getting owner and project name
if params.Owner != "" && params.Project != "" {
// first try to use params if both or project and owner are provided
@@ -85,6 +91,13 @@ func (*Github) Configure(repo repository.RepoCommon, params core.BridgeParams) (
if params.Token != "" {
token = params.Token
+ } else if params.TokenStdin {
+ reader := bufio.NewReader(os.Stdin)
+ token, err = reader.ReadString('\n')
+ if err != nil {
+ return nil, fmt.Errorf("reading from stdin: %v", err)
+ }
+ token = strings.TrimSuffix(token, "\n")
} else {
token, err = promptTokenOptions(owner, project)
if err != nil {
@@ -106,6 +119,11 @@ func (*Github) Configure(repo repository.RepoCommon, params core.BridgeParams) (
conf[keyOwner] = owner
conf[keyProject] = project
+ err = g.ValidateConfig(conf)
+ if err != nil {
+ return nil, err
+ }
+
return conf, nil
}
@@ -505,6 +523,16 @@ func validateProject(owner, project, token string) (bool, error) {
}
func promptPassword() (string, error) {
+ termState, err := terminal.GetState(int(syscall.Stdin))
+ if err != nil {
+ return "", err
+ }
+
+ cancel := interrupt.RegisterCleaner(func() error {
+ return terminal.Restore(int(syscall.Stdin), termState)
+ })
+ defer cancel()
+
for {
fmt.Print("password: ")
@@ -526,6 +554,16 @@ func promptPassword() (string, error) {
}
func prompt2FA() (string, error) {
+ termState, err := terminal.GetState(int(syscall.Stdin))
+ if err != nil {
+ return "", err
+ }
+
+ cancel := interrupt.RegisterCleaner(func() error {
+ return terminal.Restore(int(syscall.Stdin), termState)
+ })
+ defer cancel()
+
for {
fmt.Print("two-factor authentication code: ")
diff --git a/bridge/gitlab/config.go b/bridge/gitlab/config.go
index 15172871..a673af8c 100644
--- a/bridge/gitlab/config.go
+++ b/bridge/gitlab/config.go
@@ -20,7 +20,7 @@ var (
ErrBadProjectURL = errors.New("bad project url")
)
-func (*Gitlab) Configure(repo repository.RepoCommon, params core.BridgeParams) (core.Configuration, error) {
+func (g *Gitlab) Configure(repo repository.RepoCommon, params core.BridgeParams) (core.Configuration, error) {
if params.Project != "" {
fmt.Println("warning: --project is ineffective for a gitlab bridge")
}
@@ -33,6 +33,10 @@ func (*Gitlab) Configure(repo repository.RepoCommon, params core.BridgeParams) (
var url string
var token string
+ if (params.Token != "" || params.TokenStdin) && params.URL == "" {
+ return nil, fmt.Errorf("you must provide a project URL to configure this bridge with a token")
+ }
+
// get project url
if params.URL != "" {
url = params.URL
@@ -54,6 +58,13 @@ func (*Gitlab) Configure(repo repository.RepoCommon, params core.BridgeParams) (
// get user token
if params.Token != "" {
token = params.Token
+ } else if params.TokenStdin {
+ reader := bufio.NewReader(os.Stdin)
+ token, err = reader.ReadString('\n')
+ if err != nil {
+ return nil, fmt.Errorf("reading from stdin: %v", err)
+ }
+ token = strings.TrimSuffix(token, "\n")
} else {
token, err = promptToken()
if err != nil {
@@ -71,10 +82,15 @@ func (*Gitlab) Configure(repo repository.RepoCommon, params core.BridgeParams) (
conf[keyToken] = token
conf[core.KeyTarget] = target
+ err = g.ValidateConfig(conf)
+ if err != nil {
+ return nil, err
+ }
+
return conf, nil
}
-func (*Gitlab) ValidateConfig(conf core.Configuration) error {
+func (g *Gitlab) ValidateConfig(conf core.Configuration) error {
if v, ok := conf[core.KeyTarget]; !ok {
return fmt.Errorf("missing %s key", core.KeyTarget)
} else if v != target {
diff --git a/bridge/launchpad/config.go b/bridge/launchpad/config.go
index 1d6c8aba..6669d0fa 100644
--- a/bridge/launchpad/config.go
+++ b/bridge/launchpad/config.go
@@ -22,7 +22,7 @@ const (
defaultTimeout = 60 * time.Second
)
-func (*Launchpad) Configure(repo repository.RepoCommon, params core.BridgeParams) (core.Configuration, error) {
+func (l *Launchpad) Configure(repo repository.RepoCommon, params core.BridgeParams) (core.Configuration, error) {
if params.Token != "" {
fmt.Println("warning: --token is ineffective for a Launchpad bridge")
}
@@ -63,6 +63,12 @@ func (*Launchpad) Configure(repo repository.RepoCommon, params core.BridgeParams
conf[keyProject] = project
conf[core.KeyTarget] = target
+
+ err = l.ValidateConfig(conf)
+ if err != nil {
+ return nil, err
+ }
+
return conf, nil
}