From 2aea73861b682ecbc2d236e4390646cc8fce1d0f Mon Sep 17 00:00:00 2001 From: Akhil Mohan Date: Thu, 18 Apr 2024 13:09:09 +0530 Subject: [PATCH 1/2] feat(cli): added template feature to cli export command --- cli/packages/cmd/export.go | 32 ++++++++++++++++++++++++++++++++ docs/cli/commands/export.mdx | 19 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/cli/packages/cmd/export.go b/cli/packages/cmd/export.go index 4e08e5fa8d..951d538bab 100644 --- a/cli/packages/cmd/export.go +++ b/cli/packages/cmd/export.go @@ -7,6 +7,7 @@ import ( "encoding/csv" "encoding/json" "fmt" + "os" "strings" "github.com/Infisical/infisical-merge/packages/models" @@ -59,6 +60,11 @@ var exportCmd = &cobra.Command{ util.HandleError(err) } + templatePath, err := cmd.Flags().GetString("template") + if err != nil { + util.HandleError(err) + } + secretOverriding, err := cmd.Flags().GetBool("secret-overriding") if err != nil { util.HandleError(err, "Unable to parse flag") @@ -93,6 +99,31 @@ var exportCmd = &cobra.Command{ request.UniversalAuthAccessToken = token.Token } + if templatePath != "" { + sigChan := make(chan os.Signal, 1) + dynamicSecretLeases := NewDynamicSecretLeaseManager(sigChan) + newEtag := "" + + accessToken := "" + if token != nil { + accessToken = token.Token + } else { + log.Debug().Msg("GetAllEnvironmentVariables: Trying to fetch secrets using logged in details") + loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails() + if err != nil { + util.HandleError(err) + } + accessToken = loggedInUserDetails.UserCredentials.JTWToken + } + + processedTemplate, err := ProcessTemplate(1, templatePath, nil, accessToken, "", &newEtag, dynamicSecretLeases) + if err != nil { + util.HandleError(err) + } + fmt.Print(processedTemplate.String()) + return + } + secrets, err := util.GetAllEnvironmentVariables(request, "") if err != nil { util.HandleError(err, "Unable to fetch secrets") @@ -140,6 +171,7 @@ func init() { exportCmd.Flags().StringP("tags", "t", "", "filter secrets by tag slugs") exportCmd.Flags().String("projectId", "", "manually set the projectId to fetch secrets from") exportCmd.Flags().String("path", "/", "get secrets within a folder path") + exportCmd.Flags().String("template", "", "The path to the template file to output secrets") } // Format according to the format flag diff --git a/docs/cli/commands/export.mdx b/docs/cli/commands/export.mdx index 49446e7345..246c674cd8 100644 --- a/docs/cli/commands/export.mdx +++ b/docs/cli/commands/export.mdx @@ -33,6 +33,9 @@ Export environment variables from the platform into a file format. # Export variables to a YAML file infisical export --format=yaml > secrets.yaml + + # Export variables from a template + infisical export --template= ``` ### Environment variables @@ -57,6 +60,22 @@ Export environment variables from the platform into a file format. ### flags + + The --template flag specifies the path to the template file used for rendering secrets. In template mode, you can omit the other flags since the settings are already configured within the template. + + ```bash + # Example + infisical export --template="/path/to/template" + ``` + + ```text my-dot-env-secret-template + {{- with secret "" "" "/" }} + {{- range . }} + {{ .Key }}={{ .Value }} + {{- end }} + {{- end }} + ``` + Used to set the environment that secrets are pulled from. From 581ffc613c7608ee36f869d96ff4b2e6771d9735 Mon Sep 17 00:00:00 2001 From: Maidul Islam Date: Thu, 18 Apr 2024 15:45:20 -0400 Subject: [PATCH 2/2] add go lang add/minus functions and give better example --- cli/packages/cmd/agent.go | 6 ++++++ cli/packages/cmd/export.go | 2 +- docs/cli/commands/export.mdx | 26 +++++++++++++++----------- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/cli/packages/cmd/agent.go b/cli/packages/cmd/agent.go index 7e8babb85b..03bf9af4da 100644 --- a/cli/packages/cmd/agent.go +++ b/cli/packages/cmd/agent.go @@ -381,6 +381,12 @@ func ProcessTemplate(templateId int, templatePath string, data interface{}, acce funcs := template.FuncMap{ "secret": secretFunction, "dynamic_secret": dynamicSecretFunction, + "minus": func(a, b int) int { + return a - b + }, + "add": func(a, b int) int { + return a + b + }, } templateName := path.Base(templatePath) diff --git a/cli/packages/cmd/export.go b/cli/packages/cmd/export.go index 951d538bab..84015cc02f 100644 --- a/cli/packages/cmd/export.go +++ b/cli/packages/cmd/export.go @@ -171,7 +171,7 @@ func init() { exportCmd.Flags().StringP("tags", "t", "", "filter secrets by tag slugs") exportCmd.Flags().String("projectId", "", "manually set the projectId to fetch secrets from") exportCmd.Flags().String("path", "/", "get secrets within a folder path") - exportCmd.Flags().String("template", "", "The path to the template file to output secrets") + exportCmd.Flags().String("template", "", "The path to the template file used to render secrets") } // Format according to the format flag diff --git a/docs/cli/commands/export.mdx b/docs/cli/commands/export.mdx index 246c674cd8..91ed215ef2 100644 --- a/docs/cli/commands/export.mdx +++ b/docs/cli/commands/export.mdx @@ -34,7 +34,7 @@ Export environment variables from the platform into a file format. # Export variables to a YAML file infisical export --format=yaml > secrets.yaml - # Export variables from a template + # Render secrets using a custom template file infisical export --template= ``` @@ -61,20 +61,24 @@ Export environment variables from the platform into a file format. ### flags - The --template flag specifies the path to the template file used for rendering secrets. In template mode, you can omit the other flags since the settings are already configured within the template. + The `--template` flag specifies the path to the template file used for rendering secrets. When using templates, you can omit the other format flags. + + ```text my-template-file + {{$secrets := secret "" "" ""}} + {{$length := len $secrets}} + {{- "{"}} + {{- with $secrets }} + {{- range $index, $secret := . }} + "{{ $secret.Key }}": "{{ $secret.Value }}"{{if lt $index (minus $length 1)}},{{end}} + {{- end }} + {{- end }} + {{ "}" -}} + ``` ```bash # Example - infisical export --template="/path/to/template" + infisical export --template="/path/to/template/file" ``` - - ```text my-dot-env-secret-template - {{- with secret "" "" "/" }} - {{- range . }} - {{ .Key }}={{ .Value }} - {{- end }} - {{- end }} - ``` Used to set the environment that secrets are pulled from.