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
"github.com/spf13/cobra"
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"
2019-12-08 23:15:06 +03:00
"github.com/MichaelMure/git-bug/bridge/core/auth"
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-12-08 23:15:06 +03:00
bridgeConfigureName string
bridgeConfigureTarget string
bridgeConfigureParams core . BridgeParams
bridgeConfigureToken string
bridgeConfigureTokenStdin bool
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-12-08 23:15:06 +03:00
if ( bridgeConfigureTokenStdin || bridgeConfigureToken != "" || bridgeConfigureParams . CredPrefix != "" ) &&
2019-11-23 21:21:14 +03:00
( bridgeConfigureName == "" || bridgeConfigureTarget == "" ) {
2019-12-08 23:15:06 +03:00
return fmt . Errorf ( "you must provide a bridge name and target to configure a bridge with a credential" )
}
// early fail
if bridgeConfigureParams . CredPrefix != "" {
if _ , err := auth . LoadWithPrefix ( repo , bridgeConfigureParams . CredPrefix ) ; err != nil {
return err
}
}
switch {
case bridgeConfigureTokenStdin :
reader := bufio . NewReader ( os . Stdin )
token , err := reader . ReadString ( '\n' )
if err != nil {
return fmt . Errorf ( "reading from stdin: %v" , err )
}
bridgeConfigureParams . TokenRaw = strings . TrimSpace ( token )
case bridgeConfigureToken != "" :
bridgeConfigureParams . TokenRaw = bridgeConfigureToken
2019-08-27 01:32:30 +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-12-08 23:15:06 +03:00
err = b . Configure ( bridgeConfigureParams )
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
}
2019-11-25 17:08:48 +03:00
line = strings . TrimSpace ( line )
2018-09-24 16:25:57 +03:00
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-12-08 23:15:06 +03:00
func promptName ( repo repository . RepoConfig ) ( string , error ) {
2019-11-19 22:20:19 +03:00
defaultExist := core . BridgeExist ( repo , defaultName )
2019-07-07 13:15:01 +03:00
for {
2019-11-19 22:20:19 +03:00
if defaultExist {
fmt . Printf ( "name: " )
} else {
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-11-25 17:08:48 +03:00
line = strings . TrimSpace ( line )
2018-09-24 16:25:57 +03:00
2019-07-07 13:15:01 +03:00
name := line
2019-11-19 22:20:19 +03:00
if defaultExist && name == "" {
continue
}
2019-07-07 13:15:01 +03:00
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." ,
2020-02-15 04:55:19 +03:00
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-29 21:48:51 +03:00
Example : ` # Interactive example
[ 1 ] : github
2020-02-15 04:55:19 +03:00
[ 2 ] : gitlab
[ 3 ] : jira
[ 4 ] : launchpad - preview
2019-05-29 21:48:51 +03:00
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 : 87 cf5c03b64029f18ea5f9ca5679daa08ccbd700
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 \
2019-08-24 15:18:22 +03:00
-- name = default \
2019-08-24 14:55:49 +03:00
-- target = github \
-- url = https : //github.com/michaelmure/git-bug \
-- token = $ ( TOKEN ) ` ,
2019-12-26 00:55:53 +03:00
PreRunE : loadRepo ,
2018-10-17 21:38:10 +03:00
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 ( ) , "," ) ) )
2020-02-15 04:55:19 +03:00
bridgeConfigureCmd . Flags ( ) . StringVarP ( & bridgeConfigureParams . URL , "url" , "u" , "" , "The URL of the remote repository" )
bridgeConfigureCmd . Flags ( ) . StringVarP ( & bridgeConfigureParams . BaseURL , "base-url" , "b" , "" , "The base URL of your remote issue tracker" )
bridgeConfigureCmd . Flags ( ) . StringVarP ( & bridgeConfigureParams . Login , "login" , "l" , "" , "The login on your remote issue tracker" )
bridgeConfigureCmd . Flags ( ) . StringVarP ( & bridgeConfigureParams . CredPrefix , "credential" , "c" , "" , "The identifier or prefix of an already known credential for your remote issue tracker (see \"git-bug bridge auth\")" )
bridgeConfigureCmd . Flags ( ) . StringVar ( & bridgeConfigureToken , "token" , "" , "A raw authentication token for the remote issue tracker" )
2019-12-08 23:15:06 +03:00
bridgeConfigureCmd . Flags ( ) . BoolVar ( & bridgeConfigureTokenStdin , "token-stdin" , false , "Will read the token from stdin and ignore --token" )
2020-02-15 04:55:19 +03:00
bridgeConfigureCmd . Flags ( ) . StringVarP ( & bridgeConfigureParams . Owner , "owner" , "o" , "" , "The owner of the remote repository" )
bridgeConfigureCmd . Flags ( ) . StringVarP ( & bridgeConfigureParams . Project , "project" , "p" , "" , "The name of the remote repository" )
2019-05-26 14:10:47 +03:00
bridgeConfigureCmd . Flags ( ) . SortFlags = false
2018-09-24 16:25:57 +03:00
}