Skip to content

Commit 481c3e8

Browse files
kevinstyrasrenatus
authored andcommitted
cmd: allow branding
This change allows users that build their own executable or "spin" of OPA to give it a name, and have it reference itself properly in help texts. It's a vanity thing, but I think some people would appreciate it, hat tip to the international association of pedants. Signed-off-by: Stephan Renatus <[email protected]> Co-authored-by: kevinstyra <[email protected]>
1 parent cba9eee commit 481c3e8

File tree

22 files changed

+212
-164
lines changed

22 files changed

+212
-164
lines changed

build/generate-cli-docs/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
func main() {
16-
command := cmd.RootCommand
16+
command := cmd.Command(nil, "opa")
1717
command.Use = "opa [command]"
1818
command.DisableAutoGenTag = true
1919

build/generate-man/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func main() {
2323
log.Fatal(err)
2424
}
2525

26-
cmd := cmd.RootCommand
26+
cmd := cmd.Command(nil, "OPA")
2727
cmd.Use = "opa [command]"
2828
cmd.DisableAutoGenTag = true
2929

cmd/bench.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ func newBenchmarkEvalParams() benchmarkCommandParams {
6363
}
6464
}
6565

66-
func init() {
66+
func initBench(root *cobra.Command, brand string) {
67+
executable := root.Name()
68+
6769
params := newBenchmarkEvalParams()
6870

6971
benchCommand := &cobra.Command{
@@ -76,9 +78,10 @@ evaluation will be repeated a number of times and performance results will be re
7678
7779
Example with bundle and input data:
7880
79-
opa bench -b ./policy-bundle -i input.json 'data.authz.allow'
81+
` + executable + ` bench -b ./policy-bundle -i input.json 'data.authz.allow'
8082
81-
To run benchmarks against a running OPA server to evaluate server overhead use the --e2e flag.
83+
To run benchmarks against a running ` + brand + ` server to evaluate server overhead use the --e2e flag.
84+
To enable more detailed analysis use the --metrics and --benchmem flags.
8285
8386
The optional "gobench" output format conforms to the Go Benchmark Data Format.
8487
`,
@@ -130,13 +133,13 @@ The optional "gobench" output format conforms to the Go Benchmark Data Format.
130133
addCountFlag(benchCommand.Flags(), &params.count, "benchmark")
131134
addBenchmemFlag(benchCommand.Flags(), &params.benchMem, true)
132135

133-
addE2EFlag(benchCommand.Flags(), &params.e2e, false)
136+
addE2EFlag(benchCommand.Flags(), &params.e2e, false, brand)
134137
addConfigFileFlag(benchCommand.Flags(), &params.configFile)
135138

136139
benchCommand.Flags().IntVar(&params.gracefulShutdownPeriod, "shutdown-grace-period", 10, "set the time (in seconds) that the server will wait to gracefully shut down. This flag is valid in 'e2e' mode only.")
137140
benchCommand.Flags().IntVar(&params.shutdownWaitPeriod, "shutdown-wait-period", 0, "set the time (in seconds) that the server will wait before initiating shutdown. This flag is valid in 'e2e' mode only.")
138141

139-
RootCommand.AddCommand(benchCommand)
142+
root.AddCommand(benchCommand)
140143
}
141144

142145
type benchRunner interface {

cmd/build.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,34 +69,35 @@ func (p *buildParams) regoVersion() ast.RegoVersion {
6969
return ast.DefaultRegoVersion
7070
}
7171

72-
func init() {
72+
func initBuild(root *cobra.Command, brand string) {
73+
executable := root.Name()
7374

7475
buildParams := newBuildParams()
7576

7677
var buildCommand = &cobra.Command{
7778
Use: "build <path> [<path> [...]]",
78-
Short: "Build an OPA bundle",
79-
Long: `Build an OPA bundle.
79+
Short: `Build an ` + brand + ` bundle`,
80+
Long: `Build an ` + brand + ` bundle.
8081
81-
The 'build' command packages OPA policy and data files into bundles. Bundles are
82+
The 'build' command packages ` + brand + ` policy and data files into bundles. Bundles are
8283
gzipped tarballs containing policies and data. Paths referring to directories are
8384
loaded recursively.
8485
8586
$ ls
8687
example.rego
8788
88-
$ opa build -b .
89+
$ ` + executable + ` build -b .
8990
90-
You can load bundles into OPA on the command-line:
91+
You can load bundles into ` + brand + ` on the command-line:
9192
9293
$ ls
9394
bundle.tar.gz example.rego
9495
95-
$ opa run bundle.tar.gz
96+
$ ` + executable + ` run bundle.tar.gz
9697
97-
You can also configure OPA to download bundles from remote HTTP endpoints:
98+
You can also configure ` + brand + ` to download bundles from remote HTTP endpoints:
9899
99-
$ opa run --server \
100+
$ ` + executable + ` run --server \
100101
--set bundles.example.resource=bundle.tar.gz \
101102
--set services.example.url=http://localhost:8080
102103
@@ -136,9 +137,9 @@ The 'build' command supports targets (specified by -t):
136137
original policy or data files.
137138
138139
plan The plan target emits a bundle containing a plan, i.e., an intermediate
139-
representation compiled from the input files for each specified entrypoint.
140-
This is for further processing, OPA cannot evaluate a "plan bundle" like it
141-
can evaluate a wasm or rego bundle.
140+
representation compiled from the input files for each specified entrypoint.
141+
This is for further processing, ` + brand + ` cannot evaluate a "plan bundle" like it
142+
can evaluate a wasm or rego bundle.
142143
143144
The -e flag tells the 'build' command which documents (entrypoints) will be queried by
144145
the software asking for policy decisions, so that it can focus optimization efforts and
@@ -161,7 +162,7 @@ https://www.openpolicyagent.org/docs/latest/management-bundles/#signing.
161162
162163
Example:
163164
164-
$ opa build --verification-key /path/to/public_key.pem --signing-key /path/to/private_key.pem --bundle foo
165+
$ ` + executable + ` build --verification-key /path/to/public_key.pem --signing-key /path/to/private_key.pem --bundle foo
165166
166167
Where foo has the following structure:
167168
@@ -196,7 +197,7 @@ see https://www.openpolicyagent.org/docs/latest/management-bundles/#signature-fo
196197
Capabilities
197198
------------
198199
199-
The 'build' command can validate policies against a configurable set of OPA capabilities.
200+
The 'build' command can validate policies against a configurable set of ` + brand + ` capabilities.
200201
The capabilities define the built-in functions and other language features that policies
201202
may depend on. For example, the following capabilities file only permits the policy to
202203
depend on the "plus" built-in function ('+'):
@@ -224,12 +225,12 @@ depend on the "plus" built-in function ('+'):
224225
]
225226
}
226227
227-
Capabilities can be used to validate policies against a specific version of OPA.
228-
The OPA repository contains a set of capabilities files for each OPA release. For example,
228+
Capabilities can be used to validate policies against a specific version of ` + brand + `.
229+
The ` + brand + ` repository contains a set of capabilities files for each ` + brand + ` release. For example,
229230
the following command builds a directory of policies ('./policies') and validates them
230-
against OPA v0.22.0:
231+
against ` + brand + ` v0.22.0:
231232
232-
opa build ./policies --capabilities v0.22.0
233+
` + executable + ` build ./policies --capabilities v0.22.0
233234
`,
234235
PreRunE: func(Cmd *cobra.Command, args []string) error {
235236
if len(args) == 0 {
@@ -279,7 +280,7 @@ against OPA v0.22.0:
279280
addV0CompatibleFlag(buildCommand.Flags(), &buildParams.v0Compatible, false)
280281
addV1CompatibleFlag(buildCommand.Flags(), &buildParams.v1Compatible, false)
281282

282-
RootCommand.AddCommand(buildCommand)
283+
root.AddCommand(buildCommand)
283284
}
284285

285286
func dobuild(params buildParams, args []string) error {

cmd/capabilities.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,21 @@ func (p *capabilitiesParams) regoVersion() ast.RegoVersion {
2828
return ast.DefaultRegoVersion
2929
}
3030

31-
func init() {
31+
func initCapabilities(root *cobra.Command, brand string) {
32+
executable := root.Name()
3233

3334
capabilitiesParams := capabilitiesParams{}
3435

3536
var capabilitiesCommand = &cobra.Command{
3637
Use: "capabilities",
37-
Short: "Print the capabilities of OPA",
38-
Long: `Show capabilities for OPA.
38+
Short: "Print the capabilities of " + brand,
39+
Long: `Show capabilities for ` + brand + `.
3940
40-
The 'capabilities' command prints the OPA capabilities, prior to and including the version of OPA used.
41+
The 'capabilities' command prints the ` + brand + ` capabilities, prior to and including the version of ` + brand + ` used.
4142
4243
Print a list of all existing capabilities version names
4344
44-
$ opa capabilities
45+
$ ` + executable + ` capabilities
4546
v0.17.0
4647
v0.17.1
4748
...
@@ -52,7 +53,7 @@ Print a list of all existing capabilities version names
5253
5354
Print the capabilities of the current version
5455
55-
$ opa capabilities --current
56+
$ ` + executable + ` capabilities --current
5657
{
5758
"builtins": [...],
5859
"future_keywords": [...],
@@ -61,7 +62,7 @@ Print the capabilities of the current version
6162
6263
Print the capabilities of a specific version
6364
64-
$ opa capabilities --version v0.32.1
65+
$ ` + executable + ` capabilities --version v0.32.1
6566
{
6667
"builtins": [...],
6768
"future_keywords": null,
@@ -70,7 +71,7 @@ Print the capabilities of a specific version
7071
7172
Print the capabilities of a capabilities file
7273
73-
$ opa capabilities --file ./capabilities/v0.32.1.json
74+
$ ` + executable + ` capabilities --file ./capabilities/v0.32.1.json
7475
{
7576
"builtins": [...],
7677
"future_keywords": null,
@@ -98,7 +99,7 @@ Print the capabilities of a capabilities file
9899
capabilitiesCommand.Flags().StringVar(&capabilitiesParams.file, "file", "", "print capabilities defined by a file")
99100
addV0CompatibleFlag(capabilitiesCommand.Flags(), &capabilitiesParams.v0Compatible, false)
100101

101-
RootCommand.AddCommand(capabilitiesCommand)
102+
root.AddCommand(capabilitiesCommand)
102103
}
103104

104105
func doCapabilities(params capabilitiesParams) (string, error) {

cmd/check.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func outputErrors(format string, err error) {
173173
}
174174
}
175175

176-
func init() {
176+
func initCheck(root *cobra.Command, _ string) {
177177
checkParams := newCheckParams()
178178

179179
checkCommand := &cobra.Command{
@@ -215,5 +215,6 @@ and exit with a non-zero exit code.`,
215215
"check for Rego v0 and v1 compatibility (policies must be compatible with both Rego versions)")
216216
addV0CompatibleFlag(checkCommand.Flags(), &checkParams.v0Compatible, false)
217217
addV1CompatibleFlag(checkCommand.Flags(), &checkParams.v1Compatible, false)
218-
RootCommand.AddCommand(checkCommand)
218+
219+
root.AddCommand(checkCommand)
219220
}

cmd/commands.go

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,45 @@
55
package cmd
66

77
import (
8-
"os"
9-
"path"
10-
118
"github.com/spf13/cobra"
9+
10+
iversion "github.com/open-policy-agent/opa/internal/version"
1211
)
1312

14-
// RootCommand is the base CLI command that all subcommands are added to.
15-
var RootCommand = &cobra.Command{
16-
Use: path.Base(os.Args[0]),
17-
Short: "Open Policy Agent (OPA)",
18-
Long: "An open source project to policy-enable your service.",
13+
// UserAgent lets you override the OPA UA sent with all the HTTP requests.
14+
// It's another vanity thing -- if you build your own version of OPA, you
15+
// may want to adjust this.
16+
// NOTE(sr): Caution: Please consider this experimental, I have the hunch
17+
// that we'll find a better way to make this adjustment in the future.
18+
func UserAgent(agent string) {
19+
iversion.UserAgent = agent
20+
}
21+
22+
func Command(rootCommand *cobra.Command, brand string) *cobra.Command {
23+
// rootCommand is the base CLI command that all subcommands are added to.
24+
if rootCommand == nil {
25+
rootCommand = &cobra.Command{
26+
Use: "opa",
27+
Short: "Open Policy Agent (OPA)",
28+
Long: "An open source project to policy-enable your service.",
29+
}
30+
}
31+
32+
initBench(rootCommand, brand)
33+
initBuild(rootCommand, brand)
34+
initCapabilities(rootCommand, brand)
35+
initCheck(rootCommand, brand)
36+
initDeps(rootCommand, brand)
37+
initEval(rootCommand, brand)
38+
initExec(rootCommand, brand)
39+
initFmt(rootCommand, brand)
40+
initInspect(rootCommand, brand)
41+
initOracle(rootCommand, brand)
42+
initParse(rootCommand, brand)
43+
initRefactor(rootCommand, brand)
44+
initRun(rootCommand, brand)
45+
initSign(rootCommand, brand)
46+
initTest(rootCommand, brand)
47+
initVersion(rootCommand, brand)
48+
return rootCommand
1949
}

cmd/deps.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ func newDepsCommandParams() depsCommandParams {
5050
}
5151
}
5252

53-
func init() {
53+
func initDeps(root *cobra.Command, _ string) {
54+
executable := root.Name()
55+
5456
params := newDepsCommandParams()
5557

5658
depsCommand := &cobra.Command{
@@ -72,9 +74,9 @@ Given a policy like this:
7274
is_admin if "admin" in input.user.roles
7375
7476
To evaluate the dependencies of a simple query (e.g. data.policy.allow),
75-
we'd run opa deps like demonstrated below:
77+
we'd run ` + executable + ` deps like demonstrated below:
7678
77-
$ opa deps --data policy.rego data.policy.allow
79+
$ ` + executable + ` deps --data policy.rego data.policy.allow
7880
+------------------+----------------------+
7981
| BASE DOCUMENTS | VIRTUAL DOCUMENTS |
8082
+------------------+----------------------+
@@ -109,7 +111,7 @@ data.policy.is_admin.
109111
addOutputFormat(depsCommand.Flags(), params.outputFormat)
110112
addV1CompatibleFlag(depsCommand.Flags(), &params.v1Compatible, false)
111113

112-
RootCommand.AddCommand(depsCommand)
114+
root.AddCommand(depsCommand)
113115
}
114116

115117
func deps(args []string, params depsCommandParams, w io.Writer) error {

cmd/eval.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ func (r regoError) Unwrap() error {
187187
return r.wrapped
188188
}
189189

190-
func init() {
190+
func initEval(root *cobra.Command, _ string) {
191+
executable := root.Name()
192+
191193
params := newEvalCommandParams()
192194

193195
evalCommand := &cobra.Command{
@@ -198,15 +200,15 @@ func init() {
198200
199201
To evaluate a simple query:
200202
201-
$ opa eval 'x := 1; y := 2; x < y'
203+
$ ` + executable + ` eval 'x := 1; y := 2; x < y'
202204
203205
To evaluate a query against JSON data:
204206
205-
$ opa eval --data data.json 'name := data.names[_]'
207+
$ ` + executable + ` eval --data data.json 'name := data.names[_]'
206208
207209
To evaluate a query against JSON data supplied with a file:// URL:
208210
209-
$ opa eval --data file:///path/to/file.json 'data'
211+
$ ` + executable + ` eval --data file:///path/to/file.json 'data'
210212
211213
212214
File & Bundle Loading
@@ -216,7 +218,7 @@ The --bundle flag will load data files and Rego files contained
216218
in the bundle specified by the path. It can be either a
217219
compressed tar archive bundle file or a directory tree.
218220
219-
$ opa eval --bundle /some/path 'data'
221+
$ ` + executable + ` eval --bundle /some/path 'data'
220222
221223
Where /some/path contains:
222224
@@ -269,8 +271,8 @@ Schema
269271
The -s/--schema flag provides one or more JSON Schemas used to validate references to the input or data documents.
270272
Loads a single JSON file, applying it to the input document; or all the schema files under the specified directory.
271273
272-
$ opa eval --data policy.rego --input input.json --schema schema.json
273-
$ opa eval --data policy.rego --input input.json --schema schemas/
274+
$ ` + executable + ` eval --data policy.rego --input input.json --schema schema.json
275+
$ ` + executable + ` eval --data policy.rego --input input.json --schema schemas/
274276
275277
Capabilities
276278
------------
@@ -364,7 +366,7 @@ access.
364366
addV1CompatibleFlag(evalCommand.Flags(), &params.v1Compatible, false)
365367
addReadAstValuesFromStoreFlag(evalCommand.Flags(), &params.ReadAstValuesFromStore, false)
366368

367-
RootCommand.AddCommand(evalCommand)
369+
root.AddCommand(evalCommand)
368370
}
369371

370372
func eval(args []string, params evalCommandParams, w io.Writer) (bool, error) {

0 commit comments

Comments
 (0)