{"record":{"id":"9f6407d9f81e2644","repo":"hashicorp/nomad","slug":"template-needs-to-be-specified-in-golang-s-text-te","errorCode":null,"errorMessage":"template needs to be specified in golang's text/template format.","messagePattern":"template needs to be specified in golang's text/template format\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command/data_format.go","lineNumber":63,"sourceCode":"func (p *JSONFormat) TransformData(data any) (string, error) {\n\tvar buf bytes.Buffer\n\terr := codec.NewEncoder(&buf, jsonHandlePretty).Encode(data)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\n\treturn buf.String(), nil\n}\n\ntype TemplateFormat struct {\n\ttmpl string\n}\n\n// TransformData returns template format string data.\nfunc (p *TemplateFormat) TransformData(data any) (string, error) {\n\tvar out bytes.Buffer\n\tif len(p.tmpl) == 0 {\n\t\treturn \"\", fmt.Errorf(\"template needs to be specified in golang's text/template format.\")\n\t}\n\n\tt, err := template.New(\"\").Funcs(makeFuncMap()).Parse(p.tmpl)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\n\terr = t.Execute(&out, data)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\treturn out.String(), nil\n}\n\nfunc Format(json bool, template string, data any) (string, error) {\n\tvar format string\n\tif json && len(template) > 0 {\n\t\treturn \"\", fmt.Errorf(\"Both json and template formatting are not allowed\")","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/command/data_format.go#L45-L81","documentation":"Guard in TemplateFormat.TransformData: the TemplateFormat was constructed with an empty template string, so it cannot render output; the caller must supply a Go text/template before using this output format.","triggerScenarios":"Calling command.DataFormat(\"template\", \"\") (empty tmpl) to get a TemplateFormat, then invoking TransformData on any data.","commonSituations":"Passing -format=template without also supplying -template; an environment variable or config default that resolves to an empty string; template flag value trimmed to empty by upstream flag handling.","solutions":["Supply a Go text/template string when selecting template format, e.g. -template '{{ .Name }}'.","Add a pre-check that the template flag is non-empty before choosing the template formatter.","Fix whatever default/env resolution is yielding an empty template value.","Fall back to json format when no template is provided."],"exampleFix":"// before\nf, _ := command.DataFormat(\"template\", os.Getenv(\"OUT_TMPL\")) // env empty\n// after\ntmpl := os.Getenv(\"OUT_TMPL\")\nif tmpl == \"\" { return errors.New(\"-template is required with format=template\") }\nf, err := command.DataFormat(\"template\", tmpl)","handlingStrategy":"validation","validationCode":"if len(tmpl) == 0 {\n    return errors.New(\"template format requires a non-empty -template value\")\n}\nif _, err := template.New(\"t\").Parse(tmpl); err != nil {\n    return fmt.Errorf(\"invalid template: %w\", err)\n}","typeGuard":"null","tryCatchPattern":"out, err := formatter.TransformData(data)\nif err != nil {\n    if strings.Contains(err.Error(), \"template needs to be specified\") {\n        return \"\", errors.New(\"-template is required when format=template\")\n    }\n    return \"\", err\n}","preventionTips":["Require the -template flag when format=template (mark it required conditionally).","Pre-parse the template at startup to fail fast on empty or invalid templates.","Avoid empty-defaulted env vars for template values; fail loudly when unset.","Add a smoke test rendering sample data through the template."],"tags":["template","cli","text-template"],"backgroundTag":"empty-template-specified","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}