Skip to content

Commit 6592bfe

Browse files
committed
feat(sdk/provisioner): allow both prepending and appending arguments
- some commands (like `age`) have positional argument which require to be placed after all flag arguments - change allows prepending of args to after the command itself instead of just appending to the end and potentially causing issues
1 parent 6e74fd8 commit 6592bfe

File tree

2 files changed

+65
-10
lines changed

2 files changed

+65
-10
lines changed

sdk/provision/file_provisioner.go

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2727
type 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.
3038
func 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

sdk/provisioner.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,30 @@ func (out *ProvisionOutput) AddEnvVar(name string, value string) {
102102
out.Environment[name] = value
103103
}
104104

105+
func (out *ProvisionOutput) AddArgsAtIndex(position int, args ...string) {
106+
if position == -1 || position >= len(out.CommandLine) {
107+
out.CommandLine = append(out.CommandLine, args...)
108+
return
109+
}
110+
111+
if position <= 0 {
112+
out.CommandLine = append(args, out.CommandLine...)
113+
return
114+
}
115+
116+
out.CommandLine = append(out.CommandLine[:position], append(args, out.CommandLine[position:]...)...)
117+
}
118+
119+
// PrependArgs can be used to add additional arguments to the command line of the provision output.
120+
func (out *ProvisionOutput) PrependArgs(args ...string) {
121+
out.AddArgsAtIndex(1, args...)
122+
}
123+
124+
// AppendArgs can be used to add additional arguments to the command line of the provision output.
125+
func (out *ProvisionOutput) AppendArgs(args ...string) {
126+
out.AddArgsAtIndex(-1, args...)
127+
}
128+
105129
// AddArgs can be used to add additional arguments to the command line of the provision output.
106130
func (out *ProvisionOutput) AddArgs(args ...string) {
107131
out.CommandLine = append(out.CommandLine, args...)

0 commit comments

Comments
 (0)