From 66478bb8a970fd7ff207049d3c4d701209542544 Mon Sep 17 00:00:00 2001 From: Floris van der Grinten Date: Wed, 15 Feb 2023 11:36:22 +0100 Subject: [PATCH] Add file provisioner helper to provision output dir Some CLIs only allow a config file path to be set using the directory it's in. --- sdk/provision/file_provisioner.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/sdk/provision/file_provisioner.go b/sdk/provision/file_provisioner.go index b182b767e..4d5d6041d 100644 --- a/sdk/provision/file_provisioner.go +++ b/sdk/provision/file_provisioner.go @@ -5,6 +5,7 @@ import ( "context" "crypto/rand" "fmt" + "path/filepath" "text/template" "github.com/1Password/shell-plugins/sdk" @@ -18,6 +19,7 @@ type FileProvisioner struct { outfileName string outpathFixed string outpathEnvVar string + outdirEnvVar string setOutpathAsArg bool outpathArgTemplates []string } @@ -74,6 +76,13 @@ func SetPathAsEnvVar(envVarName string) FileOption { } } +// SetOutputDirAsEnvVar can be used to provision the directory of the output file as an environment variable. +func SetOutputDirAsEnvVar(envVarName string) FileOption { + return func(p *FileProvisioner) { + p.outdirEnvVar = envVarName + } +} + // AddArgs can be used to add args to the command line. This is useful when the output file path // should be passed as an arg. The output path is available as "{{ .Path }}" in each arg. // For example: @@ -118,6 +127,12 @@ func (p FileProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, o out.AddEnvVar(p.outpathEnvVar, outpath) } + if p.outdirEnvVar != "" { + // Populate the specified environment variable with the output dir. + dir := filepath.Dir(outpath) + out.AddEnvVar(p.outpathEnvVar, dir) + } + // Add args to specify the output path. if p.setOutpathAsArg { tmplData := struct{ Path string }{