Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions cmd/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,6 @@ var configuredParseParams = parseParams{
jsonInclude: "",
}

var parseCommand = &cobra.Command{
Use: "parse <path>",
Short: "Parse Rego source file",
Long: `Parse Rego source file and print AST.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("no source file specified")
}
return env.CmdFlags.CheckEnvironmentVariables(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true
cmd.SilenceUsage = true

exit := parse(args, &configuredParseParams, os.Stdout, os.Stderr)
if exit != 0 {
return newExitError(exit)
}
return nil
},
}

func parse(args []string, params *parseParams, stdout io.Writer, stderr io.Writer) int {
if len(args) == 0 {
return 0
Expand Down Expand Up @@ -140,6 +118,28 @@ func parse(args []string, params *parseParams, stdout io.Writer, stderr io.Write
}

func initParse(root *cobra.Command, _ string) {
parseCommand := &cobra.Command{
Use: "parse <path>",
Short: "Parse Rego source file",
Long: `Parse Rego source file and print AST.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("no source file specified")
}
return env.CmdFlags.CheckEnvironmentVariables(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true
cmd.SilenceUsage = true

exit := parse(args, &configuredParseParams, os.Stdout, os.Stderr)
if exit != 0 {
return newExitError(exit)
}
return nil
},
}

addOutputFormat(parseCommand.Flags(), configuredParseParams.format)
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.")
addV1CompatibleFlag(parseCommand.Flags(), &configuredParseParams.v1Compatible, false)
Expand Down