-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathget.go
More file actions
70 lines (56 loc) · 1.48 KB
/
get.go
File metadata and controls
70 lines (56 loc) · 1.48 KB
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
package cmd
import (
"context"
"errors"
"fmt"
pb "github.com/runconduit/conduit/controller/gen/public"
"github.com/runconduit/conduit/pkg/k8s"
"github.com/spf13/cobra"
)
func newCmdGet() *cobra.Command {
cmd := &cobra.Command{
Use: "get [flags] pods",
Short: "Display one or many mesh resources",
Long: `Display one or many mesh resources.
Only pod resources (aka pods, po) are supported.`,
Example: ` # get all pods
conduit get pods`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("please specify a resource type")
}
if len(args) > 1 {
return errors.New("please specify only one resource type")
}
friendlyName := args[0]
resourceType, err := k8s.CanonicalKubernetesNameFromFriendlyName(friendlyName)
if err != nil || resourceType != k8s.Pods {
return fmt.Errorf("invalid resource type %s, only %s are allowed as resource types", friendlyName, k8s.Pods)
}
client, err := newPublicAPIClient()
if err != nil {
return err
}
podNames, err := getPods(client)
if err != nil {
return err
}
for _, podName := range podNames {
fmt.Println(podName)
}
return nil
},
}
return cmd
}
func getPods(apiClient pb.ApiClient) ([]string, error) {
resp, err := apiClient.ListPods(context.Background(), &pb.Empty{})
if err != nil {
return nil, err
}
names := make([]string, 0)
for _, pod := range resp.GetPods() {
names = append(names, pod.Name)
}
return names, nil
}