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"
|
|
|
|
|
2019-05-24 22:03:31 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bridge/core"
|
|
|
|
|
2018-09-24 16:25:57 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bridge"
|
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
2018-10-25 00:36:39 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/interrupt"
|
2018-09-24 16:25:57 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2019-05-24 22:03:31 +03:00
|
|
|
const (
|
|
|
|
defaultName = "default"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
name string
|
|
|
|
target string
|
|
|
|
bridgeParams core.BridgeParams
|
|
|
|
)
|
|
|
|
|
2018-09-24 16:25:57 +03:00
|
|
|
func runBridgeConfigure(cmd *cobra.Command, args []string) error {
|
|
|
|
backend, err := cache.NewRepoCache(repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer backend.Close()
|
2018-10-25 00:36:39 +03:00
|
|
|
interrupt.RegisterCleaner(backend.Close)
|
2018-09-24 16:25:57 +03:00
|
|
|
|
2019-05-24 22:03:31 +03:00
|
|
|
if target == "" {
|
|
|
|
target, err = promptTarget()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-24 16:25:57 +03:00
|
|
|
}
|
|
|
|
|
2019-05-24 22:03:31 +03:00
|
|
|
if name == "" {
|
|
|
|
name, err = promptName()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-24 16:25:57 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-05-24 22:03:31 +03:00
|
|
|
err = b.Configure(bridgeParams)
|
2018-09-24 16:25:57 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-24 22:03:31 +03:00
|
|
|
fmt.Println("successfully configured bridge")
|
2018-09-24 16:25:57 +03:00
|
|
|
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) {
|
|
|
|
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",
|
2019-02-24 16:46:08 +03:00
|
|
|
Short: "Configure a new bridge.",
|
2018-10-17 21:38:10 +03:00
|
|
|
PreRunE: loadRepo,
|
|
|
|
RunE: runBridgeConfigure,
|
2018-09-24 16:25:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
bridgeCmd.AddCommand(bridgeConfigureCmd)
|
2019-05-26 13:35:53 +03:00
|
|
|
bridgeConfigureCmd.Flags().StringVarP(&name, "name", "n", "", "A distinctive name to identify the bridge")
|
|
|
|
bridgeConfigureCmd.Flags().StringVarP(&target, "target", "t", "",
|
|
|
|
fmt.Sprintf("The target of the bridge. Valid values are [%s]", strings.Join(bridge.Targets(), ",")))
|
|
|
|
bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.URL, "url", "u", "", "The URL of the target repository")
|
|
|
|
bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Owner, "owner", "o", "", "The owner of the target repository")
|
|
|
|
bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Token, "token", "T", "", "The authentication token for the API")
|
|
|
|
bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Project, "project", "p", "", "The name of the target repository")
|
2018-09-24 16:25:57 +03:00
|
|
|
}
|