Skip to content

Commit

Permalink
Merge pull request #1532 from rhythmbhiwani/get-only-value-from-cli
Browse files Browse the repository at this point in the history
Feature to get only value of specific secret in `secrets get` command
  • Loading branch information
maidul98 authored Mar 8, 2024
2 parents 79196b0 + 99fe43f commit 8a17cd3
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 29 deletions.
16 changes: 15 additions & 1 deletion cli/packages/cmd/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,11 @@ func getSecretsByNames(cmd *cobra.Command, args []string) {
util.HandleError(err, "Unable to parse path flag")
}

showOnlyValue, err := cmd.Flags().GetBool("raw-value")
if err != nil {
util.HandleError(err, "Unable to parse path flag")
}

secrets, err := util.GetAllEnvironmentVariables(models.GetAllSecretsParameters{Environment: environmentName, InfisicalToken: infisicalToken, TagSlugs: tagSlugs, SecretsPath: secretsPath}, "")
if err != nil {
util.HandleError(err, "To fetch all secrets")
Expand All @@ -435,7 +440,15 @@ func getSecretsByNames(cmd *cobra.Command, args []string) {
}
}

visualize.PrintAllSecretDetails(requestedSecrets)
if showOnlyValue && len(requestedSecrets) > 1 {
util.PrintErrorMessageAndExit("--raw-value only works with one secret.")
}

if showOnlyValue {
fmt.Printf(requestedSecrets[0].Value)
} else {
visualize.PrintAllSecretDetails(requestedSecrets)
}
Telemetry.CaptureEvent("cli-command:secrets get", posthog.NewProperties().Set("secretCount", len(secrets)).Set("version", util.CLI_VERSION))
}

