Skip to content

Commit

Permalink
support reading from stdin for sst secret set
Browse files Browse the repository at this point in the history
  • Loading branch information
thdxr committed Jun 12, 2024
1 parent 7b0d30a commit 649ffdf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/sst/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TransformError(err error) error {
project.ErrStackRunFailed: "",
provider.ErrLockExists: "",
project.ErrVersionInvalid: "The version range defined in the config is invalid",
provider.ErrCloudflareMissingAccount: "This cloudflare token does not have access to any accounts. Please make sure the token has the right permissions. You can also set the CLOUDFLARE_DEFAULT_ACCOUNT_ID environment variable to the account id you want to use.",
provider.ErrCloudflareMissingAccount: "The Cloudflare Account ID was not able to be determined from this token. Make sure it has permissions to fetch account information or you can set the CLOUDFLARE_DEFAULT_ACCOUNT_ID environment variable to the account id you want to use.",
}

readable := []error{
Expand Down
8 changes: 7 additions & 1 deletion cmd/sst/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ var Root = Command{
},
{
Name: "value",
Required: true,
Required: false,
Description: Description{
Short: "The value of the secret",
Long: "The value of the secret.",
Expand All @@ -714,6 +714,12 @@ var Root = Command{
Short: "Set the StripeSecret to 123456789",
},
},
{
Content: "sst secret set StripeSecret < tmp.txt",
Description: Description{
Short: "Set the StripeSecret to contents of tmp.txt",
},
},
{
Content: "sst secret set StripeSecret productionsecret --stage=production",
Description: Description{
Expand Down
25 changes: 25 additions & 0 deletions cmd/sst/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bufio"
"fmt"
"io"
"net/http"
"os"
"regexp"
Expand Down Expand Up @@ -41,6 +42,30 @@ func CmdSecretList(cli *Cli) error {
func CmdSecretSet(cli *Cli) error {
key := cli.Positional(0)
value := cli.Positional(1)
if value == "" {
stat, err := os.Stdin.Stat()
if err != nil {
return err
}
isTerminal := (stat.Mode() & os.ModeCharDevice) != 0
if isTerminal {
fmt.Print("Enter value: ")
}
reader := bufio.NewReader(os.Stdin)
for {
input, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
break
}
return err
}
value += input
if isTerminal {
break
}
}
}
if !regexp.MustCompile(`^[A-Z][a-zA-Z0-9]*$`).MatchString(key) {
return util.NewReadableError(nil, "Secret names must start with a capital letter and contain only letters and numbers")
}
Expand Down

0 comments on commit 649ffdf

Please sign in to comment.