/
config.go
178 lines (143 loc) · 4.23 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package stoclient
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/function61/eventkit/httpcommandclient"
"github.com/function61/gokit/ezhttp"
"github.com/function61/gokit/fileexists"
"github.com/function61/gokit/jsonfile"
"github.com/function61/gokit/osutil"
"github.com/function61/varasto/pkg/stoserver/stoservertypes"
"github.com/spf13/cobra"
)
type Client struct {
conf ClientConfig
}
func New(conf ClientConfig) *Client {
return &Client{conf}
}
func (c *Client) Config() ClientConfig {
return c.conf
}
type ClientConfig struct {
ServerAddr string `json:"server_addr"` // example: "https://localhost"
AuthToken string `json:"auth_token"`
FuseMountPath string `json:"fuse_mount_path"`
TlsInsecureSkipValidation bool `json:"tls_insecure_skip_validation"`
}
// TODO: this should be temporary
func (c *ClientConfig) Client() *Client {
return New(*c)
}
func (c *ClientConfig) CommandClient() *httpcommandclient.Client {
return httpcommandclient.New(c.ServerAddr+"/command/", c.AuthToken, c.HttpClient())
}
func (c *ClientConfig) UrlBuilder() *stoservertypes.RestClientUrlBuilder {
return stoservertypes.NewRestClientUrlBuilder(c.ServerAddr)
}
func (c *ClientConfig) HttpClient() *http.Client {
client := http.DefaultClient
if c.TlsInsecureSkipValidation {
client = ezhttp.InsecureTlsClient
}
return client
}
func WriteConfig(conf *ClientConfig) error {
confPath, err := ConfigFilePath()
if err != nil {
return err
}
return WriteConfigWithPath(conf, confPath)
}
// used by bootstrap
func WriteConfigWithPath(conf *ClientConfig, confPath string) error {
return jsonfile.Write(confPath, conf)
}
func ReadConfig() (*ClientConfig, error) {
confPath, err := ConfigFilePath()
if err != nil {
return nil, fmt.Errorf("Varasto client config: %v", err)
}
conf := &ClientConfig{}
if err := jsonfile.Read(confPath, conf, true); err != nil {
return nil, fmt.Errorf("Varasto client config: %v", err)
}
if strings.HasSuffix(conf.ServerAddr, "/") {
return nil, fmt.Errorf(
"Varasto client config: server_addr must not end in '/'; got %s",
conf.ServerAddr)
}
return conf, nil
}
// returns ~/.config/varasto/varastoclient-config.json
func ConfigFilePath() (string, error) {
userConfigDir, err := os.UserConfigDir()
if err != nil {
return "", fmt.Errorf("ConfigFilePath: %w", err)
}
return filepath.Join(userConfigDir, "varasto", "client-config.json"), nil
}
func configEntrypoint() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Client configuration file management",
}
cmd.AddCommand(&cobra.Command{
Use: "init [serverAddr] [authToken] [fuseMountPath]",
Short: "Initialize configuration (helps writing JSON file)",
Long: ` serverAddr looks like https://localhost
authToken looks like dTPM59uxWm_uloW4...
fuseMountPath looks like /mnt/varasto/stofuse OR leave it as empty ("")`,
Args: cobra.ExactArgs(3),
Run: func(cmd *cobra.Command, args []string) {
osutil.ExitIfError(func(serverAddr string, authToken string, fuseMountPath string) error {
confPath, err := ConfigFilePath()
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(confPath), 0700); err != nil {
return err
}
exists, err := fileexists.Exists(confPath)
if err != nil {
return err
}
if exists {
return fmt.Errorf("config file '%s' already exists", confPath)
}
conf := &ClientConfig{
ServerAddr: serverAddr,
AuthToken: authToken,
FuseMountPath: fuseMountPath,
}
return WriteConfig(conf)
}(args[0], args[1], args[2]))
},
})
cmd.AddCommand(&cobra.Command{
Use: "print",
Short: "Prints path to config file & its contents",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
confPath, err := ConfigFilePath()
osutil.ExitIfError(err)
fmt.Printf("file: %s\n", confPath)
exists, err := fileexists.Exists(confPath)
osutil.ExitIfError(err)
if !exists {
fmt.Printf(".. does not exist. To configure, run:\n $ %s config init\n", os.Args[0])
return
}
file, err := os.Open(confPath)
osutil.ExitIfError(err)
defer file.Close()
_, err = io.Copy(os.Stdout, file)
osutil.ExitIfError(err)
},
})
return cmd
}