@@ -20,12 +20,20 @@ type FileProvisioner struct {
2020 outpathFixed string
2121 outpathEnvVar string
2222 outdirEnvVar string
23- setOutpathAsArg bool
23+ argPlacementMode ArgPlacementMode
2424 outpathArgTemplates []string
2525}
2626
2727type ItemToFileContents func (in sdk.ProvisionInput ) ([]byte , error )
2828
29+ type ArgPlacementMode int
30+
31+ const (
32+ Unset ArgPlacementMode = iota
33+ Prepend
34+ Append
35+ )
36+
2937// FieldAsFile can be used to store the value of a single field as a file.
3038func FieldAsFile (fieldName sdk.FieldName ) ItemToFileContents {
3139 return ItemToFileContents (func (in sdk.ProvisionInput ) ([]byte , error ) {
@@ -39,7 +47,7 @@ func FieldAsFile(fieldName sdk.FieldName) ItemToFileContents {
3947
4048// TempFile returns a file provisioner and takes a function that maps a 1Password item to the contents of
4149// a single file.
42- func TempFile (fileContents ItemToFileContents , opts ... FileOption ) sdk. Provisioner {
50+ func TempFile (fileContents ItemToFileContents , opts ... FileOption ) FileProvisioner {
4351 p := FileProvisioner {
4452 fileContents : fileContents ,
4553 }
@@ -83,14 +91,30 @@ func SetOutputDirAsEnvVar(envVarName string) FileOption {
8391 }
8492}
8593
86- // AddArgs can be used to add args to the command line. This is useful when the output file path
87- // should be passed as an arg. The output path is available as "{{ .Path }}" in each arg.
94+ // AppendArgs appends arguments to the command line for a FileProvisioner.
95+ // This is particularly useful when you need to add arguments that reference the output file path.
96+ // The output path is available as "{{ .Path }}" within the provided argument templates.
97+ // For example:
98+ // * `AppendArgs("--log", "{{ .Path }}")` results in `--log /path/to/tempfile`.
99+ // * `AppendArgs("--log={{ .Path }}")` results in `--log=/path/to/tempfile`.
100+ func AppendArgs (argTemplates ... string ) FileOption {
101+ return func (p * FileProvisioner ) {
102+ p .argPlacementMode = Append
103+ p .outpathArgTemplates = argTemplates
104+ }
105+ }
106+
107+ // PrependArgs prepends arguments to the command line for a FileProvisioner.
108+ // This is particularly useful when you need to add arguments that reference the output file path.
109+ // The output path is available as "{{ .Path }}" within the provided argument templates.
88110// For example:
89- // * `AddArgs("--config-file", "{{ .Path }}")` will result in `--config-file /path/to/tempfile`.
90- // * `AddArgs("--config-file={{ .Path }}")` will result in `--config-file=/path/to/tempfile`.
91- func AddArgs (argTemplates ... string ) FileOption {
111+ // * `PrependArgs("--input", "{{ .Path }}")` results in `--input /path/to/tempfile`.
112+ // * `PrependArgs("--input={{ .Path }}")` results in `--input=/path/to/tempfile`.
113+ //
114+ // The arguments provided are added before any pre-existing arguments in the command line, but after the command itself.
115+ func PrependArgs (argTemplates ... string ) FileOption {
92116 return func (p * FileProvisioner ) {
93- p .setOutpathAsArg = true
117+ p .argPlacementMode = Prepend
94118 p .outpathArgTemplates = argTemplates
95119 }
96120}
@@ -134,7 +158,7 @@ func (p FileProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, o
134158 }
135159
136160 // Add args to specify the output path.
137- if p .setOutpathAsArg {
161+ if p .argPlacementMode != Unset {
138162 tmplData := struct { Path string }{
139163 Path : outpath ,
140164 }
@@ -159,7 +183,14 @@ func (p FileProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, o
159183 argsResolved [i ] = result .String ()
160184 }
161185
162- out .AddArgs (argsResolved ... )
186+ switch p .argPlacementMode {
187+ case Append :
188+ out .AppendArgs (argsResolved ... )
189+ case Prepend :
190+ out .PrependArgs (argsResolved ... )
191+ default :
192+ out .AddError (fmt .Errorf ("invalid argument placement mode" ))
193+ }
163194 }
164195}
165196
0 commit comments