Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hedzr/cmdr-loaders
loaders of the external sources, for cmdr v2
https://github.com/hedzr/cmdr-loaders
configuration-files env hcl2 hjson json nestedtext toml yaml
Last synced: about 1 month ago
JSON representation
loaders of the external sources, for cmdr v2
- Host: GitHub
- URL: https://github.com/hedzr/cmdr-loaders
- Owner: hedzr
- License: apache-2.0
- Created: 2024-04-21T06:53:51.000Z (8 months ago)
- Default Branch: master
- Last Pushed: 2024-11-11T06:15:18.000Z (about 1 month ago)
- Last Synced: 2024-11-11T06:31:55.667Z (about 1 month ago)
- Topics: configuration-files, env, hcl2, hjson, json, nestedtext, toml, yaml
- Language: Go
- Homepage:
- Size: 64.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# Loaders for cmdr/v2
![Go](https://github.com/hedzr/cmdr-loaders/workflows/Go/badge.svg)
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/hedzr/cmdr-loaders)
[![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/hedzr/cmdr-loaders.svg?label=release)](https://github.com/hedzr/cmdr-loaders/releases)Local configuration file loaders for various file formats, such as YAML, TOML, HCL, and much more.
This is an addon library especially for [cmdr/v2](https://github.com/hedzr/cmdr).
The typical app is [cmdr-test/examples/large](https://github.com/hedzr/cmdr-tests/blob/master/examples/large).
![image-20241111141228632](https://cdn.jsdelivr.net/gh/hzimg/blog-pics@master/upgit/2024/11/20241111_1731305562.png)
A tiny app using `cmdr/v2` and `cmdr-loaders` is:
```go
package main// Simplest tiny app
import (
"context"
"io"
"os""gopkg.in/hedzr/errors.v3"
"github.com/hedzr/cmdr/v2"
"github.com/hedzr/cmdr/v2/cli"
"github.com/hedzr/cmdr/v2/pkg/dir"
logz "github.com/hedzr/logg/slog"
"github.com/hedzr/store"
)func main() {
ctx := context.Background() // with cancel can be passed thru in your actions
app := prepareApp(
cmdr.WithStore(store.New()), // use an option store explicitly, or a dummy store by default// cmdr.WithExternalLoaders(
// local.NewConfigFileLoader(), // import "github.com/hedzr/cmdr-loaders/local" to get in advanced external loading features
// local.NewEnvVarLoader(),
// ),cmdr.WithTasksBeforeRun(func(ctx context.Context, cmd cli.Cmd, runner cli.Runner, extras ...any) (err error) {
logz.DebugContext(ctx, "command running...", "cmd", cmd, "runner", runner, "extras", extras)
return
}),// true for debug in developing time, it'll disable onAction on each Cmd.
// for productive mode, comment this line.
// The envvars FORCE_DEFAULT_ACTION & FORCE_RUN can override this.
// cmdr.WithForceDefaultAction(true),// cmdr.WithAutoEnvBindings(true),
)
if err := app.Run(ctx); err != nil {
logz.ErrorContext(ctx, "Application Error:", "err", err) // stacktrace if in debug mode/build
os.Exit(app.SuggestRetCode())
}
}func prepareApp(opts ...cli.Opt) (app cli.App) {
app = cmdr.New(opts...).
Info("tiny-app", "0.3.1").
Author("The Example Authors") // .Description(``).Header(``).Footer(``)// another way to disable `cmdr.WithForceDefaultAction(true)` is using
// env-var FORCE_RUN=1 (builtin already).
app.Flg("no-default").
Description("disable force default action").
// Group(cli.UnsortedGroup).
OnMatched(func(f *cli.Flag, position int, hitState *cli.MatchState) (err error) {
if b, ok := hitState.Value.(bool); ok {
// disable/enable the final state about 'force default action'
f.Set().Set("app.force-default-action", b)
}
return
}).
Build()app.Cmd("jump").
Description("jump command").
Examples(`jump example`). // {{.AppName}}, {{.AppVersion}}, {{.DadCommands}}, {{.Commands}}, ...
Deprecated(`v1.1.0`).
// Group(cli.UnsortedGroup).
Hidden(false).
// Both With(cb) and Build() to end a building sequence
With(func(b cli.CommandBuilder) {
b.Cmd("to").
Description("to command").
Examples(``).
Deprecated(`v0.1.1`).
OnAction(func(ctx context.Context, cmd cli.Cmd, args []string) (err error) {
// cmd.Set() == cmdr.Store(), cmd.Store() == cmdr.Store()
cmd.Set().Set("app.demo.working", dir.GetCurrentDir())
println()
println(cmd.Set().WithPrefix("app.demo").MustString("working"))cs := cmdr.Store().WithPrefix("jump.to")
if cs.MustBool("full") {
println()
println(cmd.Set().Dump())
}
cs2 := cmd.Store()
if cs2.MustBool("full") != cs.MustBool("full") {
logz.Panic("a bug found")
}
app.SetSuggestRetCode(1) // ret code must be in 0-255
return // handling command action here
}).
With(func(b cli.CommandBuilder) {
b.Flg("full", "f").
Default(false).
Description("full command").
Build()
})
})app.Flg("dry-run", "n").
Default(false).
Description("run all but without committing").
Build()app.Flg("wet-run", "w").
Default(false).
Description("run all but with committing").
Build() // no matter even if you're adding the duplicated one.app.Cmd("wrong").
Description("a wrong command to return error for testing").
// cmdline `FORCE_RUN=1 go run ./tiny wrong -d 8s` to verify this command to see the returned application error.
OnAction(func(ctx context.Context, cmd cli.Cmd, args []string) (err error) {
dur := cmd.Store().MustDuration("duration")
println("the duration is:", dur.String())ec := errors.New()
defer ec.Defer(&err) // store the collected errors in native err and return it
ec.Attach(io.ErrClosedPipe, errors.New("something's wrong"), os.ErrPermission)
// see the application error by running `go run ./tiny/tiny/main.go wrong`.
return
}).
With(func(b cli.CommandBuilder) {
b.Flg("duration", "d").
Default("5s").
Description("a duration var").
Build()
})
return
}
```See also:
- [cmdr/v2](https://github.com/hedzr/cmdr)
- [demos for cmdr/v2 - cmdr-tests](https://github.com/hedzr/cmdr-tests)## History
CHANGELOGs
- security patches
## License
Apache 2.0