From 43bda202fa347e6893671b05376c0e4fcb9196f4 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Mon, 24 Sep 2018 15:25:57 +0200 Subject: commands: add the "bridge" and "bridge configure" commands --- commands/bridge_configure.go | 95 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 commands/bridge_configure.go (limited to 'commands/bridge_configure.go') diff --git a/commands/bridge_configure.go b/commands/bridge_configure.go new file mode 100644 index 00000000..564affcc --- /dev/null +++ b/commands/bridge_configure.go @@ -0,0 +1,95 @@ +package commands + +import ( + "bufio" + "fmt" + "os" + "strings" + + "github.com/MichaelMure/git-bug/bridge" + "github.com/MichaelMure/git-bug/bridge/core" + "github.com/MichaelMure/git-bug/cache" + "github.com/spf13/cobra" +) + +func runBridgeConfigure(cmd *cobra.Command, args []string) error { + backend, err := cache.NewRepoCache(repo) + if err != nil { + return err + } + defer backend.Close() + + target, err := promptTarget() + if err != nil { + return err + } + + name, err := promptName() + if err != nil { + return err + } + + b, err := core.NewBridge(backend, target, name) + if err != nil { + return err + } + + err = b.Configure() + if err != nil { + return err + } + + return nil +} + +func promptTarget() (string, error) { + targets := bridge.Targets() + + for { + fmt.Printf("target (%s): ", strings.Join(targets, ",")) + + line, err := bufio.NewReader(os.Stdin).ReadString('\n') + if err != nil { + return "", err + } + + line = strings.TrimRight(line, "\n") + + for _, t := range targets { + if t == line { + return t, nil + } + } + + fmt.Println("invalid target") + } +} + +func promptName() (string, error) { + defaultName := "default" + + fmt.Printf("name [%s]: ", defaultName) + + line, err := bufio.NewReader(os.Stdin).ReadString('\n') + if err != nil { + return "", err + } + + line = strings.TrimRight(line, "\n") + + if line == "" { + return defaultName, nil + } + + return line, nil +} + +var bridgeConfigureCmd = &cobra.Command{ + Use: "configure", + Short: "Configure a new bridge", + RunE: runBridgeConfigure, +} + +func init() { + bridgeCmd.AddCommand(bridgeConfigureCmd) +} -- cgit