-
Notifications
You must be signed in to change notification settings - Fork 110
/
config.go
78 lines (66 loc) · 1.71 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
package cli
import (
"encoding/json"
"os"
"path/filepath"
"github.com/pkg/errors"
"go.uber.org/multierr"
)
var viamDotDir = filepath.Join(os.Getenv("HOME"), ".viam")
func getCLICachePath() string {
return filepath.Join(viamDotDir, "cached_cli_config.json")
}
func configFromCache() (*config, error) {
rd, err := os.ReadFile(getCLICachePath())
if err != nil {
return nil, err
}
var conf config
tokenErr := conf.tryUnmarshallWithToken(rd)
if tokenErr == nil {
return &conf, nil
}
apiKeyErr := conf.tryUnmarshallWithAPIKey(rd)
if apiKeyErr == nil {
return &conf, nil
}
return nil, errors.Wrap(multierr.Combine(tokenErr, apiKeyErr), "failed to read config from cache")
}
func removeConfigFromCache() error {
return os.Remove(getCLICachePath())
}
func storeConfigToCache(cfg *config) error {
if err := os.MkdirAll(viamDotDir, 0o700); err != nil {
return err
}
md, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
}
//nolint:gosec
return os.WriteFile(getCLICachePath(), md, 0o640)
}
type config struct {
BaseURL string `json:"base_url"`
Auth authMethod `json:"auth"`
}
func (conf *config) tryUnmarshallWithToken(configBytes []byte) error {
conf.Auth = &token{}
if err := json.Unmarshal(configBytes, &conf); err != nil {
return err
}
if conf.Auth != nil && conf.Auth.(*token).User.Email != "" {
return nil
}
return errors.New("config did not contain a user token")
}
func (conf *config) tryUnmarshallWithAPIKey(configBytes []byte) error {
conf.Auth = &apiKey{}
if err := json.Unmarshal(configBytes, &conf); err != nil {
return err
}
if conf.Auth != nil && conf.Auth.(*apiKey).KeyID != "" {
return nil
}
return errors.New("config did not contain an api key")
}