generated from cybozu-go/neco-template
/
credential.go
51 lines (44 loc) · 1.2 KB
/
credential.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
package cmd
import (
"context"
"fmt"
"github.com/spf13/cobra"
)
var credentialConfig struct {
user string
format string
}
// credentialCmd represents the credential command
var credentialCmd = &cobra.Command{
Use: "credential CLUSTER_NAME",
Short: "Fetch the credential of a specified user",
Long: "Fetch the credential of a specified user.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return fetchCredential(cmd.Context(), args[0])
},
}
func fetchCredential(ctx context.Context, clusterName string) error {
password, err := getPassword(ctx, clusterName, credentialConfig.user)
if err != nil {
return err
}
switch credentialConfig.format {
case "plain":
fmt.Println(password)
case "mycnf":
fmt.Printf(`[client]
user=%s
password="%s"
`, credentialConfig.user, password)
default:
return fmt.Errorf("unknown format: %s", credentialConfig.format)
}
return nil
}
func init() {
fs := credentialCmd.Flags()
fs.StringVarP(&credentialConfig.user, "mysql-user", "u", "moco-readonly", "User for login to mysql")
fs.StringVar(&credentialConfig.format, "format", "plain", "The format of output [`plain` or `mycnf`]")
rootCmd.AddCommand(credentialCmd)
}