/
org_search.go
344 lines (306 loc) 路 8.98 KB
/
org_search.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// 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 (
"context"
"encoding/csv"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strconv"
"github.com/pkg/browser"
"github.com/pulumi/pulumi/pkg/v3/backend"
"github.com/pulumi/pulumi/pkg/v3/backend/display"
"github.com/pulumi/pulumi/pkg/v3/backend/httpstate"
"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/util/logging"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
"github.com/spf13/cobra"
auto_table "go.pennock.tech/tabular/auto"
"gopkg.in/yaml.v3"
)
type Delimiter rune
func (d *Delimiter) String() string {
return string(*d)
}
func (d *Delimiter) Set(v string) error {
if v == "\\t" {
*d = Delimiter('\t')
} else if len(v) != 1 {
return errors.New("delimiter must be a single character")
} else {
*d = Delimiter(v[0])
}
return nil
}
func (d *Delimiter) Type() string {
return "Delimiter"
}
func (d *Delimiter) Rune() rune {
return rune(*d)
}
type outputFormat string
const (
outputFormatTable outputFormat = "table"
outputFormatJSON outputFormat = "json"
outputFormatYAML outputFormat = "yaml"
outputFormatCSV outputFormat = "csv"
)
// String is used both by fmt.Print and by Cobra in help text
func (o *outputFormat) String() string {
return string(*o)
}
// Set must have pointer receiver so it doesn't change the value of a copy
func (o *outputFormat) Set(v string) error {
switch v {
case "csv", "table", "json", "yaml":
*o = outputFormat(v)
return nil
default:
return errors.New(`must be one of "csv", "table", "json", or "yaml"`)
}
}
// Type is only used in help text
func (o *outputFormat) Type() string {
return "outputFormat"
}
type searchCmd struct {
orgName string
csvDelimiter Delimiter
outputFormat
openWeb bool
Stdout io.Writer // defaults to os.Stdout
// currentBackend is a reference to the top-level currentBackend function.
// This is used to override the default implementation for testing purposes.
currentBackend func(context.Context, *workspace.Project, display.Options) (backend.Backend, error)
}
type orgSearchCmd struct {
searchCmd
queryParams []string
}
func (cmd *orgSearchCmd) Run(ctx context.Context, args []string) error {
interactive := cmdutil.Interactive()
if cmd.outputFormat == "" {
cmd.outputFormat = outputFormatTable
}
if cmd.Stdout == nil {
cmd.Stdout = os.Stdout
}
if cmd.currentBackend == nil {
cmd.currentBackend = currentBackend
}
currentBackend := cmd.currentBackend // shadow the top-level function
opts := backend.QueryOptions{}
opts.Display = display.Options{
Color: cmdutil.GetGlobalColorization(),
IsInteractive: interactive,
Type: display.DisplayQuery,
}
// Try to read the current project
project, _, err := readProject()
if err != nil && !errors.Is(err, workspace.ErrProjectNotFound) {
return err
}
backend, err := currentBackend(ctx, project, opts.Display)
if err != nil {
return err
}
cloudBackend, isCloud := backend.(httpstate.Backend)
if !isCloud {
return errors.New("Pulumi AI search is only supported for the Pulumi Cloud")
}
defaultOrg, err := workspace.GetBackendConfigDefaultOrg(project)
if err != nil {
return err
}
userName, orgs, _, err := cloudBackend.CurrentUser()
if err != nil {
return err
}
if defaultOrg != "" && cmd.orgName == "" {
cmd.orgName = defaultOrg
}
if cmd.orgName != "" {
if !sliceContains(orgs, cmd.orgName) {
return fmt.Errorf("user %s is not a member of organization %s", userName, cmd.orgName)
}
} else {
cmd.orgName = userName
}
parsedQueryParams := apitype.ParseQueryParams(cmd.queryParams)
res, err := cloudBackend.Search(ctx, cmd.orgName, parsedQueryParams)
if err != nil {
return err
}
err = cmd.outputFormat.Render(&cmd.searchCmd, res)
if err != nil {
return fmt.Errorf("table rendering error: %w", err)
}
if cmd.openWeb {
err = browser.OpenURL(res.URL)
if err != nil {
logging.Warningf("failed to open URL: %s", err)
}
}
return nil
}
func newSearchCmd() *cobra.Command {
var scmd orgSearchCmd
cmd := &cobra.Command{
Use: "search",
Short: "Search for resources in Pulumi Cloud",
Long: "Search for resources in Pulumi Cloud.",
Args: cmdutil.NoArgs,
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
if len(scmd.queryParams) == 0 {
return cmd.Help()
}
return scmd.Run(ctx, args)
},
),
}
cmd.AddCommand(newSearchAICmd())
cmd.PersistentFlags().StringVar(
&scmd.orgName, "org", "",
"Name of the organization to search. Defaults to the current user's default organization.",
)
cmd.PersistentFlags().StringArrayVarP(
&scmd.queryParams, "query", "q", nil,
"A Pulumi Query to send to Pulumi Cloud for resource search."+
"May be formatted as a single query, or multiple:\n"+
"\t-q \"type:aws:s3/bucket:Bucket modified:>=2023-09-01\"\n"+
"\t-q \"type:aws:s3/bucket:Bucket\" -q \"modified:>=2023-09-01\"\n"+
"See https://www.pulumi.com/docs/pulumi-cloud/insights/search/#query-syntax for syntax reference.",
)
cmd.PersistentFlags().VarP(
&scmd.outputFormat, "output", "o",
"Output format. Supported formats are 'table', 'json', 'csv', and 'yaml'.",
)
cmd.PersistentFlags().Var(
&scmd.csvDelimiter, "delimiter",
"Delimiter to use when rendering CSV output.",
)
cmd.PersistentFlags().BoolVar(
&scmd.openWeb, "web", false,
"Open the search results in a web browser.",
)
return cmd
}
func sliceContains(slice []string, search string) bool {
for _, s := range slice {
if s == search {
return true
}
}
return false
}
func renderSearchTable(w io.Writer, results *apitype.ResourceSearchResponse) error {
urlInfo := "Results are also visible in Pulumi Cloud:"
table := auto_table.New("utf8-heavy")
table.AddHeaders("Project", "Stack", "Name", "Type", "Package", "Module", "Modified")
for _, r := range results.Resources {
table.AddRowItems(*r.Program, *r.Stack, *r.Name, *r.Type, *r.Package, *r.Module, *r.Modified)
}
if errs := table.Errors(); errs != nil {
return errors.Join(errs...)
}
err := table.RenderTo(w)
if err != nil {
return err
}
_, err = w.Write(
[]byte(
fmt.Sprintf(
"Displaying %s of %s total results.\n",
strconv.Itoa(len(results.Resources)),
strconv.FormatInt(*results.Total, 10))),
)
if err != nil {
return err
}
_, err = w.Write([]byte(urlInfo + "\n" + results.URL + "\n"))
if err != nil {
return err
}
_, err = w.Write(
[]byte(fmt.Sprintf("\nResources displayed are the result of the following Pulumi query:\n%s\n", results.Query)),
)
return err
}
func (cmd *searchCmd) RenderTable(results *apitype.ResourceSearchResponse) error {
return renderSearchTable(cmd.Stdout, results)
}
func renderSearchCSV(w io.Writer, results []apitype.ResourceResult, delimiter rune) error {
data := make([][]string, 0, len(results)+1)
writer := csv.NewWriter(w)
if delimiter != 0 {
writer.Comma = delimiter
}
data = append(data, []string{"Project", "Stack", "Name", "Type", "Package", "Module", "Modified"})
for _, result := range results {
data = append(data, []string{
*result.Program,
*result.Stack,
*result.Name,
*result.Type,
*result.Package,
*result.Module,
*result.Modified,
})
}
return writer.WriteAll(data)
}
func (cmd *searchCmd) RenderCSV(results []apitype.ResourceResult, delimiter rune) error {
return renderSearchCSV(cmd.Stdout, results, delimiter)
}
func renderSearchJSON(w io.Writer, results []apitype.ResourceResult) error {
output, err := json.MarshalIndent(results, "", " ")
if err != nil {
return err
}
_, err = w.Write(output)
return err
}
func (o *outputFormat) Render(cmd *searchCmd, result *apitype.ResourceSearchResponse) error {
switch *o {
case outputFormatJSON:
return cmd.RenderJSON(result)
case outputFormatTable:
return cmd.RenderTable(result)
case outputFormatYAML:
return cmd.RenderYAML(result)
case outputFormatCSV:
return cmd.RenderCSV(result.Resources, cmd.csvDelimiter.Rune())
default:
return fmt.Errorf("unknown output format %q", *o)
}
}
func (cmd *searchCmd) RenderJSON(result *apitype.ResourceSearchResponse) error {
return renderSearchJSON(cmd.Stdout, result.Resources)
}
func renderSearchYAML(w io.Writer, results []apitype.ResourceResult) error {
output, err := yaml.Marshal(results)
if err != nil {
return err
}
_, err = w.Write(output)
return err
}
func (cmd *searchCmd) RenderYAML(result *apitype.ResourceSearchResponse) error {
return renderSearchYAML(cmd.Stdout, result.Resources)
}