-
Notifications
You must be signed in to change notification settings - Fork 2
/
profile.go
90 lines (82 loc) · 2.31 KB
/
profile.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
82
83
84
85
86
87
88
89
90
package profile
import (
"encoding/json"
"fmt"
"io"
"os"
"strconv"
"github.com/appgate/sdpctl/pkg/configuration"
"github.com/appgate/sdpctl/pkg/docs"
"github.com/appgate/sdpctl/pkg/factory"
"github.com/appgate/sdpctl/pkg/profiles"
"github.com/mitchellh/mapstructure"
"github.com/spf13/cobra"
)
type commandOpts struct {
Out io.Writer
}
var tabCompletion = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
p, err := profiles.Read()
if err != nil {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}
return p.Available(), cobra.ShellCompDirectiveNoFileComp
}
// NewProfileCmd return a new profile subcommand
func NewProfileCmd(f *factory.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "profile",
Annotations: map[string]string{
configuration.SkipAuthCheck: "true",
},
TraverseChildren: true,
Short: docs.ProfileRootDoc.Short,
Long: docs.ProfileRootDoc.Long,
}
opts := &commandOpts{
Out: f.IOOutWriter,
}
cmd.AddCommand(NewListCmd(opts))
cmd.AddCommand(NewAddCmd(opts))
cmd.AddCommand(NewDeleteCmd(opts))
cmd.AddCommand(NewSetCmd(opts))
return cmd
}
// readConfig read the config file from the profile settings directory
// it tries to respect environment variable and parse boolean values correctly
//
// See: https://github.com/spf13/viper/issues/937
func readConfig(path string) (*configuration.Config, error) {
content, err := os.ReadFile(path)
if err != nil {
return nil, err
}
raw := make(map[string]interface{})
if err := json.Unmarshal(content, &raw); err != nil {
return nil, err
}
if v, ok := raw["insecure"].(string); ok {
boolValue, err := strconv.ParseBool(v)
if err != nil {
return nil, fmt.Errorf("insecure should be true|false, got %s %s", v, err)
}
delete(raw, "insecure")
raw["insecure"] = boolValue
}
if v, ok := raw["debug"].(string); ok {
boolValue, err := strconv.ParseBool(v)
if err != nil {
return nil, fmt.Errorf("debug should be true|false, got %s %s", v, err)
}
delete(raw, "debug")
raw["debug"] = boolValue
}
var config configuration.Config
if err := mapstructure.Decode(raw, &config); err != nil {
return nil, fmt.Errorf("%s file is corrupt: %s \n", path, err)
}
return &config, nil
}