Skip to content

feat: Preview can now show presets and validate them#149

Merged
SasSwart merged 3 commits into
mainfrom
jjs/presets
Jul 14, 2025
Merged

feat: Preview can now show presets and validate them#149
SasSwart merged 3 commits into
mainfrom
jjs/presets

Conversation

@SasSwart
Copy link
Copy Markdown
Contributor

This PR provides a mechanism to "lint" presets to ensure that they refer to parameters that actually exist and define values that are actually valid for those parameters.

Relates to: coder/coder#17333

Screenshot 2025-06-18 at 12 03 37

@SasSwart SasSwart requested a review from Emyrk June 18, 2025 10:11
Comment thread types/preset.go Outdated
Comment thread extract/preset.go
Comment thread preset.go
Comment thread preset.go Outdated
Comment thread preview.go Outdated
@SasSwart SasSwart requested review from Emyrk and ssncferreira July 13, 2025 20:15
@SasSwart SasSwart marked this pull request as ready for review July 13, 2025 20:15
Comment thread extract/parameter.go
EvalContext: block.Context().Inner(),
}

if tyAttr.IsNotNil() {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a nilpointer here during testing.

Comment thread extract/preset.go
Copy link
Copy Markdown
Member

@Emyrk Emyrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Comment on lines +77 to +104
func Presets(writer io.Writer, presets []types.Preset, files map[string]*hcl.File) {
tableWriter := table.NewWriter()
tableWriter.SetStyle(table.StyleLight)
tableWriter.Style().Options.SeparateColumns = false
row := table.Row{"Preset"}
tableWriter.AppendHeader(row)
for _, p := range presets {
tableWriter.AppendRow(table.Row{
fmt.Sprintf("%s\n%s", p.Name, formatPresetParameters(p.Parameters)),
})
if hcl.Diagnostics(p.Diagnostics).HasErrors() {
var out bytes.Buffer
WriteDiagnostics(&out, files, hcl.Diagnostics(p.Diagnostics))
tableWriter.AppendRow(table.Row{out.String()})
}

tableWriter.AppendSeparator()
}
_, _ = fmt.Fprintln(writer, tableWriter.Render())
}

func formatPresetParameters(presetParameters map[string]string) string {
var str strings.Builder
for presetParamName, PresetParamValue := range presetParameters {
_, _ = str.WriteString(fmt.Sprintf("%s = %s\n", presetParamName, PresetParamValue))
}
return str.String()
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice. The preview cli is very much a dev tool still. 👍

Comment thread preset.go
blocks := mod.GetDatasByType(types.BlockTypePreset)
for _, block := range blocks {
preset := extract.PresetFromBlock(block)
switch true {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
switch true {
switch {

Comment thread preview_test.go
"valid_parameter_name": ap().
optVals("valid_option_value"),
},
presets: func(t *testing.T, presets []types.Preset) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is probably a way to make these assertions a bit more reusable. Fine for now though

@SasSwart SasSwart merged commit a737d47 into main Jul 14, 2025
3 checks passed
@SasSwart SasSwart deleted the jjs/presets branch July 14, 2025 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants