Skip to content

Commit 92b764b

Browse files
committed
[config] Use sync.Once instead of boolean
1 parent 4bc4863 commit 92b764b

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

ginclient/config/config.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"strconv"
99
"strings"
10+
"sync"
1011

1112
"github.com/G-Node/gin-cli/ginclient/log"
1213
"github.com/G-Node/gin-cli/gincmd/ginerrors"
@@ -51,7 +52,9 @@ var (
5152

5253
// configuration cache: used to avoid rereading during a single command invocation
5354
configuration GinCliCfg
54-
set = false
55+
56+
// configReadOnce to read config only once per run
57+
configReadOnce sync.Once
5558
)
5659

5760
// Types
@@ -114,9 +117,11 @@ type GinCliCfg struct {
114117
// Read loads in the configuration from the config file(s), merges any defined values into the default configuration, and returns a populated GinConfiguration struct.
115118
// The configuration is cached. Subsequent reads reuse the already loaded configuration.
116119
func Read() GinCliCfg {
117-
if set {
118-
return configuration
119-
}
120+
configReadOnce.Do(read)
121+
return configuration
122+
}
123+
124+
func read() {
120125
viper.Reset()
121126
viper.SetTypeByDefaultValue(true)
122127

@@ -155,9 +160,6 @@ func Read() GinCliCfg {
155160
path, _ := filepath.Split(configuration.Bin.GitAnnex)
156161
configuration.Bin.GitAnnexPath = path
157162
}
158-
159-
set = true
160-
return configuration
161163
}
162164

163165
func removeInvalidServerConfs() {
@@ -200,7 +202,7 @@ func SetConfig(key string, value interface{}) error {
200202
v.Set(key, value)
201203
v.WriteConfig()
202204
// invalidate the read cache
203-
set = false
205+
configReadOnce = sync.Once{}
204206
return nil
205207
}
206208

@@ -223,7 +225,8 @@ func RmServerConf(alias string) {
223225
delete(c.Servers, alias)
224226
v.Set("servers", c.Servers)
225227
v.WriteConfig()
226-
set = false
228+
// invalidate the read cache
229+
configReadOnce = sync.Once{}
227230
}
228231

229232
// SetDefaultServer writes the given name to the config file to server as the default server for web calls.

0 commit comments

Comments
 (0)