-
Notifications
You must be signed in to change notification settings - Fork 72
/
file.go
126 lines (114 loc) · 2.64 KB
/
file.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
package config
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
// NewFile creates a new config type
func NewFile() IConfig {
cfg := &File{}
return cfg
}
// File is a type which describes a config file
type File struct{}
const errorFormat = "%v: %w"
const EnvName = "RHOASCONFIG"
// Load loads the configuration from the configuration file. If the configuration file doesn't exist
// it will return an empty configuration object.
func (c *File) Load() (*Config, error) {
file, err := c.Location()
if err != nil {
return nil, err
}
_, err = os.Stat(file)
if os.IsNotExist(err) {
return nil, err
}
if err != nil {
return nil, fmt.Errorf(errorFormat, "unable to check if config file exists", err)
}
// #nosec G304
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf(errorFormat, "unable to read config file", err)
}
var cfg Config
err = json.Unmarshal(data, &cfg)
if err != nil {
return nil, fmt.Errorf(errorFormat, "unable to parse config", err)
}
return &cfg, nil
}
// Save saves the given configuration to the configuration file.
func (c *File) Save(cfg *Config) error {
file, err := c.Location()
if err != nil {
return err
}
data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return fmt.Errorf("%v: %w", "unable to marshal config", err)
}
rhoasCfgDir, err := DefaultDir()
if err != nil {
return err
}
if _, err = os.Stat(rhoasCfgDir); os.IsNotExist(err) {
err = os.Mkdir(rhoasCfgDir, 0o700)
if err != nil {
return err
}
}
err = ioutil.WriteFile(file, data, 0o600)
if err != nil {
return fmt.Errorf(errorFormat, "unable to save config", err)
}
return nil
}
// Remove removes the configuration file.
func (c *File) Remove() error {
file, err := c.Location()
if err != nil {
return err
}
_, err = os.Stat(file)
if os.IsNotExist(err) {
return nil
}
err = os.Remove(file)
if err != nil {
return err
}
return nil
}
// Location gets the path to the config file
func (c *File) Location() (path string, err error) {
if rhoasConfig := os.Getenv(EnvName); rhoasConfig != "" {
path = rhoasConfig
} else {
rhoasCfgDir, err := DefaultDir()
if err != nil {
return "", err
}
path = filepath.Join(rhoasCfgDir, "config.json")
if err != nil {
return "", err
}
}
return path, nil
}
// Checks if config has custom location
func HasCustomLocation() bool {
rhoasConfig := os.Getenv(EnvName)
return rhoasConfig != ""
}
// DefaultDir returns the default parent directory of the config file
func DefaultDir() (string, error) {
userCfgDir, err := os.UserConfigDir()
if err != nil {
return "", err
}
return filepath.Join(userCfgDir, "rhoas"), nil
}