diff options
author | Michael Muré <batolettre@gmail.com> | 2018-09-24 15:25:57 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-09-24 15:25:57 +0200 |
commit | 43bda202fa347e6893671b05376c0e4fcb9196f4 (patch) | |
tree | a542a6b7f593335df9950df16997c9b220bf4520 /commands/bridge_configure.go | |
parent | 5e8fb7ec5058b816ccc9622f52dbf1777254656c (diff) | |
download | git-bug-43bda202fa347e6893671b05376c0e4fcb9196f4.tar.gz |
commands: add the "bridge" and "bridge configure" commands
Diffstat (limited to 'commands/bridge_configure.go')
-rw-r--r-- | commands/bridge_configure.go | 95 |
1 files changed, 95 insertions, 0 deletions
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) +} |