Skip to content

Commit

Permalink
improve ReadInput method
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Aug 1, 2024
1 parent 3b0f2b0 commit bf2377b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
7 changes: 4 additions & 3 deletions config/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ var hookSaveConfiguration = func(fn func(config *Configuration) error) func(conf
return fn
}

var stdin io.Reader = os.Stdin

func loadConfiguration() (*Configuration, error) {
return hookLoadConfiguration(LoadConfiguration)(GetConfigPath() + "/" + configFile)
}

func NewConfigureCommand() *cli.Command {

c := &cli.Command{
Name: "configure",
Short: i18n.T(
Expand Down Expand Up @@ -287,14 +288,14 @@ func configureOIDC(w io.Writer, cp *Profile) error {

func ReadInput(defaultValue string) string {
var s string
scanner := bufio.NewScanner(os.Stdin)
scanner := bufio.NewScanner(stdin)
if scanner.Scan() {
s = scanner.Text()
}
if s == "" {
return defaultValue
}
return s
return strings.TrimSpace(s)
}

func MosaicString(s string, lastChars int) string {
Expand Down
13 changes: 13 additions & 0 deletions config/configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package config

import (
"bytes"
"os"
"runtime"
"strings"
"testing"
Expand Down Expand Up @@ -314,7 +315,19 @@ func TestConfigureOIDC(t *testing.T) {
}

func TestReadInput(t *testing.T) {
defer func() {
stdin = os.Stdin
}()
// read empty string, return default value
stdin = strings.NewReader("")
assert.Equal(t, "default", ReadInput("default"))
// read input, return input
stdin = strings.NewReader("input from stdion\n")
assert.Equal(t, "input from stdion", ReadInput("default"))

// read input with spaces
stdin = strings.NewReader("input from stdion \n")
assert.Equal(t, "input from stdion", ReadInput("default"))
}

func TestMosaicString(t *testing.T) {
Expand Down

0 comments on commit bf2377b

Please sign in to comment.