Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 14, 2025
Merged

Conversation

SasSwart
Copy link
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
@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
EvalContext: block.Context().Inner(),
}

if tyAttr.IsNotNil() {
Copy link
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.

Copy link
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
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. 👍

blocks := mod.GetDatasByType(types.BlockTypePreset)
for _, block := range blocks {
preset := extract.PresetFromBlock(block)
switch true {
Copy link
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 {

"valid_parameter_name": ap().
optVals("valid_option_value"),
},
presets: func(t *testing.T, presets []types.Preset) {
Copy link
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