From 118207dbf6214fe07dc7f897d0accc3bdac8aa6e Mon Sep 17 00:00:00 2001 From: vince Date: Thu, 16 Jul 2020 17:25:37 +0800 Subject: [PATCH] Move reflect methods into bridge This commit removes the reflect methods in the command. --- bridge/bridges.go | 4 ++++ bridge/core/bridge.go | 20 ++++++++++++++++++++ commands/bridge_configure.go | 9 --------- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/bridge/bridges.go b/bridge/bridges.go index d74a58fa7b2967c6e4aa834567f8c8e550c86ec4..7bd0e76d74d06f7e1bbe83528c9755cf7c7287d3 100644 --- a/bridge/bridges.go +++ b/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. diff --git a/bridge/core/bridge.go b/bridge/core/bridge.go index 5b75fec508922dd348c129b23104e8b4c1eb8135..0ecc39528faa1ff7e6faba0a6073c1dbdad31afa 100644 --- a/bridge/core/bridge.go +++ b/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] diff --git a/commands/bridge_configure.go b/commands/bridge_configure.go index 278ecc74a652b277002dcba4b81538f000a4b964..0f0124bd1fed53e29f1a9848729d76065feb74ea 100644 --- a/commands/bridge_configure.go +++ b/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",