-
Notifications
You must be signed in to change notification settings - Fork 143
/
list.go
110 lines (94 loc) · 2.98 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
// 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 application
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"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
appName string
appKind string
pipedID string
disabled bool
cursor string
labels []string
limit int32
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 applications.",
RunE: cli.WithContext(c.run),
}
cmd.Flags().StringVar(&c.appName, "app-name", c.appName, "The application name.")
cmd.Flags().StringVar(&c.appKind, "app-kind", c.appKind, fmt.Sprintf("The kind of application. (%s)", strings.Join(model.ApplicationKindStrings(), "|")))
cmd.Flags().StringVar(&c.pipedID, "piped-id", c.pipedID, "The piped id.")
cmd.Flags().BoolVar(&c.disabled, "disabled", c.disabled, "True to show only disabled applications.")
cmd.Flags().StringVar(&c.cursor, "cursor", c.cursor, "The cursor which returned by the previous request applications list.")
cmd.Flags().Int32Var(&c.limit, "limit", 10, "Upper limit on the number of return values. Default value is 10.")
cmd.Flags().StringSliceVar(&c.labels, "label", c.labels, "The application label. Expect input in the form KEY:VALUE.")
return cmd
}
func (c *list) run(ctx context.Context, _ cli.Input) error {
if c.appKind != "" {
if _, ok := model.ApplicationKind_value[c.appKind]; !ok {
return fmt.Errorf("invalid application 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.ListApplicationsRequest{
Name: c.appName,
Kind: c.appKind,
PipedId: c.pipedID,
Disabled: c.disabled,
Cursor: c.cursor,
Limit: c.limit,
Labels: labels,
}
resp, err := cli.ListApplications(ctx, req)
if err != nil {
return fmt.Errorf("failed to list application: %w", err)
}
bytes, err := json.Marshal(resp)
if err != nil {
return fmt.Errorf("failed to marshal applications: %w", err)
}
fmt.Fprintln(c.stdout, string(bytes))
return nil
}