4
4
"os"
5
5
"path/filepath"
6
6
"strings"
7
+ "sync"
7
8
8
9
"github.com/pkg/errors"
9
10
"gopkg.in/yaml.v2"
@@ -26,7 +27,6 @@ const BuildOpenGlVersionDefault = "3.3"
26
27
27
28
// Config contains the parsed contents of hover.yaml
28
29
type Config struct {
29
- loaded bool
30
30
ApplicationName string `yaml:"application-name"`
31
31
ExecutableName string `yaml:"executable-name"`
32
32
PackageName string `yaml:"package-name"`
@@ -67,53 +67,54 @@ func (c Config) GetLicense() string {
67
67
return c .License
68
68
}
69
69
70
- var config = Config {}
70
+ var (
71
+ config Config
72
+ configLoadOnce sync.Once
73
+ )
71
74
72
75
// GetConfig returns the working directory hover.yaml as a Config
73
76
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" ))
77
80
if err != nil {
78
81
if os .IsNotExist (errors .Cause (err )) {
79
82
// TODO: Add a solution for the user. Perhaps we can let `hover
80
83
// init` write missing files when ran on an existing project.
81
84
// https://github.com/go-flutter-desktop/hover/pull/121#pullrequestreview-408680348
82
85
log .Warnf ("Missing config: %v" , err )
83
- return config
86
+ return
84
87
}
85
88
log .Errorf ("Failed to load config: %v" , err )
86
89
os .Exit (1 )
87
90
}
88
- config = * c
89
- config .loaded = true
90
91
91
92
if config .CachePathREMOVED != "" {
92
93
log .Errorf ("The hover.yaml field 'cache-path' is not used anymore. Remove it from your hover.yaml and use --cache-path instead." )
93
94
os .Exit (1 )
94
95
}
95
- }
96
+ })
96
97
return config
97
98
}
98
99
99
100
// ReadConfigFile reads a .yaml file at a path and return a correspond Config
100
101
// struct
101
- func ReadConfigFile (configPath string ) (* Config , error ) {
102
+ func ReadConfigFile (configPath string ) (Config , error ) {
102
103
file , err := os .Open (configPath )
103
104
if err != nil {
104
105
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" )
106
107
}
107
- return nil , errors .Wrap (err , "failed to open hover.yaml" )
108
+ return Config {} , errors .Wrap (err , "failed to open hover.yaml" )
108
109
}
109
110
defer file .Close ()
110
111
111
112
var config Config
112
113
err = yaml .NewDecoder (file ).Decode (& config )
113
114
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" )
115
116
}
116
- return & config , nil
117
+ return config , nil
117
118
}
118
119
119
120
func PrintMissingField (name , file , def string ) {
0 commit comments