Skip to content

Commit b22c599

Browse files
author
jld3103
authored
Merge pull request #132 from go-flutter-desktop/use-sync-for-config-loading
2 parents cf17094 + 3120f34 commit b22c599

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

internal/config/config.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"os"
55
"path/filepath"
66
"strings"
7+
"sync"
78

89
"github.com/pkg/errors"
910
"gopkg.in/yaml.v2"
@@ -26,7 +27,6 @@ const BuildOpenGlVersionDefault = "3.3"
2627

2728
// Config contains the parsed contents of hover.yaml
2829
type Config struct {
29-
loaded bool
3030
ApplicationName string `yaml:"application-name"`
3131
ExecutableName string `yaml:"executable-name"`
3232
PackageName string `yaml:"package-name"`
@@ -67,53 +67,54 @@ func (c Config) GetLicense() string {
6767
return c.License
6868
}
6969

70-
var config = Config{}
70+
var (
71+
config Config
72+
configLoadOnce sync.Once
73+
)
7174

7275
// GetConfig returns the working directory hover.yaml as a Config
7376
func GetConfig() Config {
74-
// TODO(GeertJohan): Add sync.Once
75-
if !config.loaded {
76-
c, err := ReadConfigFile(filepath.Join(build.BuildPath, "hover.yaml"))
77+
configLoadOnce.Do(func() {
78+
var err error
79+
config, err = ReadConfigFile(filepath.Join(build.BuildPath, "hover.yaml"))
7780
if err != nil {
7881
if os.IsNotExist(errors.Cause(err)) {
7982
// TODO: Add a solution for the user. Perhaps we can let `hover
8083
// init` write missing files when ran on an existing project.
8184
// https://github.com/go-flutter-desktop/hover/pull/121#pullrequestreview-408680348
8285
log.Warnf("Missing config: %v", err)
83-
return config
86+
return
8487
}
8588
log.Errorf("Failed to load config: %v", err)
8689
os.Exit(1)
8790
}
88-
config = *c
89-
config.loaded = true
9091

9192
if config.CachePathREMOVED != "" {
9293
log.Errorf("The hover.yaml field 'cache-path' is not used anymore. Remove it from your hover.yaml and use --cache-path instead.")
9394
os.Exit(1)
9495
}
95-
}
96+
})
9697
return config
9798
}
9899

99100
// ReadConfigFile reads a .yaml file at a path and return a correspond Config
100101
// struct
101-
func ReadConfigFile(configPath string) (*Config, error) {
102+
func ReadConfigFile(configPath string) (Config, error) {
102103
file, err := os.Open(configPath)
103104
if err != nil {
104105
if os.IsNotExist(err) {
105-
return nil, errors.Wrap(err, "file hover.yaml not found")
106+
return Config{}, errors.Wrap(err, "file hover.yaml not found")
106107
}
107-
return nil, errors.Wrap(err, "failed to open hover.yaml")
108+
return Config{}, errors.Wrap(err, "failed to open hover.yaml")
108109
}
109110
defer file.Close()
110111

111112
var config Config
112113
err = yaml.NewDecoder(file).Decode(&config)
113114
if err != nil {
114-
return nil, errors.Wrap(err, "failed to decode hover.yaml")
115+
return Config{}, errors.Wrap(err, "failed to decode hover.yaml")
115116
}
116-
return &config, nil
117+
return config, nil
117118
}
118119

119120
func PrintMissingField(name, file, def string) {

0 commit comments

Comments
 (0)