-
Notifications
You must be signed in to change notification settings - Fork 153
/
cmd.go
55 lines (45 loc) · 1.41 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
package credentials
import (
"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/templates"
"k8s.io/cli-runtime/pkg/printers"
)
func CredentialCommand(endpoint, username, password *string, client *resty.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "credential",
Aliases: []string{"credentials"},
Short: "Get CAPI credentials",
Example: `
# Get all CAPI credentials
gitops get credentials
`,
SilenceUsage: true,
SilenceErrors: true,
PreRunE: getCredentialCmdPreRunE(endpoint, client),
RunE: getCredentialCmdRunE(endpoint, username, password, client),
}
return cmd
}
func getCredentialCmdPreRunE(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 getCredentialCmdRunE(endpoint, username, password *string, client *resty.Client) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
r, err := adapters.NewHttpClient(*endpoint, *username, *password, client, os.Stdout)
if err != nil {
return err
}
w := printers.GetNewTabWriter(os.Stdout)
defer w.Flush()
return templates.GetCredentials(r, w)
}
}