forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapabilities.go
128 lines (103 loc) · 2.83 KB
/
capabilities.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2022 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package cmd
import (
"encoding/json"
"fmt"
"strings"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/cmd/internal/env"
"github.com/spf13/cobra"
)
type capabilitiesParams struct {
showCurrent bool
version string
file string
}
func init() {
capabilitiesParams := capabilitiesParams{}
var capabilitiesCommand = &cobra.Command{
Use: "capabilities",
Short: "Print the capabilities of OPA",
Long: `Show capabilities for OPA.
The 'capabilities' command prints the OPA capabilities, prior to and including the version of OPA used.
Print a list of all existing capabilities version names
$ opa capabilities
v0.17.0
v0.17.1
...
v0.37.1
v0.37.2
v0.38.0
...
Print the capabilities of the current version
$ opa capabilities --current
{
"builtins": [...],
"future_keywords": [...],
"wasm_abi_versions": [...]
}
Print the capabilities of a specific version
$ opa capabilities --version v0.32.1
{
"builtins": [...],
"future_keywords": null,
"wasm_abi_versions": [...]
}
Print the capabilities of a capabilities file
$ opa capabilities --file ./capabilities/v0.32.1.json
{
"builtins": [...],
"future_keywords": null,
"wasm_abi_versions": [...]
}
`,
PreRunE: func(cmd *cobra.Command, _ []string) error {
return env.CmdFlags.CheckEnvironmentVariables(cmd)
},
RunE: func(*cobra.Command, []string) error {
cs, err := doCapabilities(capabilitiesParams)
if err != nil {
return err
}
fmt.Println(cs)
return nil
},
}
capabilitiesCommand.Flags().BoolVar(&capabilitiesParams.showCurrent, "current", false, "print current capabilities")
capabilitiesCommand.Flags().StringVar(&capabilitiesParams.version, "version", "", "print capabilities of a specific version")
capabilitiesCommand.Flags().StringVar(&capabilitiesParams.file, "file", "", "print current capabilities")
RootCommand.AddCommand(capabilitiesCommand)
}
func doCapabilities(params capabilitiesParams) (string, error) {
var (
c *ast.Capabilities
err error
)
if len(params.version) > 0 {
c, err = ast.LoadCapabilitiesVersion(params.version)
} else if len(params.file) > 0 {
c, err = ast.LoadCapabilitiesFile(params.file)
} else if params.showCurrent {
c = ast.CapabilitiesForThisVersion()
} else {
return showVersions()
}
if err != nil {
return "", err
}
bs, err := json.MarshalIndent(c, "", " ")
if err != nil {
return "", err
}
return string(bs), nil
}
func showVersions() (string, error) {
cvs, err := ast.LoadCapabilitiesVersions()
if err != nil {
return "", err
}
t := strings.Join(cvs, "\n")
return t, nil
}