-
Notifications
You must be signed in to change notification settings - Fork 5
/
config_file_store.go
79 lines (71 loc) · 1.77 KB
/
config_file_store.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
package config
import (
"errors"
"fmt"
"os"
"path/filepath"
)
// ConfigFileStore reads and writes the configuration file
//
// The config store is looking for the config file in
// "$HOME/.uipath/config" path.
type ConfigFileStore struct {
data []byte
filePath string
}
const configFilePermissions = 0600
const configDirectoryPermissions = 0700
func (s ConfigFileStore) Write(data []byte) error {
filename, err := s.configurationFilePath()
if err != nil {
return err
}
err = os.MkdirAll(filepath.Dir(filename), configDirectoryPermissions)
if err != nil {
return fmt.Errorf("Error creating configuration folder: %w", err)
}
err = os.WriteFile(filename, data, configFilePermissions)
if err != nil {
return fmt.Errorf("Error updating configuration file: %w", err)
}
return nil
}
func (s ConfigFileStore) Read() ([]byte, error) {
if s.data != nil {
return s.data, nil
}
filename, err := s.configurationFilePath()
if err != nil {
return nil, err
}
data, err := os.ReadFile(filename)
if err != nil && errors.Is(err, os.ErrNotExist) {
return []byte{}, nil
}
if err != nil {
return nil, fmt.Errorf("Error reading configuration file '%s': %w", filename, err)
}
return data, nil
}
func (s ConfigFileStore) configurationFilePath() (string, error) {
if s.filePath != "" {
return s.filePath, nil
}
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("Error reading configuration file: %w", err)
}
filename := filepath.Join(homeDir, ".uipath", "config")
return filename, nil
}
func NewConfigFileStore(filePath string) *ConfigFileStore {
return &ConfigFileStore{
filePath: filePath,
}
}
func NewConfigFileStoreWithData(filePath string, data []byte) *ConfigFileStore {
return &ConfigFileStore{
filePath: filePath,
data: data,
}
}