{"record":{"id":"f8f34c070c8aae63","repo":"hashicorp/nomad","slug":"both-json-and-template-formatting-are-not-allowed","errorCode":null,"errorMessage":"Both json and template formatting are not allowed","messagePattern":"Both json and template formatting are not allowed","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command/data_format.go","lineNumber":81,"sourceCode":"\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\")\n\t} else if json {\n\t\tformat = \"json\"\n\t} else if len(template) > 0 {\n\t\tformat = \"template\"\n\t} else {\n\t\treturn \"\", fmt.Errorf(\"no formatting option given\")\n\t}\n\n\tf, err := DataFormat(format, template)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\n\tout, err := f.TransformData(data)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"Error formatting the data: %w\", err)\n\t}\n","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/command/data_format.go#L63-L99","documentation":"Format (command/data_format.go) is the CLI-level helper that maps the boolean -json flag and -template string into a DataFormat call. It rejects the mutually exclusive combination of json=true and a non-empty template, since output can only be rendered one way.","triggerScenarios":"Calling command.Format(true, tmpl, data) with len(tmpl) > 0 — e.g. a CLI invocation that sets both -json and -template.","commonSituations":"Users passing both -json and -template to commands like consul services list or catalog commands; wrapper scripts that always append -json while also injecting a template; migration from old flag combinations after this validation was added.","solutions":["Pass only one formatting option: either json=true or a non-empty template.","Fix wrapper scripts so -template is dropped when -json is present (or vice versa).","If you need json filtered through a template, use format=template and pipe data through jq instead.","Add CLI-level flag validation to fail fast with a clear usage message."],"exampleFix":"// before\nout, err := command.Format(true, tmpl, data) // both set\n// after\nout, err := command.Format(len(tmpl) == 0, tmpl, data) // or drop one option","handlingStrategy":"validation","validationCode":"if json && len(template) > 0 {\n    return errors.New(\"-json and -template are mutually exclusive\")\n}","typeGuard":"null","tryCatchPattern":"out, err := command.Format(jsonFlag, tmplFlag, data)\nif err != nil {\n    if strings.Contains(err.Error(), \"Both json and template\") {\n        return usageError(\"pass either -json or -template, not both\")\n    }\n    return err\n}","preventionTips":["Mark -json and -template mutually exclusive in your CLI framework (e.g. cobra MarkFlagsMutuallyExclusive).","Centralize output-format flag handling so every command shares the same guard.","Reject conflicting combinations in flag parsing before invoking Format.","Show usage/help text when conflicting flags are detected."],"tags":["cli","json","template","flag-parsing"],"backgroundTag":"conflicting-output-format-options","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"}