-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(flag/stdflags): initial cli flag support. * chore(_example): updates example. * feat(diag): adds support for diagnosing flag specified config file path. * chore(stdflag): renames `flag/flag` to `flag/stdflag` to avoid polluting stdlib imports. * feat(configurator): initial cli flag support. * chore(_example): reorder imports. * refactor(configurator): moves flag processing into a function. * refactor(configurator): migrates core processing functions to methods.
- Loading branch information
1 parent
bb33806
commit 692ceef
Showing
14 changed files
with
419 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/matthewhartstonge/configurator" | ||
"github.com/matthewhartstonge/configurator/diag" | ||
"github.com/matthewhartstonge/configurator/flag/stdflag" | ||
) | ||
|
||
var _ configurator.ConfigParser = (*ExampleFlagConfig)(nil) | ||
var _ configurator.ConfigImplementer = (*ExampleFlagConfig)(nil) | ||
var _ configurator.ConfigFlagImplementer = (*ExampleFlagConfig)(nil) | ||
|
||
type ExampleFlagConfig struct { | ||
stdflag.Flag | ||
|
||
Port int | ||
BackupFrequency int | ||
} | ||
|
||
func (f *ExampleFlagConfig) Init() { | ||
flag.IntVar(&f.Port, "port", 0, "path to config file") | ||
flag.IntVar(&f.BackupFrequency, "backup-frequency", 0, "path to config file") | ||
} | ||
|
||
func (f *ExampleFlagConfig) Validate(component diag.Component) diag.Diagnostics { | ||
var diags diag.Diagnostics | ||
if f.Port < 0 || f.Port > 65535 { | ||
diags.FromComponent(component, "-port"). | ||
Error("Unable to parse port", | ||
"Port must be between 0 and 65535, but instead got "+strconv.Itoa(f.Port)) | ||
f.Port = 0 | ||
} | ||
if f.BackupFrequency < 0 { | ||
diags.FromComponent(component, "-backup-frequency"). | ||
Error("Unable to parse backup frequency", | ||
"Backup frequency should be provided in hours and be non-negative, but instead got "+strconv.Itoa(f.BackupFrequency)) | ||
f.BackupFrequency = 0 | ||
} | ||
|
||
return diags | ||
} | ||
|
||
func (f *ExampleFlagConfig) Merge(d any) any { | ||
cfg := d.(*DomainConfig) | ||
|
||
if f.Port != 0 { | ||
cfg.Port = uint16(f.Port) | ||
} | ||
if f.BackupFrequency != 0 { | ||
cfg.BackupFrequency = time.Duration(f.BackupFrequency) * time.Hour | ||
} | ||
|
||
return cfg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
myapp: | ||
version: 1.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package configurator | ||
|
||
import ( | ||
"github.com/matthewhartstonge/configurator/diag" | ||
) | ||
|
||
type ConfigFileTypeable interface { | ||
ConfigParser | ||
ConfigFileParser | ||
ConfigImplementer | ||
} | ||
|
||
type ConfigFileParser interface { | ||
// Stat returns false if a file can't be found by the parser. | ||
Stat(diags *diag.Diagnostics, component diag.Component, cfg *Config, dirPath string) bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package configurator | ||
|
||
type ConfigFlagTypeable interface { | ||
ConfigParser | ||
ConfigFlagImplementer | ||
} | ||
|
||
type ConfigFlagImplementer interface { | ||
ConfigImplementer | ||
|
||
// Init is called to configure and set up the flag environment. | ||
Init() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.