make token visible in configuration process

Amine Hilaly created

validate token
global fixes

Change summary

bridge/github/config.go      | 28 ++++++++++++++++------------
commands/bridge_configure.go | 19 ++++++++++++-------
2 files changed, 28 insertions(+), 19 deletions(-)

Detailed changes

bridge/github/config.go 🔗

@@ -197,8 +197,8 @@ func randomFingerprint() string {
 func promptTokenOptions(owner, project string) (string, error) {
 	for {
 		fmt.Println()
-		fmt.Println("[0]: user provided token")
-		fmt.Println("[1]: interactive token creation")
+		fmt.Println("[1]: user provided token")
+		fmt.Println("[2]: interactive token creation")
 		fmt.Print("Select option: ")
 
 		line, err := bufio.NewReader(os.Stdin).ReadString('\n')
@@ -210,12 +210,12 @@ func promptTokenOptions(owner, project string) (string, error) {
 		line = strings.TrimRight(line, "\n")
 
 		index, err := strconv.Atoi(line)
-		if err != nil || (index != 0 && index != 1) {
+		if err != nil || (index != 1 && index != 2) {
 			fmt.Println("invalid input")
 			continue
 		}
 
-		if index == 0 {
+		if index == 1 {
 			return promptToken()
 		}
 
@@ -234,21 +234,25 @@ func promptToken() (string, error) {
 	fmt.Println("  - 'repo'       : to be able to read private repositories")
 	fmt.Println()
 
+	re, err := regexp.Compile(`^[a-zA-Z0-9]{40}`)
+	if err != nil {
+		panic("regexp compile:" + err.Error())
+	}
+
 	for {
 		fmt.Print("Enter token: ")
 
-		byteToken, err := terminal.ReadPassword(int(syscall.Stdin))
-		fmt.Println()
-
+		line, err := bufio.NewReader(os.Stdin).ReadString('\n')
 		if err != nil {
 			return "", err
 		}
 
-		if len(byteToken) > 0 {
-			return string(byteToken), nil
+		token := strings.TrimRight(line, "\n")
+		if re.MatchString(token) {
+			return token, nil
 		}
 
-		fmt.Println("token is empty")
+		fmt.Println("token is invalid")
 	}
 }
 
@@ -534,8 +538,8 @@ func prompt2FA() (string, error) {
 
 func promptProjectVisibility() (bool, error) {
 	for {
-		fmt.Println("[0]: public")
-		fmt.Println("[1]: private")
+		fmt.Println("[1]: public")
+		fmt.Println("[2]: private")
 		fmt.Print("repository visibility: ")
 
 		line, err := bufio.NewReader(os.Stdin).ReadString('\n')

commands/bridge_configure.go 🔗

@@ -6,13 +6,15 @@ import (
 	"os"
 	"strconv"
 	"strings"
+	"syscall"
 
-	"github.com/MichaelMure/git-bug/bridge/core"
+	"github.com/spf13/cobra"
+	"golang.org/x/crypto/ssh/terminal"
 
 	"github.com/MichaelMure/git-bug/bridge"
+	"github.com/MichaelMure/git-bug/bridge/core"
 	"github.com/MichaelMure/git-bug/cache"
 	"github.com/MichaelMure/git-bug/util/interrupt"
-	"github.com/spf13/cobra"
 )
 
 const (
@@ -37,9 +39,11 @@ func runBridgeConfigure(cmd *cobra.Command, args []string) error {
 	if err != nil {
 		return err
 	}
+
 	interrupt.RegisterCleaner(func() error {
 		return terminal.Restore(int(syscall.Stdin), termState)
 	})
+
 	if bridgeConfigureTarget == "" {
 		bridgeConfigureTarget, err = promptTarget()
 		if err != nil {
@@ -78,8 +82,9 @@ func promptTarget() (string, error) {
 		fmt.Printf("target: ")
 
 		line, err := bufio.NewReader(os.Stdin).ReadString('\n')
+
 		if err != nil {
-			return "", err
+			return "", fmt.Errorf("got err: '%v' '%v'", line, err)
 		}
 
 		line = strings.TrimRight(line, "\n")
@@ -131,9 +136,9 @@ Detected projects:
 
 Select option: 1
 
-[0]: user provided token
-[1]: interactive token creation
-Select option: 0
+[1]: user provided token
+[2]: interactive token creation
+Select option: 1
 
 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.
@@ -144,7 +149,7 @@ Public:
 Private:
 	- 'repo'       : to be able to read private repositories
 
-Enter token: 
+Enter token: 87cf5c03b64029f18ea5f9ca5679daa08ccbd700
 Successfully configured bridge: default
 
 # For Github