Move reflect methods into bridge

vince created

This commit removes the reflect methods in the command.

Change summary

bridge/bridges.go            |  4 ++++
bridge/core/bridge.go        | 20 ++++++++++++++++++++
commands/bridge_configure.go |  9 ---------
3 files changed, 24 insertions(+), 9 deletions(-)

Detailed changes

bridge/bridges.go 🔗

@@ -23,6 +23,10 @@ func Targets() []string {
 	return core.Targets()
 }
 
+func ValidParams(target string) ([]string, error) {
+	return core.ValidParams(target)
+}
+
 // LoginMetaKey return the metadata key used to store the remote bug-tracker login
 // on the user identity. The corresponding value is used to match identities and
 // credentials.

bridge/core/bridge.go 🔗

@@ -29,6 +29,7 @@ const (
 )
 
 var bridgeImpl map[string]reflect.Type
+var bridgeValidParams map[string][]string
 var bridgeLoginMetaKey map[string]string
 
 // Bridge is a wrapper around a BridgeImpl that will bind low-level
@@ -54,6 +55,16 @@ func Register(impl BridgeImpl) {
 	}
 	bridgeImpl[impl.Target()] = reflect.TypeOf(impl).Elem()
 	bridgeLoginMetaKey[impl.Target()] = impl.LoginMetaKey()
+
+	paramMap := bridgeImpl[impl.Target()].(BridgeImpl).ValidParams()
+	params := make([]string, len(paramMap))
+
+	i := 0
+	for k := range paramMap {
+		params[i] = k
+		i++
+	}
+	bridgeValidParams[impl.Target()] = params
 }
 
 // Targets return all known bridge implementation target
@@ -74,6 +85,15 @@ func TargetTypes() map[string]reflect.Type {
 	return bridgeImpl
 }
 
+func ValidParams(target string) ([]string, error) {
+	validParams, ok := bridgeValidParams[target]
+	if !ok {
+		return nil, fmt.Errorf("unknown bridge target %v", target)
+	}
+
+	return validParams, nil
+}
+
 // TargetExist return true if the given target has a bridge implementation
 func TargetExist(target string) bool {
 	_, ok := bridgeImpl[target]

commands/bridge_configure.go 🔗

@@ -4,7 +4,6 @@ import (
 	"bufio"
 	"fmt"
 	"os"
-	"reflect"
 	"strconv"
 	"strings"
 
@@ -29,14 +28,6 @@ func newBridgeConfigureCommand() *cobra.Command {
 	options := bridgeConfigureOptions{}
 
 	targetDocs := ""
-	for _, v := range core.TargetTypes() {
-		targetDocs += fmt.Sprintf("# For %s:\ngit bug bridge configure \\\n", strings.Title(strings.Split(v.String(), ".")[0]))
-		b := reflect.New(v).Interface().(core.BridgeImpl)
-		for param := range b.ValidParams() {
-			targetDocs += fmt.Sprintf("    --%s=Placeholder Text \\\n", strings.ToLower(param))
-		}
-		targetDocs += "\n"
-	}
 
 	cmd := &cobra.Command{
 		Use:   "configure",