Skip to content

Commit 9874953

Browse files
committed
cmd/parse: Move accidental pkg var to local var. (#7813)
This commit moves an accidental package-level definition of the `opa parse` CLI subcommand to a local variable inside the `initParse` function, similar to how we do command initialization for all other OPA CLI subcommands. Before this change, it was possible to see panics from the package variable `cobra.Command` in `parse.go` having some of its flags redefined. This fix makes it possible for `make generate-cli-docs` to run without error again. Signed-off-by: Philip Conrad <[email protected]> (cherry picked from commit 47e2b74)
1 parent 21a527d commit 9874953

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

cmd/parse.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,6 @@ var configuredParseParams = parseParams{
4646
jsonInclude: "",
4747
}
4848

49-
var parseCommand = &cobra.Command{
50-
Use: "parse <path>",
51-
Short: "Parse Rego source file",
52-
Long: `Parse Rego source file and print AST.`,
53-
PreRunE: func(cmd *cobra.Command, args []string) error {
54-
if len(args) == 0 {
55-
return errors.New("no source file specified")
56-
}
57-
return env.CmdFlags.CheckEnvironmentVariables(cmd)
58-
},
59-
RunE: func(cmd *cobra.Command, args []string) error {
60-
cmd.SilenceErrors = true
61-
cmd.SilenceUsage = true
62-
63-
exit := parse(args, &configuredParseParams, os.Stdout, os.Stderr)
64-
if exit != 0 {
65-
return newExitError(exit)
66-
}
67-
return nil
68-
},
69-
}
70-
7149
func parse(args []string, params *parseParams, stdout io.Writer, stderr io.Writer) int {
7250
if len(args) == 0 {
7351
return 0
@@ -140,6 +118,28 @@ func parse(args []string, params *parseParams, stdout io.Writer, stderr io.Write
140118
}
141119

142120
func initParse(root *cobra.Command, _ string) {
121+
parseCommand := &cobra.Command{
122+
Use: "parse <path>",
123+
Short: "Parse Rego source file",
124+
Long: `Parse Rego source file and print AST.`,
125+
PreRunE: func(cmd *cobra.Command, args []string) error {
126+
if len(args) == 0 {
127+
return errors.New("no source file specified")
128+
}
129+
return env.CmdFlags.CheckEnvironmentVariables(cmd)
130+
},
131+
RunE: func(cmd *cobra.Command, args []string) error {
132+
cmd.SilenceErrors = true
133+
cmd.SilenceUsage = true
134+
135+
exit := parse(args, &configuredParseParams, os.Stdout, os.Stderr)
136+
if exit != 0 {
137+
return newExitError(exit)
138+
}
139+
return nil
140+
},
141+
}
142+
143143
addOutputFormat(parseCommand.Flags(), configuredParseParams.format)
144144
parseCommand.Flags().StringVarP(&configuredParseParams.jsonInclude, "json-include", "", "", "include or exclude optional elements. By default comments are included. Current options: locations, comments. E.g. --json-include locations,-comments will include locations and exclude comments.")
145145
addV1CompatibleFlag(parseCommand.Flags(), &configuredParseParams.v1Compatible, false)

0 commit comments

Comments
 (0)