-
Notifications
You must be signed in to change notification settings - Fork 151
/
cmd.go
62 lines (47 loc) · 1.45 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
package clusters
import (
"os"
"github.com/go-resty/resty/v2"
"github.com/spf13/cobra"
"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`,
RunE: getClustersCmdRunE(endpoint, client),
}
cmd.PersistentFlags().BoolVar(&clustersGetCmdFlags.Kubeconfig, "kubeconfig", false, "Returns the Kubeconfig of the workload cluster")
return cmd
}
func getClustersCmdRunE(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 len(args) == 1 {
if clustersGetCmdFlags.Kubeconfig {
return clusters.GetClusterKubeconfig(args[0], r, os.Stdout)
}
return clusters.GetClusterByName(args[0], r, w)
}
return clusters.GetClusters(r, w)
}
}