From 8df38c44954dd906a1a844809e9aee2f6106d100 Mon Sep 17 00:00:00 2001 From: frectonz Date: Wed, 14 Aug 2024 08:03:31 +0300 Subject: [PATCH 1/4] Allow users to provide their token via a stdin prompt --- cmd/configure.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/cmd/configure.go b/cmd/configure.go index dd1c47ed8..c5d1cb6c7 100644 --- a/cmd/configure.go +++ b/cmd/configure.go @@ -1,6 +1,7 @@ package cmd import ( + "bufio" "fmt" "os" "strings" @@ -58,10 +59,25 @@ func runConfigure(configuration config.Config, flags *pflag.FlagSet) error { } // If the command is run 'bare' and we have no token, - // explain how to set the token. + // prompt the user to provide the token. if flags.NFlag() == 0 && cfg.GetString("token") == "" { - tokenURL := config.SettingsURL(cfg.GetString("apibaseurl")) - return fmt.Errorf("There is no token configured. Find your token on %s, and call this command again with --token=.", 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) } // Determine the base API URL. From caebe214bc62ba0e9c85f7ac911d69ece649f0d8 Mon Sep 17 00:00:00 2001 From: frectonz Date: Wed, 14 Aug 2024 08:29:46 +0300 Subject: [PATCH 2/4] Change error message for bare configure --- cmd/configure_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/configure_test.go b/cmd/configure_test.go index df3542328..c3e675de3 100644 --- a/cmd/configure_test.go +++ b/cmd/configure_test.go @@ -32,7 +32,7 @@ func TestBareConfigure(t *testing.T) { err = runConfigure(cfg, flags) if assert.Error(t, err) { - assert.Regexp(t, "no token configured", err.Error()) + assert.Regexp(t, "no token provided", err.Error()) } } From 0063ab13247a2658f7f63de9757e55d17a554522 Mon Sep 17 00:00:00 2001 From: frectonz Date: Wed, 14 Aug 2024 14:06:50 +0300 Subject: [PATCH 3/4] Instruct students on where they can get their token --- cmd/configure.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmd/configure.go b/cmd/configure.go index c5d1cb6c7..85c146962 100644 --- a/cmd/configure.go +++ b/cmd/configure.go @@ -61,6 +61,9 @@ func runConfigure(configuration config.Config, flags *pflag.FlagSet) error { // If the command is run 'bare' and we have no token, // prompt the user to provide the token. if flags.NFlag() == 0 && cfg.GetString("token") == "" { + 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') From a92963141c2ad3c87853b59eb18b853a59ed66fd Mon Sep 17 00:00:00 2001 From: frectonz Date: Thu, 15 Aug 2024 11:48:54 +0300 Subject: [PATCH 4/4] Only prompt students when the `--interactive` flag is set --- cmd/configure.go | 17 ++++++++++++++--- cmd/configure_test.go | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/cmd/configure.go b/cmd/configure.go index 85c146962..d2116f39f 100644 --- a/cmd/configure.go +++ b/cmd/configure.go @@ -58,9 +58,12 @@ func runConfigure(configuration config.Config, flags *pflag.FlagSet) error { return nil } - // If the command is run 'bare' and we have no token, - // prompt the user to provide the token. - if flags.NFlag() == 0 && cfg.GetString("token") == "" { + // 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) @@ -83,6 +86,13 @@ func runConfigure(configuration config.Config, flags *pflag.FlagSet) error { 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") == "" { + tokenURL := config.SettingsURL(cfg.GetString("apibaseurl")) + return fmt.Errorf("There is no token configured. Find your token on %s, and call this command again with --token=.", tokenURL) + } + // Determine the base API URL. baseURL, err := flags.GetString("api") if err != nil { @@ -250,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") } diff --git a/cmd/configure_test.go b/cmd/configure_test.go index c3e675de3..df3542328 100644 --- a/cmd/configure_test.go +++ b/cmd/configure_test.go @@ -32,7 +32,7 @@ func TestBareConfigure(t *testing.T) { err = runConfigure(cfg, flags) if assert.Error(t, err) { - assert.Regexp(t, "no token provided", err.Error()) + assert.Regexp(t, "no token configured", err.Error()) } }