/
configure_get.go
58 lines (48 loc) · 1.29 KB
/
configure_get.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
package cmd
import (
"fmt"
"reflect"
"strings"
"github.com/Songmu/prompter"
"github.com/spf13/cobra"
)
var configureGetCmd = &cobra.Command{
Use: "get",
Short: "Return ca-cli profile",
Long: `Use the command to get specified name ca-cli profile with local config file.`,
RunE: execConfigureGet,
}
func init() {
configureCmd.AddCommand(configureGetCmd)
}
func execConfigureGet(cmd *cobra.Command, args []string) error {
// get profile data
paramProfileName := rootPprofileName
currentProfile, err := getProfile(paramProfileName)
if err != nil {
return err
}
// get struct field names
rt := reflect.TypeOf(*currentProfile)
structNames := make([]string, 0, rt.NumField()+1)
for i := 0; i < rt.NumField(); i++ {
field := rt.Field(i)
structNames = append(structNames, field.Name)
}
input := (&prompter.Prompter{
Choices: structNames,
Default: structNames[0],
Message: "Please enter what you want to display",
IgnoreCase: true,
}).Prompt()
var drawText string
switch strings.ToLower(input) {
case "apikey":
drawText = fmt.Sprintf("APIKey: %s", currentProfile.APIKey)
case "endpoint":
drawText = fmt.Sprintf("Endpoint: %s", currentProfile.Endpoint)
}
fmt.Printf("\nCurrent \"%s\" profile value: \n", paramProfileName)
fmt.Println(drawText)
return nil
}