Skip to content

Commit 8cb7d24

Browse files
Merge pull request #53 from alexthemayers/development
support overriding loaded config for CI
2 parents 44c4d53 + 807c0ba commit 8cb7d24

File tree

1 file changed

+41
-24
lines changed

1 file changed

+41
-24
lines changed

config/config.go

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ var (
1717
VersionFlagSet bool
1818
// ConfigFilePath is `.` if not set through the `-configFile` flag
1919
ConfigFilePath string
20-
2120
// ProcWorkingDir is set through calling os.Getwd()
2221
ProcWorkingDir string
2322
ExePath string
23+
24+
// for package use only
25+
outputPathFlag string
26+
templateNameFlag string
2427
)
2528

2629
// Data represents the config data used in the day-to-day running of can
@@ -42,14 +45,12 @@ type Data struct {
4245

4346
// Template's fields are dependant on there being a defined templated name in the CLI arguments or config file.
4447
type Template struct {
45-
Name string
46-
47-
absDirectory string
48-
48+
Name string
4949
ModuleName string
50-
5150
// BasePackageName represents the
5251
BasePackageName string
52+
53+
absDirectory string
5354
}
5455

5556
func (d *Data) Load() (err error) {
@@ -71,7 +72,9 @@ func (d *Data) Load() (err error) {
7172
flag.BoolVar(&Dryrun, "dryrun", false,
7273
"Toggles whether to perform a render without writing to disk."+
7374
"This works particularly well in combination with -debug")
74-
flag.StringVar(&d.Template.Name, "template", "", "Specify which template set to use")
75+
flag.StringVar(&templateNameFlag, "template", "", "Specify which template set to use")
76+
// TODO should we support stdout as an option here?
77+
flag.StringVar(&outputPathFlag, "outputPath", "", "Specify where to write output to")
7578
flag.Parse()
7679
}
7780

@@ -90,24 +93,9 @@ func (d *Data) Load() (err error) {
9093
if Debug {
9194
fmt.Printf("[v%s]::Using config file \"%s\".\n", SemVer, ConfigFilePath)
9295
}
93-
viper.SetConfigFile(ConfigFilePath)
94-
95-
err = viper.ReadInConfig()
96-
if err != nil {
97-
return fmt.Errorf("loadConfig:: could not read config file: %w\n", err)
98-
}
99-
100-
// Handle Template name
101-
if d.Template.Name == "" {
102-
fmt.Printf("template not set via command line. Expecting field to be set in config file\n")
103-
}
104-
105-
err = viper.Unmarshal(&d)
96+
err = d.setOverridesAndLoadConfig()
10697
if err != nil {
107-
return fmt.Errorf("loadConfig:: could not parse config file: %w\n", err)
108-
}
109-
if d.Template.Name == "" {
110-
fmt.Printf("template not set via config file either\nexiting...")
98+
return err
11199
}
112100

113101
err = d.resolveTemplateConfig()
@@ -270,3 +258,32 @@ func (d *Data) resolveTemplateConfig() error {
270258
}
271259
return nil
272260
}
261+
262+
func (d *Data) setOverridesAndLoadConfig() error {
263+
viper.SetConfigFile(ConfigFilePath)
264+
err := viper.ReadInConfig()
265+
if err != nil {
266+
return fmt.Errorf("setOverridesAndLoadConfig:: could not read config file: %w\n", err)
267+
}
268+
// Handle Template name
269+
err = viper.Unmarshal(&d)
270+
if err != nil {
271+
return fmt.Errorf("setOverridesAndLoadConfig:: could not unmarshal config file: %w\n", err)
272+
}
273+
// Handle empty config fields
274+
if d.Template.Name == "" {
275+
fmt.Printf("template not set via config file. Expecting field to be set via command line\n")
276+
if templateNameFlag == "" {
277+
return fmt.Errorf("template not set via cli flags either\nexiting...")
278+
}
279+
d.Template.Name = templateNameFlag
280+
}
281+
if d.OutputPath == "" {
282+
fmt.Printf("outputPath not set via config file. Expecting field to be set via command line\n")
283+
if outputPathFlag == "" {
284+
return fmt.Errorf("outputPath not set via cli flags either\nexiting...")
285+
}
286+
d.OutputPath = outputPathFlag
287+
}
288+
return nil
289+
}

0 commit comments

Comments
 (0)