Skip to content

Commit

Permalink
Merge pull request #219 from imakecodes/feature/adding-export-dotenv-…
Browse files Browse the repository at this point in the history
…export-format

feat(CLI): adding new export format (dotenv-export)
  • Loading branch information
maidul98 authored Jan 14, 2023
2 parents 3bc7f2a + 98d84b6 commit 9ca544f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
22 changes: 17 additions & 5 deletions cli/packages/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import (
)

const (
FormatDotenv string = "dotenv"
FormatJson string = "json"
FormatCSV string = "csv"
FormatYaml string = "yaml"
FormatDotenv string = "dotenv"
FormatJson string = "json"
FormatCSV string = "csv"
FormatYaml string = "yaml"
FormatDotEnvExport string = "dotenv-export"
)

// exportCmd represents the export command
Expand Down Expand Up @@ -85,14 +86,16 @@ func formatEnvs(envs []models.SingleEnvironmentVariable, format string) (string,
switch strings.ToLower(format) {
case FormatDotenv:
return formatAsDotEnv(envs), nil
case FormatDotEnvExport:
return formatAsDotEnvExport(envs), nil
case FormatJson:
return formatAsJson(envs), nil
case FormatCSV:
return formatAsCSV(envs), nil
case FormatYaml:
return formatAsYaml(envs), nil
default:
return "", fmt.Errorf("invalid format type: %s. Available format types are [%s]", format, []string{FormatDotenv, FormatJson, FormatCSV, FormatYaml})
return "", fmt.Errorf("invalid format type: %s. Available format types are [%s]", format, []string{FormatDotenv, FormatJson, FormatCSV, FormatYaml, FormatDotEnvExport})
}
}

Expand All @@ -117,6 +120,15 @@ func formatAsDotEnv(envs []models.SingleEnvironmentVariable) string {
return dotenv
}

// Format environment variables as a dotenv file with export at the beginning
func formatAsDotEnvExport(envs []models.SingleEnvironmentVariable) string {
var dotenv string
for _, env := range envs {
dotenv += fmt.Sprintf("export %s='%s'\n", env.Key, env.Value)
}
return dotenv
}

func formatAsYaml(envs []models.SingleEnvironmentVariable) string {
var dotenv string
for _, env := range envs {
Expand Down
16 changes: 10 additions & 6 deletions docs/cli/commands/export.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ Export environment variables from the platform into a file format.

## Options

| Option | Description | Default value |
| ------------- | -------------------------------------------------------------------------------------------------------------------------------------------- | ------------- |
| `--env` | Used to set the environment that secrets are pulled from. Accepted values: `dev`, `staging`, `test`, `prod` | `dev` |
| `--projectId` | Only required if injecting via the [service token method](../token). If you are not using service token, the project id will be automatically retrieved from the `.infisical.json` located at the root of your local project. | `None` |
| `--expand` | Parse shell parameter expansions in your secrets (e.g., `${DOMAIN}`) | `true` |
| `--format` | Format of the output file. Accepted values: `dotenv`, `csv` and `json` | `dotenv` |
| Option | Description | Default value |
| ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- |
| `--env` | Used to set the environment that secrets are pulled from. Accepted values: `dev`, `staging`, `test`, `prod` | `dev` |
| `--projectId` | Only required if injecting via the [service token method](../token). If you are not using service token, the project id will be automatically retrieved from the `.infisical.json` located at the root of your local project. | `None` |
| `--expand` | Parse shell parameter expansions in your secrets (e.g., `${DOMAIN}`) | `true` |
| `--format` | Format of the output file. Accepted values: `dotenv`, `dotenv-export`, `csv` and `json` | `dotenv` |

## Examples

```bash
# Export variables to a .env file
infisical export > .env

# Export variables to a .env file (with export keyword)
infisical export --format=dotenv-export > .env

# Export variables to a CSV file
infisical export --format=csv > secrets.csv

Expand All @@ -33,4 +36,5 @@ infisical export --format=json > secrets.json

# Export variables to a YAML file
infisical export --format=yaml > secrets.yaml

```

0 comments on commit 9ca544f

Please sign in to comment.