2018-09-24 16:25:57 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2018-10-03 22:05:19 +03:00
|
|
|
"strconv"
|
2018-09-24 16:25:57 +03:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/MichaelMure/git-bug/bridge"
|
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
2018-10-24 00:01:00 +03:00
|
|
|
"github.com/MichaelMure/git-bug/cleaner"
|
2018-09-24 16:25:57 +03:00
|
|
|
"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()
|
2018-10-24 00:01:00 +03:00
|
|
|
cleaner.Register(backend.Close)
|
2018-09-24 16:25:57 +03:00
|
|
|
|
|
|
|
target, err := promptTarget()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
name, err := promptName()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-24 17:24:38 +03:00
|
|
|
b, err := bridge.NewBridge(backend, target, name)
|
2018-09-24 16:25:57 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = b.Configure()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func promptTarget() (string, error) {
|
|
|
|
targets := bridge.Targets()
|
|
|
|
|
|
|
|
for {
|
2018-10-03 22:05:19 +03:00
|
|
|
for i, target := range targets {
|
|
|
|
fmt.Printf("[%d]: %s\n", i+1, target)
|
|
|
|
}
|
|
|
|
fmt.Printf("target: ")
|
2018-09-24 16:25:57 +03:00
|
|
|
|
|
|
|
line, err := bufio.NewReader(os.Stdin).ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
line = strings.TrimRight(line, "\n")
|
|
|
|
|
2018-10-03 22:05:19 +03:00
|
|
|
index, err := strconv.Atoi(line)
|
|
|
|
if err != nil || index <= 0 || index > len(targets) {
|
|
|
|
fmt.Println("invalid input")
|
|
|
|
continue
|
2018-09-24 16:25:57 +03:00
|
|
|
}
|
|
|
|
|
2018-10-03 22:05:19 +03:00
|
|
|
return targets[index-1], nil
|
2018-09-24 16:25:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
2018-10-17 21:38:10 +03:00
|
|
|
Use: "configure",
|
|
|
|
Short: "Configure a new bridge",
|
|
|
|
PreRunE: loadRepo,
|
|
|
|
RunE: runBridgeConfigure,
|
2018-09-24 16:25:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
bridgeCmd.AddCommand(bridgeConfigureCmd)
|
|
|
|
}
|