This repository has been archived by the owner on Jun 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 362
/
main.go
67 lines (53 loc) · 1.51 KB
/
main.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
package config
import (
"fmt"
"reflect"
"github.com/datreeio/datree/bl/messager"
"github.com/datreeio/datree/pkg/localConfig"
"github.com/datreeio/datree/pkg/utils"
"github.com/spf13/cobra"
)
var ConfigAvailableKeys = []string{"token", "offline"}
type Messager interface {
LoadVersionMessages(cliVersion string) chan *messager.VersionMessage
}
type Printer interface {
PrintMessage(messageText string, messageColor string)
}
type LocalConfig interface {
GetLocalConfiguration() (*localConfig.LocalConfig, error)
Set(key string, value string) error
Get(key string) string
}
type ConfigCommandContext struct {
Messager Messager
CliVersion string
Printer Printer
LocalConfig LocalConfig
}
func New(ctx *ConfigCommandContext) *cobra.Command {
configCommand := &cobra.Command{
Use: "config",
Short: "Configuration management",
Long: `Internal configuration management for datree config file`,
Example: utils.Example(`
# Change the token in the datree config.yaml file
datree config set token <MY_EXAMPLE_TOKEN>
# Get the token from datree config.yaml file
datree config get token
`),
}
configCommand.AddCommand(NewSetCommand(ctx))
configCommand.AddCommand(NewGetCommand(ctx))
return configCommand
}
func validateKey(key string) error {
validKeys := make(map[string]bool)
for _, key := range ConfigAvailableKeys {
validKeys[key] = true
}
if val, ok := validKeys[key]; !ok || !val {
return fmt.Errorf("key must be one of: %s", reflect.ValueOf(validKeys).MapKeys())
}
return nil
}