-
Notifications
You must be signed in to change notification settings - Fork 151
/
cmd.go
81 lines (63 loc) · 1.93 KB
/
cmd.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
package clusters
import (
"errors"
"os"
"github.com/go-resty/resty/v2"
"github.com/spf13/cobra"
"github.com/weaveworks/weave-gitops/cmd/gitops/cmderrors"
"github.com/weaveworks/weave-gitops/pkg/adapters"
"github.com/weaveworks/weave-gitops/pkg/clusters"
"k8s.io/cli-runtime/pkg/printers"
)
type clustersGetFlags struct {
Kubeconfig bool
}
var clustersGetCmdFlags clustersGetFlags
func ClusterCommand(endpoint *string, client *resty.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "cluster",
Aliases: []string{"clusters"},
Short: "Display one or many CAPI clusters",
Example: `
# Get all CAPI clusters
gitops get clusters
# Get a single CAPI cluster
gitops get cluster <cluster-name>
# Get the Kubeconfig of a cluster
gitops get cluster <cluster-name> --kubeconfig`,
SilenceUsage: true,
SilenceErrors: true,
PreRunE: getClusterCmdPreRunE(endpoint, client),
RunE: getClusterCmdRunE(endpoint, client),
}
cmd.PersistentFlags().BoolVar(&clustersGetCmdFlags.Kubeconfig, "kubeconfig", false, "Returns the Kubeconfig of the workload cluster")
return cmd
}
func getClusterCmdPreRunE(endpoint *string, client *resty.Client) func(*cobra.Command, []string) error {
return func(c *cobra.Command, s []string) error {
if *endpoint == "" {
return cmderrors.ErrNoWGEEndpoint
}
return nil
}
}
func getClusterCmdRunE(endpoint *string, client *resty.Client) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
r, err := adapters.NewHttpClient(*endpoint, client, os.Stdout)
if err != nil {
return err
}
w := printers.GetNewTabWriter(os.Stdout)
defer w.Flush()
if clustersGetCmdFlags.Kubeconfig {
if len(args) == 0 {
return errors.New("cluster name is required")
}
return clusters.GetClusterKubeconfig(args[0], r, os.Stdout)
}
if len(args) == 1 {
return clusters.GetClusterByName(args[0], r, w)
}
return clusters.GetClusters(r, w)
}
}