-
Notifications
You must be signed in to change notification settings - Fork 236
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
Put os.Stdin size to NewReaderSize instead NewReader #441
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ package cmd | |
import ( | ||
"bufio" | ||
"fmt" | ||
"io/ioutil" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
|
@@ -31,8 +31,8 @@ func newContextCommand(config *settings.Config) *cobra.Command { | |
} | ||
|
||
listCommand := &cobra.Command{ | ||
Short: "List all contexts", | ||
Use: "list <vcs-type> <org-name>", | ||
Short: "List all contexts", | ||
Use: "list <vcs-type> <org-name>", | ||
PreRunE: initClient, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return listContexts(cl, args[0], args[1]) | ||
|
@@ -41,8 +41,8 @@ func newContextCommand(config *settings.Config) *cobra.Command { | |
} | ||
|
||
showContextCommand := &cobra.Command{ | ||
Short: "Show a context", | ||
Use: "show <vcs-type> <org-name> <context-name>", | ||
Short: "Show a context", | ||
Use: "show <vcs-type> <org-name> <context-name>", | ||
PreRunE: initClient, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return showContext(cl, args[0], args[1], args[2]) | ||
|
@@ -51,8 +51,8 @@ func newContextCommand(config *settings.Config) *cobra.Command { | |
} | ||
|
||
storeCommand := &cobra.Command{ | ||
Short: "Store a new environment variable in the named context. The value is read from stdin.", | ||
Use: "store-secret <vcs-type> <org-name> <context-name> <secret name>", | ||
Short: "Store a new environment variable in the named context. The value is read from stdin.", | ||
Use: "store-secret <vcs-type> <org-name> <context-name> <secret name>", | ||
PreRunE: initClient, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return storeEnvVar(cl, args[0], args[1], args[2], args[3]) | ||
|
@@ -61,8 +61,8 @@ func newContextCommand(config *settings.Config) *cobra.Command { | |
} | ||
|
||
removeCommand := &cobra.Command{ | ||
Short: "Remove an environment variable from the named context", | ||
Use: "remove-secret <vcs-type> <org-name> <context-name> <secret name>", | ||
Short: "Remove an environment variable from the named context", | ||
Use: "remove-secret <vcs-type> <org-name> <context-name> <secret name>", | ||
PreRunE: initClient, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return removeEnvVar(cl, args[0], args[1], args[2], args[3]) | ||
|
@@ -71,8 +71,8 @@ func newContextCommand(config *settings.Config) *cobra.Command { | |
} | ||
|
||
createContextCommand := &cobra.Command{ | ||
Short: "Create a new context", | ||
Use: "create <vcs-type> <org-name> <context-name>", | ||
Short: "Create a new context", | ||
Use: "create <vcs-type> <org-name> <context-name>", | ||
PreRunE: initClient, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return createContext(cl, args[0], args[1], args[2]) | ||
|
@@ -82,8 +82,8 @@ func newContextCommand(config *settings.Config) *cobra.Command { | |
|
||
force := false | ||
deleteContextCommand := &cobra.Command{ | ||
Short: "Delete the named context", | ||
Use: "delete <vcs-type> <org-name> <context-name>", | ||
Short: "Delete the named context", | ||
Use: "delete <vcs-type> <org-name> <context-name>", | ||
PreRunE: initClient, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return deleteContext(cl, force, args[0], args[1], args[2]) | ||
|
@@ -169,17 +169,27 @@ func showContext(client *client.Client, vcsType, orgName, contextName string) er | |
return nil | ||
} | ||
|
||
func readSecretValue() (string, error) { | ||
// ReadSecretValue reads a secret from a buffer | ||
func ReadSecretValue() (string, error) { | ||
stat, _ := os.Stdin.Stat() | ||
|
||
buffSize, err := os.Stdin.Stat() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
reader := bufio.NewReaderSize(os.Stdin, int(buffSize.Size())) | ||
|
||
if (stat.Mode() & os.ModeCharDevice) == 0 { | ||
bytes, err := ioutil.ReadAll(os.Stdin) | ||
bytes := make([]byte, buffSize.Size()) | ||
_, err := io.ReadFull(reader, bytes) | ||
Comment on lines
-175
to
+185
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you happen to experience errors other than I believe that |
||
return string(bytes), err | ||
} else { | ||
fmt.Print("Enter secret value and press enter: ") | ||
reader := bufio.NewReader(os.Stdin) | ||
str, err := reader.ReadString('\n') | ||
return strings.TrimRight(str, "\n"), err | ||
} | ||
|
||
fmt.Print("Enter secret value and press enter: ") | ||
|
||
str, err := reader.ReadString('\n') | ||
return strings.TrimRight(str, "\n"), err | ||
} | ||
|
||
func createContext(client *client.Client, vcsType, orgName, contextName string) error { | ||
|
@@ -201,7 +211,7 @@ func storeEnvVar(client *client.Client, vcsType, orgName, contextName, varName s | |
if err != nil { | ||
return err | ||
} | ||
secretValue, err := readSecretValue() | ||
secretValue, err := ReadSecretValue() | ||
|
||
if err != nil { | ||
return errors.Wrap(err, "Failed to read secret value from stdin") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid the redundancy, would it be possible to remove this
Stat()
call and instead use the one on line 174?