Merge pull request #3316 from Infisical/feat/addFileContentToArgValueCLI

Add file support to secrets set key=value command
This commit is contained in:
carlosmonastyrski
2025-03-27 16:10:44 -03:00
committed by GitHub
2 changed files with 45 additions and 4 deletions

View File

@@ -5,6 +5,7 @@ package cmd
import (
"fmt"
"os"
"regexp"
"sort"
"strings"
@@ -139,7 +140,7 @@ var secretsGenerateExampleEnvCmd = &cobra.Command{
}
var secretsSetCmd = &cobra.Command{
Example: `secrets set <secretName=secretValue> <secretName=secretValue>..."`,
Example: `secrets set <secretName=secretValue> <secretName=secretValue> <secretName=@/path/to/file>..."`,
Short: "Used set secrets",
Use: "set [secrets]",
DisableFlagsInUseLine: true,
@@ -185,6 +186,30 @@ var secretsSetCmd = &cobra.Command{
util.HandleError(err, "Unable to parse secret type")
}
processedArgs := []string{}
for _, arg := range args {
splitKeyValue := strings.SplitN(arg, "=", 2)
if len(splitKeyValue) != 2 {
util.HandleError(fmt.Errorf("invalid argument format: %s. Expected format: key=value or key=@filepath", arg), "")
}
key := splitKeyValue[0]
value := splitKeyValue[1]
if strings.HasPrefix(value, "\\@") {
value = "@" + value[2:]
} else if strings.HasPrefix(value, "@") {
filePath := strings.TrimPrefix(value, "@")
content, err := os.ReadFile(filePath)
if err != nil {
util.HandleError(err, fmt.Sprintf("Unable to read file %s", filePath))
}
value = string(content)
}
processedArgs = append(processedArgs, fmt.Sprintf("%s=%s", key, value))
}
file, err := cmd.Flags().GetString("file")
if err != nil {
util.HandleError(err, "Unable to parse flag")
@@ -216,7 +241,7 @@ var secretsSetCmd = &cobra.Command{
util.PrintErrorMessageAndExit("Your login session has expired, please run [infisical login] and try again")
}
secretOperations, err = util.SetRawSecrets(args, secretType, environmentName, secretsPath, projectId, &models.TokenDetails{
secretOperations, err = util.SetRawSecrets(processedArgs, secretType, environmentName, secretsPath, projectId, &models.TokenDetails{
Type: "",
Token: loggedInUserDetails.UserCredentials.JTWToken,
}, file)

View File

@@ -186,12 +186,28 @@ This command allows you to set or update secrets in your environment. If the sec
If the secret key does not exist, a new secret will be created using both the key and value provided.
```bash
$ infisical secrets set <key1=value1> <key2=value2>...
$ infisical secrets set <key1=value1> <key2=value2> <key3=@/path/to/file>...
## Example
$ infisical secrets set STRIPE_API_KEY=sjdgwkeudyjwe DOMAIN=example.com HASH=jebhfbwe
$ infisical secrets set STRIPE_API_KEY=sjdgwkeudyjwe DOMAIN=example.com HASH=jebhfbwe SECRET_PEM_KEY=@secret.pem
```
<Tip>
When setting secret values:
- Use `secretName=@path/to/file` to load the secret value from a file
- Use `secretName=\@value` if you need the literal '@' character at the beginning of your value
Example:
```bash
# Set a secret with the value loaded from a certificate file
$ secrets set CERTIFICATE=@/path/to/certificate.pem
# Set a secret with the literal value "@example.com"
$ secrets set email="\@example.com"
```
</Tip>
### Flags
<Accordion title="--env">