Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/mysql/database_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func DatabaseCredentials() schema.CredentialType {
Optional: true,
},
},
DefaultProvisioner: provision.TempFile(mysqlConfig, provision.Filename("my.cnf"), provision.AddArgs("--defaults-file={{ .Path }}")),
DefaultProvisioner: provision.TempFile(mysqlConfig, provision.Filename("my.cnf"), provision.PrependArgs("--defaults-file={{ .Path }}")),
Importer: importer.TryAll(
TryMySQLConfigFile("/etc/my.cnf"),
TryMySQLConfigFile("/etc/mysql/my.cnf"),
Expand Down
20 changes: 19 additions & 1 deletion sdk/provision/file_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type FileProvisioner struct {
outpathEnvVar string
outdirEnvVar string
setOutpathAsArg bool
prependArgs bool
outpathArgTemplates []string
}

Expand Down Expand Up @@ -95,6 +96,19 @@ func AddArgs(argTemplates ...string) FileOption {
}
}

// PrependArgs can be used to prepend args to the command line, placing them before any user-provided arguments.
// This is useful for CLIs like mysql that require certain arguments (e.g., --defaults-file) to be the very first argument.
// The output path is available as "{{ .Path }}" in each arg.
// For example:
// * `PrependArgs("--defaults-file={{ .Path }}")` will result in `--defaults-file=/path/to/tempfile` being placed first.
func PrependArgs(argTemplates ...string) FileOption {
return func(p *FileProvisioner) {
p.setOutpathAsArg = true
p.prependArgs = true
p.outpathArgTemplates = argTemplates
}
}

func (p FileProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, out *sdk.ProvisionOutput) {
contents, err := p.fileContents(in)
if err != nil {
Expand Down Expand Up @@ -159,7 +173,11 @@ func (p FileProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, o
argsResolved[i] = result.String()
}

out.AddArgs(argsResolved...)
if p.prependArgs {
out.PrependArgs(argsResolved...)
} else {
out.AddArgs(argsResolved...)
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions sdk/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"path/filepath"
"slices"
"time"
)

Expand Down Expand Up @@ -107,6 +108,11 @@ func (out *ProvisionOutput) AddArgs(args ...string) {
out.CommandLine = append(out.CommandLine, args...)
}

// PrependArgs can be used to insert additional arguments at the beginning of the command line of the provision output.
func (out *ProvisionOutput) PrependArgs(args ...string) {
out.CommandLine = slices.Insert(out.CommandLine, 1, args...)
}

// AddSecretFile can be used to add a file containing secrets to the provision output.
func (out *ProvisionOutput) AddSecretFile(path string, contents []byte) {
out.AddFile(path, OutputFile{
Expand Down