-
Notifications
You must be signed in to change notification settings - Fork 141
/
list.go
121 lines (104 loc) · 3.5 KB
/
list.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
// Copyright 2023 The PipeCD Authors.
//
// 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 deployment
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/pipe-cd/pipecd/pkg/app/server/service/apiservice"
"github.com/pipe-cd/pipecd/pkg/cli"
"github.com/pipe-cd/pipecd/pkg/model"
)
type list struct {
root *command
statuses []string
appKinds []string
appIds []string
appName string
labels []string
limit int32
cursor string
stdout io.Writer
}
func newListCommand(root *command) *cobra.Command {
c := &list{
root: root,
stdout: os.Stdout,
}
cmd := &cobra.Command{
Use: "list",
Short: "Show the list of deployments.",
RunE: cli.WithContext(c.run),
}
cmd.Flags().StringSliceVar(&c.statuses, "status", c.statuses, fmt.Sprintf("The list of application statuses to filter. (%s)", strings.Join(model.DeploymentStatusStrings(), "|")))
cmd.Flags().StringSliceVar(&c.appIds, "app-id", c.appIds, fmt.Sprintf("The list of application ids to filter. (%s)", strings.Join(model.ApplicationKindStrings(), "|")))
cmd.Flags().StringSliceVar(&c.appKinds, "app-kind", c.appKinds, fmt.Sprintf("The list of application kinds to filter. (%s)", strings.Join(model.ApplicationKindStrings(), "|")))
cmd.Flags().StringVar(&c.appName, "app-name", c.appName, "The application name to filter.")
cmd.Flags().StringVar(&c.cursor, "cursor", c.cursor, "The cursor which returned by the previous request applications list.")
cmd.Flags().Int32Var(&c.limit, "limit", 30, "Upper limit on the number of return values. Default value is 30.")
cmd.Flags().StringSliceVar(&c.labels, "label", c.labels, "The list of labels to filter. Expect input in the form KEY:VALUE.")
return cmd
}
func (c *list) run(ctx context.Context, _ cli.Input) error {
for _, status := range c.statuses {
if status != "" {
if _, ok := model.DeploymentStatus_value[status]; !ok {
return errors.Errorf("%s is invalid deployment status", status)
}
}
}
for _, kind := range c.appKinds {
if kind != "" {
if _, ok := model.ApplicationKind_value[kind]; !ok {
return errors.Errorf("%s is invalid application kind", kind)
}
}
}
labels := map[string]string{}
for _, label := range c.labels {
sp := strings.SplitN(label, ":", 2)
if len(sp) == 2 {
labels[sp[0]] = sp[1]
}
}
cli, err := c.root.clientOptions.NewClient(ctx)
if err != nil {
return fmt.Errorf("failed to initialize client: %w", err)
}
defer cli.Close()
req := &apiservice.ListDeploymentsRequest{
Statuses: c.statuses,
Kinds: c.appKinds,
ApplicationIds: c.appIds,
ApplicationName: c.appName,
Limit: c.limit,
Cursor: c.cursor,
Labels: labels,
}
resp, err := cli.ListDeployments(ctx, req)
if err != nil {
return fmt.Errorf("failed to list deployment: %w", err)
}
bytes, err := json.Marshal(resp)
if err != nil {
return fmt.Errorf("failed to marshal deployments: %w", err)
}
fmt.Fprintln(c.stdout, string(bytes))
return nil
}