aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/core
diff options
context:
space:
mode:
Diffstat (limited to 'bridge/core')
-rw-r--r--bridge/core/auth/options.go11
-rw-r--r--bridge/core/bridge.go31
-rw-r--r--bridge/core/interfaces.go3
-rw-r--r--bridge/core/params.go36
4 files changed, 66 insertions, 15 deletions
diff --git a/bridge/core/auth/options.go b/bridge/core/auth/options.go
index 74189874..1d8c44d1 100644
--- a/bridge/core/auth/options.go
+++ b/bridge/core/auth/options.go
@@ -2,7 +2,7 @@ package auth
type options struct {
target string
- kind CredentialKind
+ kind map[CredentialKind]interface{}
meta map[string]string
}
@@ -21,7 +21,8 @@ func (opts *options) Match(cred Credential) bool {
return false
}
- if opts.kind != "" && cred.Kind() != opts.kind {
+ _, has := opts.kind[cred.Kind()]
+ if len(opts.kind) > 0 && !has {
return false
}
@@ -40,9 +41,13 @@ func WithTarget(target string) Option {
}
}
+// WithKind match credentials with the given kind. Can be specified multiple times.
func WithKind(kind CredentialKind) Option {
return func(opts *options) {
- opts.kind = kind
+ if opts.kind == nil {
+ opts.kind = make(map[CredentialKind]interface{})
+ }
+ opts.kind[kind] = nil
}
}
diff --git a/bridge/core/bridge.go b/bridge/core/bridge.go
index ac0d47d7..62fd70f6 100644
--- a/bridge/core/bridge.go
+++ b/bridge/core/bridge.go
@@ -4,6 +4,7 @@ package core
import (
"context"
"fmt"
+ "os"
"reflect"
"regexp"
"sort"
@@ -30,18 +31,6 @@ const (
var bridgeImpl map[string]reflect.Type
var bridgeLoginMetaKey map[string]string
-// BridgeParams holds parameters to simplify the bridge configuration without
-// having to make terminal prompts.
-type BridgeParams struct {
- Owner string // owner of the repo (Github)
- Project string // name of the repo (Github, Launchpad)
- URL string // complete URL of a repo (Github, Gitlab, Launchpad)
- BaseURL string // base URL for self-hosted instance ( Gitlab)
- CredPrefix string // ID prefix of the credential to use (Github, Gitlab)
- TokenRaw string // pre-existing token to use (Github, Gitlab)
- Login string // username for the passed credential (Github, Gitlab)
-}
-
// Bridge is a wrapper around a BridgeImpl that will bind low-level
// implementation with utility code to provide high-level functions.
type Bridge struct {
@@ -220,6 +209,8 @@ func RemoveBridge(repo repository.RepoConfig, name string) error {
// Configure run the target specific configuration process
func (b *Bridge) Configure(params BridgeParams) error {
+ validateParams(params, b.impl)
+
conf, err := b.impl.Configure(b.repo, params)
if err != nil {
return err
@@ -234,6 +225,22 @@ func (b *Bridge) Configure(params BridgeParams) error {
return b.storeConfig(conf)
}
+func validateParams(params BridgeParams, impl BridgeImpl) {
+ validParams := impl.ValidParams()
+
+ paramsValue := reflect.ValueOf(params)
+ paramsType := paramsValue.Type()
+
+ for i := 0; i < paramsValue.NumField(); i++ {
+ name := paramsType.Field(i).Name
+ val := paramsValue.Field(i).Interface().(string)
+ _, valid := validParams[name]
+ if val != "" && !valid {
+ _, _ = fmt.Fprintln(os.Stderr, params.fieldWarning(name, impl.Target()))
+ }
+ }
+}
+
func (b *Bridge) storeConfig(conf Configuration) error {
for key, val := range conf {
storeKey := fmt.Sprintf("git-bug.bridge.%s.%s", b.Name, key)
diff --git a/bridge/core/interfaces.go b/bridge/core/interfaces.go
index ab2f3977..63340a95 100644
--- a/bridge/core/interfaces.go
+++ b/bridge/core/interfaces.go
@@ -18,6 +18,9 @@ type BridgeImpl interface {
// credentials.
LoginMetaKey() string
+ // The set of the BridgeParams fields supported
+ ValidParams() map[string]interface{}
+
// Configure handle the user interaction and return a key/value configuration
// for future use
Configure(repo *cache.RepoCache, params BridgeParams) (Configuration, error)
diff --git a/bridge/core/params.go b/bridge/core/params.go
new file mode 100644
index 00000000..e398b81a
--- /dev/null
+++ b/bridge/core/params.go
@@ -0,0 +1,36 @@
+package core
+
+import "fmt"
+
+// BridgeParams holds parameters to simplify the bridge configuration without
+// having to make terminal prompts.
+type BridgeParams struct {
+ URL string // complete URL of a repo (Github, Gitlab, , Launchpad)
+ BaseURL string // base URL for self-hosted instance ( Gitlab, Jira, )
+ Login string // username for the passed credential (Github, Gitlab, Jira, )
+ CredPrefix string // ID prefix of the credential to use (Github, Gitlab, Jira, )
+ TokenRaw string // pre-existing token to use (Github, Gitlab, , )
+ Owner string // owner of the repo (Github, , , )
+ Project string // name of the repo or project key (Github, , Jira, Launchpad)
+}
+
+func (BridgeParams) fieldWarning(field string, target string) string {
+ switch field {
+ case "URL":
+ return fmt.Sprintf("warning: --url is ineffective for a %s bridge", target)
+ case "BaseURL":
+ return fmt.Sprintf("warning: --base-url is ineffective for a %s bridge", target)
+ case "Login":
+ return fmt.Sprintf("warning: --login is ineffective for a %s bridge", target)
+ case "CredPrefix":
+ return fmt.Sprintf("warning: --credential is ineffective for a %s bridge", target)
+ case "TokenRaw":
+ return fmt.Sprintf("warning: tokens are ineffective for a %s bridge", target)
+ case "Owner":
+ return fmt.Sprintf("warning: --owner is ineffective for a %s bridge", target)
+ case "Project":
+ return fmt.Sprintf("warning: --project is ineffective for a %s bridge", target)
+ default:
+ panic("unknown field")
+ }
+}