Skip to content

Commit

Permalink
Add loading config file from flag in cli, and print help from cli if …
Browse files Browse the repository at this point in the history
…required
  • Loading branch information
NHAS committed Nov 10, 2024
1 parent 34d4cce commit b441b46
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ Confy offers a variety of options for configuring your application's settings.
| `Defaults(...)` | Loads configurations in the order: config file -> environment variables -> CLI flags. This sets a non-strict parsing mode for unknown fields in the config file. |
| `FromConfigFile(...)` | Load configuration from a file. Supports `YAML`, `JSON`, and `TOML`. |
| `FromConfigBytes(...)` | Load configuration from raw bytes, ideal for embedding configuration in code. |
| `FromEnvs(...)` | Load configuration from environment variables. Use the delimiter to denote nested fields. |
| `FromConfigURL(...)` | Load configuration from URL. Supports `YAML`, `JSON`, and `TOML`, use extension or content type to specify type when using auto keyword|
| `FromConfigFileFlagPath(...)` | Load configuration from file with filepath specified as cli flag |
| `WithStrictParsing(...)` | Parse config files in a strict way, do not allow unknown fields |
| `FromCli(...)` | Load configuration from CLI flags. Set a delimiter for nested struct parsing. |
| `WithLogLevel(...)` | Set logging level to control output verbosity. Useful for debugging. |
| `WithCliTransform(...)` | Takes a function to run against the generated CLI flag name, allows you to modify the flag name |
Expand Down
21 changes: 18 additions & 3 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"path/filepath"
"reflect"
"slices"
"strings"
"time"
)
Expand Down Expand Up @@ -155,6 +156,9 @@ func Config[T any](suppliedOptions ...OptionFunc) (result T, warnings []error, e
logger.Warn("parser issued warning", "parser", p, "err", err.Error())

warnings = append(warnings, err)
} else if errors.Is(err, flag.ErrHelp) && slices.Contains(o.order, cli) && p != cli {
err = orderLoadOpts[cli].apply(&result)
return result, nil, err
} else {
logger.Error("parser issued error", "parser", p, "err", err.Error())
return result, nil, err
Expand Down Expand Up @@ -356,12 +360,23 @@ func FromConfigURL(urlOpt string, configType ConfigType) OptionFunc {
// FromConfigFileFlagPath tells confy to load file from path as specified by cli flag
// cliFlagName: string cli option that defines config filepath
// configType: ConfigType, what type the config file is expected to be, use `Auto` if you dont care and just want it to choose for you. Supports yaml, toml and json
func FromConfigFileFlagPath(cliFlagName string, configType ConfigType) OptionFunc {
func FromConfigFileFlagPath(cliFlagName, defaultPath, description string, configType ConfigType) OptionFunc {
return func(c *options) error {

FromConfigFile("", configType)
commandLine := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)

return nil
configPath := commandLine.String(cliFlagName, defaultPath, description)
if err := commandLine.Parse(os.Args[1:]); err != nil {
if err == flag.ErrHelp {
commandLine.PrintDefaults()
return flag.ErrHelp
}

// We will get a lot of random "flag not defined" errors,as our flags are defined much later (if at all) in the Cli component
configPath = &defaultPath
}

return FromConfigFile(*configPath, configType)(c)
}
}

Expand Down

0 comments on commit b441b46

Please sign in to comment.