-
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: support reading CLI options from a file
* Configuration is performed inside a YAML file. * If the config file doesn't exist, it will be ignored. * Command-line arguments take priority over options set in the file.
- Loading branch information
Showing
8 changed files
with
147 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,4 @@ go.work | |
/.envrc | ||
/bin | ||
/certs | ||
/result | ||
/result |
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,28 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/urfave/cli/v2" | ||
"github.com/urfave/cli/v2/altsrc" | ||
) | ||
|
||
func loadConfigIfExists(flags []cli.Flag) cli.BeforeFunc { | ||
def := altsrc.NewMapInputSource("", make(map[interface{}]interface{})) | ||
return altsrc.InitInputSourceWithContext( | ||
flags, | ||
func(ctx *cli.Context) (altsrc.InputSourceContext, error) { | ||
path := ctx.Path("config") | ||
stat, err := os.Stat(path) | ||
if err != nil { | ||
return def, nil | ||
} | ||
if stat.IsDir() { | ||
return def, fmt.Errorf("provided config %q is a directory", | ||
path) | ||
} | ||
return altsrc.NewYamlSourceFromFile(path) | ||
}, | ||
) | ||
} |
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
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