forked from siderolabs/talos
/
config.go
143 lines (136 loc) · 3.17 KB
/
config.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package cmd
import (
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"github.com/autonomy/dianemo/src/initramfs/cmd/osctl/pkg/config"
"github.com/spf13/cobra"
)
// configCmd represents the config command.
var configCmd = &cobra.Command{
Use: "config",
Short: "Manage the client configuration",
Long: ``,
}
// configTargetCmd represents the config target command.
var configTargetCmd = &cobra.Command{
Use: "target <target>",
Short: "Set the target for the current context",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
if err := cmd.Usage(); err != nil {
os.Exit(1)
}
os.Exit(1)
}
target := args[0]
c, err := config.Open()
if err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
if c.Context == "" {
fmt.Println("no context is set")
os.Exit(1)
}
c.Contexts[c.Context].Target = target
if err := c.Save(); err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
},
}
// configContextCmd represents the configc context command.
var configContextCmd = &cobra.Command{
Use: "context <context>",
Short: "Set the current context",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
if err := cmd.Usage(); err != nil {
os.Exit(1)
}
os.Exit(1)
}
context := args[0]
c, err := config.Open()
if err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
c.Context = context
if err := c.Save(); err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
},
}
// configAddCmd represents the config add command.
var configAddCmd = &cobra.Command{
Use: "add <context>",
Short: "Add a new context",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
if err := cmd.Usage(); err != nil {
os.Exit(1)
}
os.Exit(1)
}
context := args[0]
c, err := config.Open()
if err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
caBytes, err := ioutil.ReadFile(ca)
if err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
crtBytes, err := ioutil.ReadFile(crt)
if err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
keyBytes, err := ioutil.ReadFile(key)
if err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
newContext := &config.Context{
CA: base64.StdEncoding.EncodeToString(caBytes),
Crt: base64.StdEncoding.EncodeToString(crtBytes),
Key: base64.StdEncoding.EncodeToString(keyBytes),
}
if c.Contexts == nil {
c.Contexts = map[string]*config.Context{}
}
c.Contexts[context] = newContext
if err := c.Save(); err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
},
}
func init() {
configAddCmd.Flags().StringVar(&ca, "ca", "", "the path to the CA certificate")
if err := configAddCmd.MarkFlagRequired("ca"); err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
configAddCmd.Flags().StringVar(&crt, "crt", "", "the path to the certificate")
if err := configAddCmd.MarkFlagRequired("crt"); err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
configAddCmd.Flags().StringVar(&key, "key", "", "the path to the key")
if err := configAddCmd.MarkFlagRequired("key"); err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
configCmd.AddCommand(configContextCmd, configTargetCmd, configAddCmd)
rootCmd.AddCommand(configCmd)
}