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-06-05 02:42:36 +03:00
|
|
|
"syscall"
|
2018-09-24 16:25:57 +03:00
|
|
|
|
2019-06-05 02:42:36 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
2019-05-24 22:03:31 +03:00
|
|
|
|
2018-09-24 16:25:57 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bridge"
|
2019-06-05 02:42:36 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bridge/core"
|
2018-09-24 16:25:57 +03:00
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
2019-07-07 13:15:01 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
2018-10-25 00:36:39 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/interrupt"
|
2018-09-24 16:25:57 +03:00
|
|
|
)
|
|
|
|
|
2019-05-24 22:03:31 +03:00
|
|
|
const (
|
|
|
|
defaultName = "default"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-05-29 21:48:51 +03:00
|
|
|
bridgeConfigureName string
|
|
|
|
bridgeConfigureTarget string
|
|
|
|
bridgeParams core.BridgeParams
|
2019-05-24 22:03:31 +03:00
|
|
|
)
|
|
|
|
|
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-06-05 01:45:34 +03:00
|
|
|
termState, err := terminal.GetState(int(syscall.Stdin))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-05 02:42:36 +03:00
|
|
|
|
2019-06-05 01:45:34 +03:00
|
|
|
interrupt.RegisterCleaner(func() error {
|
|
|
|
return terminal.Restore(int(syscall.Stdin), termState)
|
|
|
|
})
|
2019-06-05 02:42:36 +03:00
|
|
|
|
2019-05-29 21:48:51 +03:00
|
|
|
if bridgeConfigureTarget == "" {
|
|
|
|
bridgeConfigureTarget, err = promptTarget()
|
2019-05-24 22:03:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-24 16:25:57 +03:00
|
|
|
}
|
|
|
|
|
2019-05-29 21:48:51 +03:00
|
|
|
if bridgeConfigureName == "" {
|
2019-07-07 13:15:01 +03:00
|
|
|
bridgeConfigureName, err = promptName(repo)
|
2019-05-24 22:03:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-24 16:25:57 +03:00
|
|
|
}
|
|
|
|
|
2019-05-29 21:48:51 +03:00
|
|
|
b, err := bridge.NewBridge(backend, bridgeConfigureTarget, bridgeConfigureName)
|
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-29 21:48:51 +03:00
|
|
|
fmt.Printf("Successfully configured bridge: %s\n", bridgeConfigureName)
|
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')
|
2019-06-05 02:42:36 +03:00
|
|
|
|
2018-09-24 16:25:57 +03:00
|
|
|
if err != nil {
|
2019-06-07 02:28:52 +03:00
|
|
|
return "", err
|
2018-09-24 16:25:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-07 13:15:01 +03:00
|
|
|
func promptName(repo repository.RepoCommon) (string, error) {
|
|
|
|
for {
|
|
|
|
fmt.Printf("name [%s]: ", defaultName)
|
2018-09-24 16:25:57 +03:00
|
|
|
|
2019-07-07 13:15:01 +03:00
|
|
|
line, err := bufio.NewReader(os.Stdin).ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-09-24 16:25:57 +03:00
|
|
|
|
2019-07-07 13:15:01 +03:00
|
|
|
line = strings.TrimRight(line, "\n")
|
2018-09-24 16:25:57 +03:00
|
|
|
|
2019-07-07 13:15:01 +03:00
|
|
|
name := line
|
|
|
|
if name == "" {
|
|
|
|
name = defaultName
|
|
|
|
}
|
2018-09-24 16:25:57 +03:00
|
|
|
|
2019-07-07 13:15:01 +03:00
|
|
|
if !core.BridgeExist(repo, name) {
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("a bridge with the same name already exist")
|
|
|
|
}
|
2018-09-24 16:25:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var bridgeConfigureCmd = &cobra.Command{
|
2019-05-27 20:15:53 +03:00
|
|
|
Use: "configure",
|
|
|
|
Short: "Configure a new bridge.",
|
|
|
|
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.
|
2019-05-27 21:26:36 +03:00
|
|
|
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.
|
2019-05-29 21:48:51 +03:00
|
|
|
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.`,
|
|
|
|
Example: `# Interactive example
|
|
|
|
[1]: github
|
|
|
|
[2]: launchpad-preview
|
|
|
|
target: 1
|
|
|
|
name [default]: default
|
|
|
|
|
|
|
|
Detected projects:
|
|
|
|
[1]: github.com/a-hilaly/git-bug
|
|
|
|
[2]: github.com/MichaelMure/git-bug
|
|
|
|
|
|
|
|
[0]: Another project
|
|
|
|
|
|
|
|
Select option: 1
|
|
|
|
|
2019-06-05 02:42:36 +03:00
|
|
|
[1]: user provided token
|
|
|
|
[2]: interactive token creation
|
|
|
|
Select option: 1
|
2019-05-29 21:48:51 +03:00
|
|
|
|
|
|
|
You can generate a new token by visiting https://github.com/settings/tokens.
|
|
|
|
Choose 'Generate new token' and set the necessary access scope for your repository.
|
|
|
|
|
|
|
|
The access scope depend on the type of repository.
|
|
|
|
Public:
|
|
|
|
- 'public_repo': to be able to read public repositories
|
|
|
|
Private:
|
|
|
|
- 'repo' : to be able to read private repositories
|
|
|
|
|
2019-06-05 02:42:36 +03:00
|
|
|
Enter token: 87cf5c03b64029f18ea5f9ca5679daa08ccbd700
|
2019-05-29 21:48:51 +03:00
|
|
|
Successfully configured bridge: default
|
|
|
|
|
2019-08-24 14:55:49 +03:00
|
|
|
# For GitHub
|
2019-05-27 20:15:53 +03:00
|
|
|
git bug bridge configure \
|
|
|
|
--name=default \
|
|
|
|
--target=github \
|
|
|
|
--owner=$(OWNER) \
|
|
|
|
--project=$(PROJECT) \
|
|
|
|
--token=$(TOKEN)
|
|
|
|
|
|
|
|
# For Launchpad
|
|
|
|
git bug bridge configure \
|
|
|
|
--name=default \
|
|
|
|
--target=launchpad-preview \
|
2019-08-24 14:55:49 +03:00
|
|
|
--url=https://bugs.launchpad.net/ubuntu/
|
|
|
|
|
|
|
|
# For Gitlab
|
|
|
|
git bug bridge configure \
|
|
|
|
--name=mybridge \
|
|
|
|
--target=github \
|
|
|
|
--url=https://github.com/michaelmure/git-bug \
|
|
|
|
--token=$(TOKEN)`,
|
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-29 21:48:51 +03:00
|
|
|
bridgeConfigureCmd.Flags().StringVarP(&bridgeConfigureName, "name", "n", "", "A distinctive name to identify the bridge")
|
|
|
|
bridgeConfigureCmd.Flags().StringVarP(&bridgeConfigureTarget, "target", "t", "",
|
2019-05-26 13:35:53 +03:00
|
|
|
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")
|
2019-05-26 14:10:47 +03:00
|
|
|
bridgeConfigureCmd.Flags().SortFlags = false
|
2018-09-24 16:25:57 +03:00
|
|
|
}
|