@@ -38,6 +38,7 @@ const (
3838type inspectCommandParams struct {
3939 outputFormat * util.EnumFlag
4040 listAnnotations bool
41+ dataPaths repeatedStringFlag
4142 v0Compatible bool
4243 v1Compatible bool
4344}
@@ -56,6 +57,7 @@ func newInspectCommandParams() inspectCommandParams {
5657 return inspectCommandParams {
5758 outputFormat : formats .Flag (formats .Pretty , formats .JSON ),
5859 listAnnotations : false ,
60+ dataPaths : newrepeatedStringFlag ([]string {}),
5961 }
6062}
6163
@@ -66,12 +68,12 @@ func initInspect(root *cobra.Command, brand string) {
6668
6769 inspectCommand := & cobra.Command {
6870 Use : "inspect <path> [<path> [...]]" ,
69- Short : `Inspect ` + brand + ` bundle(s)` ,
70- Long : `Inspect ` + brand + ` bundle(s).
71+ Short : `Inspect ` + brand + ` bundle(s), Rego files, or data files ` ,
72+ Long : `Inspect ` + brand + ` bundle(s), Rego files, or data files .
7173
72- The 'inspect' command provides a summary of the contents in ` + brand + ` bundle(s) or a single Rego file.
73- Bundles are gzipped tarballs containing policies and data. The 'inspect' command reads bundle(s) and lists
74- the following:
74+ The 'inspect' command provides a summary of the contents in ` + brand + ` bundle(s), a single Rego file,
75+ or data files. Bundles are gzipped tarballs containing policies and data. The 'inspect' command reads
76+ bundle(s) and lists the following:
7577
7678* packages that are contributed by .rego files
7779* data locations defined by the data.json and data.yaml files
@@ -80,16 +82,24 @@ the following:
8082* information about the Wasm module files
8183* package- and rule annotations
8284
83- Example :
85+ Examples :
8486
87+ # Inspect a bundle
8588 $ ls
8689 bundle.tar.gz
8790 $ ` + executable + ` inspect bundle.tar.gz
8891
92+ # Inspect data files
93+ $ ` + executable + ` inspect --data data.json
94+ $ ` + executable + ` inspect --data config.yaml
95+
8996You can provide exactly one ` + brand + ` bundle, to a bundle directory, or direct path to a Rego file to the 'inspect'
9097command on the command-line. If you provide a path referring to a directory, the 'inspect' command will load that path as
91- a bundle and summarize its structure and contents. If you provide a path referring to a Rego file, the 'inspect' command
98+ a bundle and summarize its structure and contents. If you provide a path referring to a Rego file, the 'inspect' command
9299will load that file and summarize its structure and contents.
100+
101+ Alternatively, you can use the --data flag to inspect individual JSON or YAML data files. This flag can
102+ be repeated to inspect multiple data files.
93103` ,
94104 PreRunE : func (cmd * cobra.Command , args []string ) error {
95105 if err := validateInspectParams (& params , args ); err != nil {
@@ -101,7 +111,11 @@ will load that file and summarize its structure and contents.
101111 cmd .SilenceErrors = true
102112 cmd .SilenceUsage = true
103113
104- if err := doInspect (params , args [0 ], os .Stdout ); err != nil {
114+ path := ""
115+ if len (args ) > 0 {
116+ path = args [0 ]
117+ }
118+ if err := doInspect (params , path , os .Stdout ); err != nil {
105119 fmt .Fprintln (os .Stderr , "error:" , err )
106120 return err
107121 }
@@ -111,13 +125,21 @@ will load that file and summarize its structure and contents.
111125
112126 addOutputFormat (inspectCommand .Flags (), params .outputFormat )
113127 addListAnnotations (inspectCommand .Flags (), & params .listAnnotations )
128+ addDataFlag (inspectCommand .Flags (), & params .dataPaths )
114129 addV0CompatibleFlag (inspectCommand .Flags (), & params .v0Compatible , false )
115130 addV1CompatibleFlag (inspectCommand .Flags (), & params .v1Compatible , false )
116131 root .AddCommand (inspectCommand )
117132}
118133
119134func doInspect (params inspectCommandParams , path string , out io.Writer ) error {
120- info , err := ib .FileForRegoVersion (params .regoVersion (), path , params .listAnnotations )
135+ var info * ib.Info
136+ var err error
137+
138+ if params .dataPaths .isFlagSet () {
139+ info , err = ib .DataFileInfo (params .dataPaths .v )
140+ } else {
141+ info , err = ib .FileForRegoVersion (params .regoVersion (), path , params .listAnnotations )
142+ }
121143 if err != nil {
122144 return err
123145 }
@@ -168,7 +190,11 @@ func hasManifest(info *ib.Info) bool {
168190}
169191
170192func validateInspectParams (p * inspectCommandParams , args []string ) error {
171- if len (args ) != 1 {
193+ if p .dataPaths .isFlagSet () {
194+ if len (args ) > 0 {
195+ return errors .New ("specify either a bundle/path argument or --data flag, not both" )
196+ }
197+ } else if len (args ) != 1 {
172198 return errors .New ("specify exactly one OPA bundle or path" )
173199 }
174200
0 commit comments