Skip to content

Commit d9984cd

Browse files
authored
Merge pull request #2 from RealImage/config-refresh
Update conf if existing one is the deprecated one
2 parents ee9f91b + 79b1afc commit d9984cd

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

main.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
package main
33

44
import (
5+
"bytes"
6+
"crypto/md5"
57
"flag"
68
"io"
79
"io/ioutil"
@@ -23,6 +25,24 @@ var (
2325
driver *server.Server
2426
)
2527

28+
var deprecatedConfFileChecksum = md5.New().Sum([]byte(`{
29+
"version": 1,
30+
"accesses": [
31+
{
32+
"user": "test",
33+
"pass": "test",
34+
"fs": "os",
35+
"params": {
36+
"basePath": "/tmp"
37+
}
38+
}
39+
],
40+
"passive_transfer_port_range": {
41+
"start": 2122,
42+
"end": 2130
43+
}
44+
}`))
45+
2646
func main() {
2747
// Arguments vars
2848
var confFile string
@@ -48,9 +68,29 @@ func main() {
4868
}
4969

5070
if autoCreate {
51-
if _, err := os.Stat(confFile); err != nil && os.IsNotExist(err) {
52-
logger.Warn("No conf file, creating one", "confFile", confFile)
71+
shouldCreateOrUpdate := false
72+
_, err := os.Stat(confFile)
73+
if err != nil {
74+
if os.IsNotExist(err) {
75+
logger.Warn("No conf file, creating one", "confFile", confFile)
76+
shouldCreateOrUpdate = true
77+
}
78+
} else {
79+
file, errOpen := os.ReadFile(confFile)
80+
if errOpen != nil {
81+
logger.Error("Cannot open config file", "err", errOpen)
82+
return
83+
}
84+
85+
if bytes.Equal(md5.New().Sum(bytes.TrimSpace(file)), []byte(deprecatedConfFileChecksum)) {
86+
logger.Warn("Deprecated conf file found, writing new conf")
87+
shouldCreateOrUpdate = true
88+
} else {
89+
logger.Warn("Existing conf file is not same a deprecated one. No modifying.")
90+
}
91+
}
5392

93+
if shouldCreateOrUpdate {
5494
if err := ioutil.WriteFile(confFile, confFileContent(), 0600); err != nil { //nolint: gomnd
5595
logger.Warn("Couldn't create conf file", "confFile", confFile)
5696
}

0 commit comments

Comments
 (0)