Skip to content

Commit

Permalink
cmd/utils: Fix qsctl hang on non-interactive environment (#292)
Browse files Browse the repository at this point in the history
* cmd/utils: Add check before turning into interactively setup

* cmd/init: Add more debug logs, apply review comment

* Apply review comment

* Fix configuredByEnv logic

Signed-off-by: Xuanwo <[email protected]>

Co-authored-by: Xuanwo <[email protected]>
  • Loading branch information
Prnyself and Xuanwo authored Jun 3, 2020
1 parent beaf75e commit 9e9c869
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
51 changes: 36 additions & 15 deletions cmd/qsctl/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,29 +113,44 @@ func initConfig() (err error) {
// try to read config from path set above
err = viper.ReadInConfig()
if err == nil {
return
log.Debugf("Load config success from [%s]: %v", viper.ConfigFileUsed(), viper.AllSettings())
return nil
}
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
i18n.Printf("Load config failed [%v]", err)
return
return err
}

// if env not set, start interactive setup
if viper.GetString(constants.ConfigAccessKeyID) == "" && viper.GetString(constants.ConfigSecretAccessKey) == "" {
i18n.Printf("AccessKey and SecretKey not found. Please setup your config now, or exit and setup manually.")
fileName, err := utils.SetupConfigInteractive()
if err != nil {
return fmt.Errorf("setup config failed [%v], please try again", err)
}
i18n.Printf("Your config has been set to <%v>. You can still modify it manually.", fileName)
viper.SetConfigFile(fileName)
if err = viper.ReadInConfig(); err != nil {
return err
}
} else {
// if env set, get config from env
if configuredByEnv() {
i18n.Printf("Config not loaded, use default and environment value instead.")
log.Debug("Config not loaded, use default and environment value instead.")
return nil
}

// if env not set, try to start interactive setup
// if not run interactively, return error
if !utils.IsInteractiveEnable() {
log.Errorf("qsctl not run interactively, and cannot load config with err: [%v]", err)
return err
}

i18n.Printf("AccessKey and SecretKey not found. Please setup your config now, or exit and setup manually.")
log.Debug("AccessKey and SecretKey not found. Ready to turn into setup config interactively.")
var fileName string
fileName, err = utils.SetupConfigInteractive()
if err != nil {
return fmt.Errorf("setup config failed [%v], please try again", err)
}

i18n.Printf("Your config has been set to <%v>. You can still modify it manually.", fileName)
viper.SetConfigFile(fileName)
log.Debugf("Config was set to [%s]", fileName)
// read in config again after interactively setup config file
if err = viper.ReadInConfig(); err != nil {
log.Errorf("Read config after interactively setup failed: [%v]", err)
return err
}
return nil
}

Expand All @@ -159,3 +174,9 @@ func initGlobalFlag() {
func silenceUsage(c *cobra.Command) {
c.SilenceUsage = true
}

// configuredByEnv returns true if either ak or sk set
func configuredByEnv() bool {
return viper.GetString(constants.ConfigAccessKeyID) != "" &&
viper.GetString(constants.ConfigSecretAccessKey) != ""
}
7 changes: 7 additions & 0 deletions cmd/utils/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"

"github.com/AlecAivazis/survey/v2"
"golang.org/x/crypto/ssh/terminal"
"gopkg.in/yaml.v2"

"github.com/qingstor/qsctl/v2/constants"
Expand Down Expand Up @@ -139,3 +140,9 @@ func SetupConfigInteractive() (fileName string, err error) {
}
return fileName, nil
}

// IsInteractiveEnable checks whether qsctl run interactively by
// checking stdin and stdout is terminal or not
func IsInteractiveEnable() bool {
return terminal.IsTerminal(int(os.Stdout.Fd())) && terminal.IsTerminal(int(os.Stdin.Fd()))
}

0 comments on commit 9e9c869

Please sign in to comment.