/
user.go
73 lines (60 loc) · 1.9 KB
/
user.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
package config
import (
"fmt"
"io/ioutil"
"github.com/bitrise-io/go-utils/fileutil"
"github.com/bitrise-io/go-utils/pathutil"
"gopkg.in/yaml.v2"
)
const (
// UserConfigFilePath ...
UserConfigFilePath = "./.gows.user.yml"
// SyncModeSymlink ...
SyncModeSymlink = "symlink"
// SyncModeCopy ...
SyncModeCopy = "copy"
// DefaultSyncMode ...
DefaultSyncMode = SyncModeSymlink
)
// UserConfigFileAbsPath ...
func UserConfigFileAbsPath() (string, error) {
return pathutil.AbsPath(UserConfigFilePath)
}
// UserConfigModel - stored in ./.gows.user.yml
type UserConfigModel struct {
SyncMode string `json:"sync_mode" yaml:"sync_mode"`
}
// CreateDefaultUserConfig ...
func CreateDefaultUserConfig() UserConfigModel {
return UserConfigModel{
SyncMode: DefaultSyncMode,
}
}
// LoadUserConfigFromFile ...
func LoadUserConfigFromFile() (UserConfigModel, error) {
UserConfigFileAbsPath, err := UserConfigFileAbsPath()
if err != nil {
return UserConfigModel{}, fmt.Errorf("Failed to get absolute path of project config: %s", err)
}
bytes, err := ioutil.ReadFile(UserConfigFileAbsPath)
if err != nil {
return UserConfigModel{}, fmt.Errorf("Failed to read project config file (%s), error: %s", UserConfigFileAbsPath, err)
}
var UserConfig UserConfigModel
if err := yaml.Unmarshal(bytes, &UserConfig); err != nil {
return UserConfigModel{}, fmt.Errorf("Failed to parse project config (should be valid YML, path: %s), error: %s", UserConfigFileAbsPath, err)
}
return UserConfig, nil
}
// SaveUserConfigToFile ...
func SaveUserConfigToFile(projectConf UserConfigModel) error {
bytes, err := yaml.Marshal(projectConf)
if err != nil {
return fmt.Errorf("Failed to parse Project Config (should be valid YML): %s", err)
}
err = fileutil.WriteBytesToFile(UserConfigFilePath, bytes)
if err != nil {
return fmt.Errorf("Failed to write Project Config into file (%s), error: %s", UserConfigFilePath, err)
}
return nil
}