forked from viamrobotics/rdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
47 lines (39 loc) · 906 Bytes
/
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
package cli
import (
"encoding/json"
"os"
"path/filepath"
)
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
if err := json.Unmarshal(rd, &conf); err != nil {
return nil, err
}
return &conf, nil
}
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)
}
// Config contains stored config information for the CLI.
type Config struct {
Auth *Token `json:"auth"`
}