-
Notifications
You must be signed in to change notification settings - Fork 2
Feat/error #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/error #46
Changes from 39 commits
dbcd970
d78f7f6
dc854bb
925c150
e412504
cf4960b
3a0b94c
77bee82
fa9638d
55ff163
6e0ee72
1d4d293
debe88d
8ba16e0
5b2abd6
2608440
8741605
f70bbef
f758f62
22f33d6
957cf56
a17c964
c1e41d3
2c40b3e
df93cb7
66fa286
94d8c7d
9a4ce8b
e7f9f75
2fd0eda
d7519f9
1f400ea
22f0d00
4389ef3
b0933a8
4eb2a65
0f4fae2
c2b6de1
059ce8c
d5c19c7
f0bf598
59c0949
51def87
a56d0ea
19f9b48
6d8d484
c21d839
c8dec7d
c3fbd7e
78cea65
7366811
149a5cb
b0220ea
030ef4d
065eb0c
195d36a
2a812f4
3f9d1e2
4a3d1fb
2933d32
ad46d48
34a2b17
1cd57fb
d731cdf
ffaae77
e03ae0e
49c9c44
27551a6
b9e9697
bc36b0c
7acbee2
e7ecadb
0db2995
10c55c0
79e6979
d1176b3
ed864db
a7579d2
7e0f979
0b8e185
fad225d
2aeede8
171de64
155d563
cbaa02f
0aba861
ed6d56d
acdceea
79ea27d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package cliutils | ||
|
|
||
| import ( | ||
| "os" | ||
| "strings" | ||
| ) | ||
|
|
||
| func IsHelp() bool { | ||
| help := strings.TrimSpace(os.Args[len(os.Args)-1]) | ||
| if strings.HasSuffix(help, "--help") || strings.HasSuffix(help, "-h") { | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "sort" | ||
|
|
||
| "github.com/moby/term" | ||
| "github.com/pubgo/funk/assert" | ||
| "github.com/pubgo/funk/cliutils" | ||
| "github.com/pubgo/funk/cmds/versioncmd" | ||
| "github.com/pubgo/funk/ctxutil" | ||
| "github.com/pubgo/funk/version" | ||
| "github.com/urfave/cli/v3" | ||
| ) | ||
|
|
||
| func main() { | ||
| app := &cli.Command{ | ||
| Name: "testmain", | ||
| Suggest: true, | ||
| UseShortOptionHandling: true, | ||
| ShellComplete: cli.DefaultAppComplete, | ||
| Version: version.Version(), | ||
| Commands: []*cli.Command{ | ||
| versioncmd.New(), | ||
| }, | ||
| Before: func(ctx context.Context, command *cli.Command) (context.Context, error) { | ||
| if !term.IsTerminal(os.Stdin.Fd()) { | ||
| return ctx, fmt.Errorf("stdin is not a terminal") | ||
| } | ||
|
|
||
| if cliutils.IsHelp() { | ||
| return ctx, cli.ShowAppHelp(command) | ||
| } | ||
| return ctx, nil | ||
| }, | ||
| } | ||
|
|
||
| sort.Sort(cli.FlagsByName(app.Flags)) | ||
| assert.Exit(app.Run(ctxutil.Signal(), os.Args)) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package versioncmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/urfave/cli/v3" | ||
|
|
||
| "github.com/pubgo/funk/pretty" | ||
| "github.com/pubgo/funk/recovery" | ||
| "github.com/pubgo/funk/running" | ||
| "github.com/pubgo/funk/version" | ||
| ) | ||
|
|
||
| func New() *cli.Command { | ||
| return &cli.Command{ | ||
| Name: "version", | ||
| Usage: fmt.Sprintf("%s version info", version.Project()), | ||
| Action: func(ctx context.Context, command *cli.Command) error { | ||
| defer recovery.Exit() | ||
| pretty.Println(running.GetSysInfo()) | ||
| return nil | ||
| }, | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package lifecycle | ||
|
|
||
| import "context" | ||
|
|
||
| func WrapNoError(fn func(context.Context)) ExecFunc { | ||
| return func(ctx context.Context) error { fn(ctx); return nil } | ||
| } | ||
|
|
||
| func WrapNoCtx(fn func() error) ExecFunc { | ||
| return func(ctx context.Context) error { return fn() } | ||
| } | ||
|
|
||
| func WrapNoCtxErr(fn func()) ExecFunc { | ||
| return func(ctx context.Context) error { fn(); return nil } | ||
| } | ||
|
|
||
| type ExecFunc = func(context.Context) error | ||
|
|
||
| type Executor struct { | ||
| Exec ExecFunc | ||
| } | ||
|
|
||
| type Handler func(lc Lifecycle) | ||
|
|
||
| type Lifecycle interface { | ||
| AfterStop(f ExecFunc) | ||
| BeforeStop(f ExecFunc) | ||
| AfterStart(f ExecFunc) | ||
| BeforeStart(f ExecFunc) | ||
| } | ||
|
|
||
| type Getter interface { | ||
| GetAfterStops() []Executor | ||
| GetBeforeStops() []Executor | ||
| GetAfterStarts() []Executor | ||
| GetBeforeStarts() []Executor | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| assets: | ||
| test_md: | ||
| test_abc: | ||
| secret: ${{embed("test.md")}} | ||
| path_dir: ${{get_path_dir()}} | ||
| secret: {{embed("test.md")}} | ||
| path_dir: {{get_path_dir()}} |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,32 @@ | ||||||||||
| package config | ||||||||||
|
|
||||||||||
| import ( | ||||||||||
| "strings" | ||||||||||
|
|
||||||||||
| "github.com/pubgo/funk/env" | ||||||||||
| "github.com/samber/lo" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| type EnvConfigMap map[string]*EnvConf | ||||||||||
|
|
||||||||||
| type EnvConf struct { | ||||||||||
| Name string `yaml:"name"` | ||||||||||
| Description string `yaml:"description"` | ||||||||||
| Default string `yaml:"default"` | ||||||||||
| Required bool `yaml:"required"` | ||||||||||
| Example string `yaml:"example"` | ||||||||||
| Versions string `yaml:"versions"` | ||||||||||
| Tags string `yaml:"tags"` | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func initEnv(envMap EnvConfigMap) { | ||||||||||
| for name, cfg := range envMap { | ||||||||||
| envData := env.Get(name) | ||||||||||
| envData = strings.TrimSpace(lo.Ternary(envData != "", envData, cfg.Default)) | ||||||||||
| if cfg.Required && envData == "" { | ||||||||||
| panic("env " + cfg.Name + " is required") | ||||||||||
|
||||||||||
| panic("env " + cfg.Name + " is required") | |
| panic(fmt.Sprintf("env %s is required", cfg.Name)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The panic message uses cfg.Name. However, the key of the envMap is name, which is the actual environment variable name being processed. If cfg.Name is not set in the YAML configuration, the panic message will be unhelpful (e.g., "env is required"). It would be more robust to use the map key name in the panic message.
| panic("env " + cfg.Name + " is required") | |
| panic("env " + name + " is required") |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -177,7 +177,7 @@ func unmarshalOneOrList[T any](list *[]T, value *yaml.Node) error { | |||||
| if value.Kind == yaml.SequenceNode { | ||||||
| return value.Decode(list) | ||||||
| } | ||||||
| return errors.Format("unmarshalled node: %v", value.Value) | ||||||
| return errors.Errorf("unmarshalled node: %v", value.Value) | ||||||
|
||||||
| return errors.Errorf("unmarshalled node: %v", value.Value) | |
| return errors.Errorf("unmarshal node: %v", value.Value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of
strings.HasSuffixcan lead to false positives. For example, an argument like--some-option-hwould be incorrectly identified as the-hhelp flag. A direct string comparison is safer.Additionally, this implementation is fragile because it only checks the last command-line argument. If a help flag is provided anywhere else in the command, it will not be detected.