Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to provide their token via a stdin prompt #1153

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions cmd/configure.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"bufio"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -57,6 +58,34 @@ func runConfigure(configuration config.Config, flags *pflag.FlagSet) error {
return nil
}

// Interactive configuration.
interactive, err := flags.GetBool("interactive")
if err != nil {
return err
}
if interactive {
tokenURL := config.SettingsURL(cfg.GetString("apibaseurl"))
fmt.Println("Find your token on", tokenURL)

reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter your token: ")
token, err := reader.ReadString('\n')
token = strings.TrimSpace(token)

// If we got an EOF print a new line, so that the
// next print can be on a new line.
if err != nil {
fmt.Println()
}

if err != nil || token == "" {
tokenURL := config.SettingsURL(cfg.GetString("apibaseurl"))
return fmt.Errorf("There is no token provided. Find your token on %s, and call this command again.", tokenURL)
}

flags.Set("token", token)
}

// If the command is run 'bare' and we have no token,
// explain how to set the token.
if flags.NFlag() == 0 && cfg.GetString("token") == "" {
Expand Down Expand Up @@ -231,6 +260,7 @@ func setupConfigureFlags(flags *pflag.FlagSet) {
flags.StringP("workspace", "w", "", "directory for exercism exercises")
flags.StringP("api", "a", "", "API base url")
flags.BoolP("show", "s", false, "show the current configuration")
flags.BoolP("interactive", "i", false, "set configuration values with interactive prompts")
flags.BoolP("no-verify", "", false, "skip online token authorization check")
}

Expand Down
Loading