-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
policy_ls.go
136 lines (116 loc) 路 3.67 KB
/
policy_ls.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
129
130
131
132
133
134
135
136
// Copyright 2016-2023, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"errors"
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/pulumi/pulumi/pkg/v3/backend"
"github.com/pulumi/pulumi/pkg/v3/backend/display"
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/cmdutil"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
)
func newPolicyLsCmd() *cobra.Command {
var jsonOut bool
cmd := &cobra.Command{
Use: "ls [org-name]",
Args: cmdutil.MaximumNArgs(1),
Short: "List all Policy Packs for a Pulumi organization",
Long: "List all Policy Packs for a Pulumi organization",
Run: cmdutil.RunFunc(func(cmd *cobra.Command, cliArgs []string) error {
ctx := cmd.Context()
// Try to read the current project
project, _, err := readProject()
if err != nil && !errors.Is(err, workspace.ErrProjectNotFound) {
return err
}
// Get backend.
b, err := currentBackend(ctx, project, display.Options{Color: cmdutil.GetGlobalColorization()})
if err != nil {
return err
}
// Get organization.
var orgName string
if len(cliArgs) > 0 {
orgName = cliArgs[0]
} else {
orgName, _, _, err = b.CurrentUser()
if err != nil {
return err
}
}
// Gather all Policy Packs for the organization.
var (
allPolicyPacks []apitype.PolicyPackWithVersions
inContToken backend.ContinuationToken
)
for {
resp, outContToken, err := b.ListPolicyPacks(ctx, orgName, inContToken)
if err != nil {
return err
}
allPolicyPacks = append(allPolicyPacks, resp.PolicyPacks...)
if outContToken == nil {
break
}
inContToken = outContToken
}
if jsonOut {
return formatPolicyPacksJSON(allPolicyPacks)
}
return formatPolicyPacksConsole(allPolicyPacks)
}),
}
cmd.PersistentFlags().BoolVarP(
&jsonOut, "json", "j", false, "Emit output as JSON")
return cmd
}
func formatPolicyPacksConsole(policyPacks []apitype.PolicyPackWithVersions) error {
// Header string and formatting options to align columns.
headers := []string{"NAME", "VERSIONS"}
rows := []cmdutil.TableRow{}
for _, packs := range policyPacks {
// Name column
name := packs.Name
// Version Tags column
versionTags := strings.Trim(strings.ReplaceAll(fmt.Sprint(packs.VersionTags), " ", ", "), "[]")
// Render the columns.
columns := []string{name, versionTags}
rows = append(rows, cmdutil.TableRow{Columns: columns})
}
printTable(cmdutil.Table{
Headers: headers,
Rows: rows,
}, nil)
return nil
}
// policyPacksJSON is the shape of the --json output of this command. When --json is passed, we print an array
// of policyPacksJSON objects. While we can add fields to this structure in the future, we should not change
// existing fields.
type policyPacksJSON struct {
Name string `json:"name"`
Versions []string `json:"versions"`
}
func formatPolicyPacksJSON(policyPacks []apitype.PolicyPackWithVersions) error {
output := make([]policyPacksJSON, len(policyPacks))
for i, pack := range policyPacks {
output[i] = policyPacksJSON{
Name: pack.Name,
Versions: pack.VersionTags,
}
}
return printJSON(output)
}