diff --git a/cmd/wp-opentofu/flags.go b/cmd/wp-opentofu/flags.go index 166d218..6249d46 100644 --- a/cmd/wp-opentofu/flags.go +++ b/cmd/wp-opentofu/flags.go @@ -19,15 +19,15 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag { Category: category, }, &cli.StringFlag{ - Name: "init-options", + Name: "init-option", Usage: "tofu init command options, see https://opentofu.org/docs/cli/commands/init/", - EnvVars: []string{"PLUGIN_INIT_OPTIONS"}, + EnvVars: []string{"PLUGIN_INIT_OPTION"}, Category: category, }, &cli.StringFlag{ - Name: "fmt-options", + Name: "fmt-option", Usage: "options for the fmt command, see https://opentofu.org/docs/cli/commands/fmt/", - EnvVars: []string{"PLUGIN_FMT_OPTIONS"}, + EnvVars: []string{"PLUGIN_FMT_OPTION"}, Category: category, }, &cli.IntFlag{ diff --git a/docs/data/data.yaml b/docs/data/data.yaml index d9f4b51..97cee67 100644 --- a/docs/data/data.yaml +++ b/docs/data/data.yaml @@ -7,13 +7,13 @@ properties: defaultValue: "validate,plan,apply" required: false - - name: fmt_options + - name: fmt_option description: | Options for the fmt command, see the OpenTofu [fmt command](https://opentofu.org/docs/cli/commands/fmt/) documentation. type: string required: false - - name: init_options + - name: init_option description: | Tofu init command options, see the OpenTofu [init command](https://opentofu.org/docs/cli/commands/init/) documentation. type: string diff --git a/plugin/impl.go b/plugin/impl.go index 98a39e3..6af1042 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -2,6 +2,7 @@ package plugin import ( "context" + "encoding/json" "errors" "fmt" "os" @@ -24,6 +25,10 @@ const ( //nolint:revive func (p *Plugin) run(ctx context.Context) error { + if err := p.FlagsFromContext(); err != nil { + return fmt.Errorf("validation failed: %w", err) + } + if err := p.Validate(); err != nil { return fmt.Errorf("validation failed: %w", err) } @@ -35,6 +40,28 @@ func (p *Plugin) run(ctx context.Context) error { return nil } +func (p *Plugin) FlagsFromContext() error { + if p.Context.String("init-option") != "" { + initOptions := InitOptions{} + if err := json.Unmarshal([]byte(p.Context.String("init-option")), &initOptions); err != nil { + return fmt.Errorf("cannot unmarshal init_option: %w", err) + } + + p.Settings.InitOptions = initOptions + } + + if p.Context.String("fmt-option") != "" { + fmtOptions := FmtOptions{} + if err := json.Unmarshal([]byte(p.Context.String("fmt-option")), &fmtOptions); err != nil { + return fmt.Errorf("cannot unmarshal fmt_option: %w", err) + } + + p.Settings.FmtOptions = fmtOptions + } + + return nil +} + // Validate handles the settings validation of the plugin. func (p *Plugin) Validate() error { p.Settings.DataDir = ".terraform"