Expand Down Expand Up @@ -669,6 +682,7 @@ func init() {
secretsGetCmd.Flags().String("token", "", "Fetch secrets using the Infisical Token")
secretsCmd.AddCommand(secretsGetCmd)
secretsGetCmd.Flags().String("path", "/", "get secrets within a folder path")
secretsGetCmd.Flags().Bool("raw-value", false, "Returns only the value of secret, only works with one secret")

secretsCmd.Flags().Bool("secret-overriding", true, "Prioritizes personal secrets, if any, with the same name over shared secrets")
secretsCmd.AddCommand(secretsSetCmd)
Expand Down
90 changes: 62 additions & 28 deletions docs/cli/commands/secrets.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,27 @@ infisical secrets
```

## Description

This command enables you to perform CRUD (create, read, update, delete) operations on secrets within your Infisical project. With it, you can view, create, update, and delete secrets in your environment.

### Sub-commands
### Sub-commands
<Accordion title="infisical secrets" defaultOpen="true">
Use this command to print out all of the secrets in your project

```bash
$ infisical secrets
```

### Environment variables
### Environment variables

<Accordion title="INFISICAL_TOKEN">
Used to fetch secrets via a [service token](/documentation/platform/token) apposed to logged in credentials. Simply, export this variable in the terminal before running this command.

```bash
# Example
# Example
export INFISICAL_TOKEN=st.63e03c4a97cb4a747186c71e.ed5b46a34c078a8f94e8228f4ab0ff97.4f7f38034811995997d72badf44b42ec
```

</Accordion>

<Accordion title="INFISICAL_DISABLE_UPDATE_CHECK">
Expand All @@ -34,22 +37,26 @@ This command enables you to perform CRUD (create, read, update, delete) operatio
To use, simply export this variable in the terminal before running this command.

```bash
# Example
# Example
export INFISICAL_DISABLE_UPDATE_CHECK=true
```

</Accordion>

### Flags
### Flags

<Accordion title="--expand">
Parse shell parameter expansions in your secrets

Default value: `true`

</Accordion>

<Accordion title="--env">
Used to select the environment name on which actions should be taken on

Default value: `dev`

</Accordion>
<Accordion title="--path">
The `--path` flag indicates which project folder secrets will be injected from.
Expand All @@ -58,45 +65,63 @@ This command enables you to perform CRUD (create, read, update, delete) operatio
# Example
infisical secrets --path="/" --env=dev
```

</Accordion>

</Accordion>

<Accordion title="infisical secrets get">
This command allows you selectively print the requested secrets by name

```bash
$ infisical secrets get <secret-name-a> <secret-name-b> ...
```bash
$ infisical secrets get <secret-name-a> <secret-name-b> ...
# Example
$ infisical secrets get DOMAIN
# Example
$ infisical secrets get DOMAIN
```
```

### Flags

### Flags
<Accordion title="--env">
Used to select the environment name on which actions should be taken on

Default value: `dev`

</Accordion>

<Accordion title="--raw-value">
Used to print the plain value of a single requested secret without any table style.

Default value: `false`

Example: `infisical secrets get DOMAIN --value`

<Tip>
When running in CI/CD environments or in a script, set `INFISICAL_DISABLE_UPDATE_CHECK` env to `true`. This will help hide any CLI update messages and only show the secret value.
</Tip>

</Accordion>
</Accordion>

<Accordion title="infisical secrets set">
This command allows you to set or update secrets in your environment. If the secret key provided already exists, its value will be updated with the new value.
This command allows you to set or update secrets in your environment. If the secret key provided already exists, its value will be updated with the new value.
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>...
## Example
## Example
$ infisical secrets set STRIPE_API_KEY=sjdgwkeudyjwe DOMAIN=example.com HASH=jebhfbwe
```

### Flags
### Flags

<Accordion title="--env">
Used to select the environment name on which actions should be taken on

Default value: `dev`

</Accordion>
<Accordion title="--path">
Used to select the project folder in which the secrets will be set. This is useful when creating new secrets under a particular path.
Expand All @@ -105,43 +130,48 @@ $ infisical secrets set STRIPE_API_KEY=sjdgwkeudyjwe DOMAIN=example.com HASH=jeb
# Example
infisical secrets set DOMAIN=example.com --path="common/backend"
```

</Accordion>
</Accordion>

<Accordion title="infisical secrets delete">
This command allows you to delete secrets by their name(s).

```bash
$ infisical secrets delete <keyName1> <keyName2>...
```bash
$ infisical secrets delete <keyName1> <keyName2>...
## Example
$ infisical secrets delete STRIPE_API_KEY DOMAIN HASH
```
## Example
$ infisical secrets delete STRIPE_API_KEY DOMAIN HASH
```

### Flags

### Flags
<Accordion title="--env">
Used to select the environment name on which actions should be taken on

Default value: `dev`

</Accordion>
<Accordion title="--path">
The `--path` flag indicates which project folder secrets will be injected from.
The `--path` flag indicates which project folder secrets will be injected from.

```bash
# Example
infisical secrets delete <keyName1> <keyName2>... --path="/"
```

</Accordion>
</Accordion>

<Accordion title="infisical secrets folders">
This command allows you to fetch, create and delete folders from within a path from a given project.

```bash
$ infisical secrets folders
```
```bash
$ infisical secrets folders
```

### sub commands

### sub commands
<Accordion title="get">
Used to fetch all folders within a path in a given project
```
Expand Down Expand Up @@ -179,6 +209,7 @@ $ infisical secrets set STRIPE_API_KEY=sjdgwkeudyjwe DOMAIN=example.com HASH=jeb

Default value: ``
</Accordion>

</Accordion>

<Accordion title="delete">
Expand All @@ -194,10 +225,11 @@ $ infisical secrets set STRIPE_API_KEY=sjdgwkeudyjwe DOMAIN=example.com HASH=jeb
</Accordion>

<Accordion title="--name">
Name of the folder to be deleted within selected `--path`
Name of the folder to be deleted within selected `--path`

Default value: ``
</Accordion>

</Accordion>
</Accordion>

Expand All @@ -210,14 +242,16 @@ To place default values in your example .env file, you can simply include the sy
```bash
$ infisical secrets generate-example-env
## Example
## Example
$ infisical secrets generate-example-env > .example-env
```
### Flags
### Flags
<Accordion title="--env">
Used to select the environment name on which actions should be taken on
Default value: `dev`
</Accordion>
</Accordion>

0 comments on commit 8a17cd3

Please sign in to comment.