changes to the BridgeImpl Interface

Amine Hilaly created

Add bridge params
Change bridge.Configure signature

Change summary

bridge/core/bridge.go        | 13 ++++++++++-
bridge/core/interfaces.go    |  2 
commands/bridge_configure.go | 39 +++++++++++++++++++++++++++++--------
3 files changed, 42 insertions(+), 12 deletions(-)

Detailed changes

bridge/core/bridge.go 🔗

@@ -20,6 +20,15 @@ const bridgeConfigKeyPrefix = "git-bug.bridge"
 
 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
+}
+
 // Bridge is a wrapper around a BridgeImpl that will bind low-level
 // implementation with utility code to provide high-level functions.
 type Bridge struct {
@@ -166,8 +175,8 @@ func RemoveBridge(repo repository.RepoCommon, fullName string) error {
 }
 
 // Configure run the target specific configuration process
-func (b *Bridge) Configure() error {
-	conf, err := b.impl.Configure(b.repo)
+func (b *Bridge) Configure(params BridgeParams) error {
+	conf, err := b.impl.Configure(b.repo, params)
 	if err != nil {
 		return err
 	}

bridge/core/interfaces.go 🔗

@@ -15,7 +15,7 @@ type BridgeImpl interface {
 
 	// Configure handle the user interaction and return a key/value configuration
 	// for future use
-	Configure(repo repository.RepoCommon) (Configuration, error)
+	Configure(repo repository.RepoCommon, params BridgeParams) (Configuration, error)
 
 	// ValidateConfig check the configuration for error
 	ValidateConfig(conf Configuration) error

commands/bridge_configure.go 🔗

@@ -7,12 +7,24 @@ import (
 	"strconv"
 	"strings"
 
+	"github.com/MichaelMure/git-bug/bridge/core"
+
 	"github.com/MichaelMure/git-bug/bridge"
 	"github.com/MichaelMure/git-bug/cache"
 	"github.com/MichaelMure/git-bug/util/interrupt"
 	"github.com/spf13/cobra"
 )
 
+const (
+	defaultName = "default"
+)
+
+var (
+	name         string
+	target       string
+	bridgeParams core.BridgeParams
+)
+
 func runBridgeConfigure(cmd *cobra.Command, args []string) error {
 	backend, err := cache.NewRepoCache(repo)
 	if err != nil {
@@ -21,14 +33,18 @@ func runBridgeConfigure(cmd *cobra.Command, args []string) error {
 	defer backend.Close()
 	interrupt.RegisterCleaner(backend.Close)
 
-	target, err := promptTarget()
-	if err != nil {
-		return err
+	if target == "" {
+		target, err = promptTarget()
+		if err != nil {
+			return err
+		}
 	}
 
-	name, err := promptName()
-	if err != nil {
-		return err
+	if name == "" {
+		name, err = promptName()
+		if err != nil {
+			return err
+		}
 	}
 
 	b, err := bridge.NewBridge(backend, target, name)
@@ -36,11 +52,12 @@ func runBridgeConfigure(cmd *cobra.Command, args []string) error {
 		return err
 	}
 
-	err = b.Configure()
+	err = b.Configure(bridgeParams)
 	if err != nil {
 		return err
 	}
 
+	fmt.Println("successfully configured bridge")
 	return nil
 }
 
@@ -71,8 +88,6 @@ func promptTarget() (string, error) {
 }
 
 func promptName() (string, error) {
-	defaultName := "default"
-
 	fmt.Printf("name [%s]: ", defaultName)
 
 	line, err := bufio.NewReader(os.Stdin).ReadString('\n')
@@ -98,4 +113,10 @@ var bridgeConfigureCmd = &cobra.Command{
 
 func init() {
 	bridgeCmd.AddCommand(bridgeConfigureCmd)
+	bridgeConfigureCmd.Flags().StringVarP(&name, "name", "n", "", "Bridge name")
+	bridgeConfigureCmd.Flags().StringVarP(&target, "target", "t", "", "Bridge target name. Valid values are [github,gitlab,gitea,launchpad]")
+	bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.URL, "url", "u", "", "Repository url")
+	bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Owner, "owner", "o", "", "Repository owner")
+	bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Token, "token", "T", "", "Authentication token")
+	bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Project, "project", "p", "", "Repository name")
 }