forked from nlewo/comin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
87 lines (80 loc) · 2.33 KB
/
config.go
File metadata and controls
87 lines (80 loc) · 2.33 KB
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
package config
import (
"fmt"
"os"
"path/filepath"
"slices"
"strings"
"github.com/nlewo/comin/internal/types"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
func Read(path string) (config types.Configuration, err error) {
file, err := os.Open(path)
if err != nil {
return
}
defer file.Close() // nolint
d := yaml.NewDecoder(file)
if err := d.Decode(&config); err != nil {
return config, err
}
for i, remote := range config.Remotes {
if remote.Auth.AccessTokenPath != "" {
content, err := os.ReadFile(remote.Auth.AccessTokenPath)
if err != nil {
return config, err
}
config.Remotes[i].Auth.AccessToken = strings.TrimSpace(string(content))
}
// On GitLab and GitHub, any non blank username is working
if remote.Auth.Username == "" {
config.Remotes[i].Auth.Username = "comin"
}
if remote.Timeout == 0 {
config.Remotes[i].Timeout = 300
}
if remote.Branches.Main.Operation == "" {
config.Remotes[i].Branches.Main.Operation = "switch"
}
if remote.Branches.Testing.Operation == "" {
config.Remotes[i].Branches.Testing.Operation = "test"
}
}
if config.ApiServer.ListenAddress == "" {
config.ApiServer.ListenAddress = "127.0.0.1"
}
if config.ApiServer.Port == 0 {
config.ApiServer.Port = 4242
}
if config.Exporter.ListenAddress == "" {
config.Exporter.ListenAddress = "0.0.0.0"
}
if config.Exporter.Port == 0 {
config.Exporter.Port = 4243
}
if config.StateFilepath == "" {
config.StateFilepath = filepath.Join(config.StateDir, "state.json")
}
if config.RepositorySubdir == "" {
config.RepositorySubdir = "."
}
supportedRepositoryTypes := []string{"flake", "nix", "system-manager"}
if !slices.Contains(supportedRepositoryTypes, config.RepositoryType) {
return config, fmt.Errorf("config: repository type is '%s' while it be one of '%s'", config.RepositoryType, supportedRepositoryTypes)
}
if config.Grpc.UnixSocketPath == "" {
config.Grpc.UnixSocketPath = filepath.Join(config.StateDir, "grpc.sock")
}
logrus.Debugf("Config is '%#v'", config)
return
}
func MkGitConfig(config types.Configuration) types.GitConfig {
return types.GitConfig{
Path: filepath.Join(config.StateDir, "repository"),
Dir: config.RepositorySubdir,
Remotes: config.Remotes,
GpgPublicKeyPaths: config.GpgPublicKeyPaths,
Submodules: config.Submodules,
}
}