bridge_configure.go

  1package commands
  2
  3import (
  4	"bufio"
  5	"fmt"
  6	"os"
  7	"strconv"
  8	"strings"
  9	"syscall"
 10
 11	"github.com/spf13/cobra"
 12	"golang.org/x/crypto/ssh/terminal"
 13
 14	"github.com/MichaelMure/git-bug/bridge"
 15	"github.com/MichaelMure/git-bug/bridge/core"
 16	"github.com/MichaelMure/git-bug/cache"
 17	"github.com/MichaelMure/git-bug/util/interrupt"
 18)
 19
 20const (
 21	defaultName = "default"
 22)
 23
 24var (
 25	bridgeConfigureName   string
 26	bridgeConfigureTarget string
 27	bridgeParams          core.BridgeParams
 28)
 29
 30func runBridgeConfigure(cmd *cobra.Command, args []string) error {
 31	backend, err := cache.NewRepoCache(repo)
 32	if err != nil {
 33		return err
 34	}
 35	defer backend.Close()
 36	interrupt.RegisterCleaner(backend.Close)
 37
 38	termState, err := terminal.GetState(int(syscall.Stdin))
 39	if err != nil {
 40		return err
 41	}
 42
 43	interrupt.RegisterCleaner(func() error {
 44		return terminal.Restore(int(syscall.Stdin), termState)
 45	})
 46
 47	if bridgeConfigureTarget == "" {
 48		bridgeConfigureTarget, err = promptTarget()
 49		if err != nil {
 50			return err
 51		}
 52	}
 53
 54	if bridgeConfigureName == "" {
 55		bridgeConfigureName, err = promptName()
 56		if err != nil {
 57			return err
 58		}
 59	}
 60
 61	b, err := bridge.NewBridge(backend, bridgeConfigureTarget, bridgeConfigureName)
 62	if err != nil {
 63		return err
 64	}
 65
 66	err = b.Configure(bridgeParams)
 67	if err != nil {
 68		return err
 69	}
 70
 71	fmt.Printf("Successfully configured bridge: %s\n", bridgeConfigureName)
 72	return nil
 73}
 74
 75func promptTarget() (string, error) {
 76	targets := bridge.Targets()
 77
 78	for {
 79		for i, target := range targets {
 80			fmt.Printf("[%d]: %s\n", i+1, target)
 81		}
 82		fmt.Printf("target: ")
 83
 84		line, err := bufio.NewReader(os.Stdin).ReadString('\n')
 85
 86		if err != nil {
 87			return "", err
 88		}
 89
 90		line = strings.TrimRight(line, "\n")
 91
 92		index, err := strconv.Atoi(line)
 93		if err != nil || index <= 0 || index > len(targets) {
 94			fmt.Println("invalid input")
 95			continue
 96		}
 97
 98		return targets[index-1], nil
 99	}
100}
101
102func promptName() (string, error) {
103	fmt.Printf("name [%s]: ", defaultName)
104
105	line, err := bufio.NewReader(os.Stdin).ReadString('\n')
106	if err != nil {
107		return "", err
108	}
109
110	line = strings.TrimRight(line, "\n")
111
112	if line == "" {
113		return defaultName, nil
114	}
115
116	return line, nil
117}
118
119var bridgeConfigureCmd = &cobra.Command{
120	Use:   "configure",
121	Short: "Configure a new bridge.",
122	Long: `	Configure a new bridge by passing flags or/and using interactive terminal prompts. You can avoid all the terminal prompts by passing all the necessary flags to configure your bridge.
123	Repository configuration can be made by passing either the --url flag or the --project and --owner flags. If the three flags are provided git-bug will use --project and --owner flags.
124	Token configuration can be directly passed with the --token flag or in the terminal prompt. If you don't already have one you can use the interactive procedure to generate one.`,
125	Example: `# Interactive example
126[1]: github
127[2]: launchpad-preview
128target: 1
129name [default]: default
130
131Detected projects:
132[1]: github.com/a-hilaly/git-bug
133[2]: github.com/MichaelMure/git-bug
134
135[0]: Another project
136
137Select option: 1
138
139[1]: user provided token
140[2]: interactive token creation
141Select option: 1
142
143You can generate a new token by visiting https://github.com/settings/tokens.
144Choose 'Generate new token' and set the necessary access scope for your repository.
145
146The access scope depend on the type of repository.
147Public:
148	- 'public_repo': to be able to read public repositories
149Private:
150	- 'repo'       : to be able to read private repositories
151
152Enter token: 87cf5c03b64029f18ea5f9ca5679daa08ccbd700
153Successfully configured bridge: default
154
155# For Github
156git bug bridge configure \
157    --name=default \
158    --target=github \
159    --owner=$(OWNER) \
160    --project=$(PROJECT) \
161    --token=$(TOKEN)
162
163# For Launchpad
164git bug bridge configure \
165    --name=default \
166    --target=launchpad-preview \
167    --url=https://bugs.launchpad.net/ubuntu/`,
168	PreRunE: loadRepo,
169	RunE:    runBridgeConfigure,
170}
171
172func init() {
173	bridgeCmd.AddCommand(bridgeConfigureCmd)
174	bridgeConfigureCmd.Flags().StringVarP(&bridgeConfigureName, "name", "n", "", "A distinctive name to identify the bridge")
175	bridgeConfigureCmd.Flags().StringVarP(&bridgeConfigureTarget, "target", "t", "",
176		fmt.Sprintf("The target of the bridge. Valid values are [%s]", strings.Join(bridge.Targets(), ",")))
177	bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.URL, "url", "u", "", "The URL of the target repository")
178	bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Owner, "owner", "o", "", "The owner of the target repository")
179	bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Token, "token", "T", "", "The authentication token for the API")
180	bridgeConfigureCmd.Flags().StringVarP(&bridgeParams.Project, "project", "p", "", "The name of the target repository")
181	bridgeConfigureCmd.Flags().SortFlags = false
182}