-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
141 lines (127 loc) · 2.89 KB
/
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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package config
import (
"log"
"os"
"path/filepath"
"reflect"
"strings"
"github.com/go-git/go-git/v5"
"gopkg.in/yaml.v3"
)
var (
Global config
Manifest manifestConfig
Version string = "develop"
)
type manifestConfig struct {
Name string `yaml:"name"`
Replications int `yaml:"replications"`
ClusterKey string `yaml:"cluster_key"`
StatsNode string `yaml:"stats_node"`
BootstrapPeers []string `yaml:"bootstrap_peers"`
Mirrors []string `yaml:"mirrors"`
}
type config struct {
General general
Database database
Ipfs ipfs
manifest manifest
Web web
}
type general struct {
Version string
}
type database struct {
Path string
}
type manifest struct {
Url string
Path string
}
type web struct {
Addr string
}
type ipfs struct {
Path string
PrivateKey string
PeerID string
Addr string
}
func init() {
var err error
home, _ := os.UserHomeDir()
Global = parseConfigEnv(
&config{
General: general{
Version: Version,
},
Database: database{
Path: "arkstat.db",
},
manifest: manifest{
Url: "https://github.com/arken/core-manifest.git",
Path: filepath.Join(home, ".config", "arkstrap", "manifest"),
},
Web: web{
Addr: ":8080",
},
Ipfs: ipfs{
Path: filepath.Join(home, ".config", "arkstrap", "ipfs"),
PeerID: "",
PrivateKey: "",
Addr: "",
},
},
)
Manifest, err = parseConfigManifest(Global.manifest.Path, Global.manifest.Url)
if err != nil {
log.Fatal(err)
}
}
func parseConfigEnv(input *config) (result config) {
numSubStructs := reflect.ValueOf(input).Elem().NumField()
for i := 0; i < numSubStructs; i++ {
iter := reflect.ValueOf(input).Elem().Field(i)
subStruct := strings.ToUpper(iter.Type().Name())
structType := iter.Type()
for j := 0; j < iter.NumField(); j++ {
fieldVal := iter.Field(j).String()
if fieldVal != "Version" {
fieldName := structType.Field(j).Name
for _, prefix := range []string{"ARKSTRAP"} {
evName := prefix + "_" + subStruct + "_" + strings.ToUpper(fieldName)
evVal, evExists := os.LookupEnv(evName)
if evExists && evVal != fieldVal {
iter.FieldByName(fieldName).SetString(evVal)
}
}
}
}
}
return *input
}
func parseConfigManifest(path, url string) (result manifestConfig, err error) {
r, err := git.PlainOpen(path)
if err != nil && err.Error() == "repository does not exist" {
r, err = git.PlainClone(path, false, &git.CloneOptions{
URL: url,
})
}
if err != nil {
return result, err
}
w, err := r.Worktree()
if err != nil {
return result, err
}
err = w.Pull(&git.PullOptions{RemoteName: "origin"})
if err != nil && err.Error() != "already up-to-date" {
return result, err
}
bytes, err := os.ReadFile(filepath.Join(Global.manifest.Path, "config.yml"))
if os.IsNotExist(err) {
return result, err
}
err = yaml.Unmarshal(bytes, &result)
return result, err
}