-
Notifications
You must be signed in to change notification settings - Fork 0
Introduce CLI and logging library #7
Conversation
The default help text looks something like NAME:
go-bootstrap - a generic bootstrapping project
USAGE:
main [global options] command [command options] [arguments...]
VERSION:
unknown
COMMANDS:
example Start example command
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--debug, --verbose, -d sets the log level to debug (default: false) [$BOOTSTRAP_DEBUG]
--help, -h show help (default: false)
--version, -v print the version (default: false) Running subcommand with 2022-01-06T12:22:56.976+0100 INFO go-bootstrap Starting up go-bootstrap {"version": "unknown", "date": "2022-01-06", "commit": "-dirty-", "go_os": "linux", "go_arch": "amd64", "go_version": "go1.17.5", "uid": 1000, "gid": 1000}
2022-01-06T12:22:56.976+0100 INFO go-bootstrap.example Hello from example command! {"config": {"ExampleFlag":"bar"}} |
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.
LGTM. A bit over-engineered for my taste. I don't think most controllers and similar need a full blown CLI library, but 🤷
My 2 cents, but nothing I have too strong opinions on:
- I'm not a huge fan of using
cmd/
directory as anything else then containingmain
entry-points. I know multiple CLI libraries suggest that but it still confuses me when I see it - I'm not a fan of the global config variable.
- Pretty much all K8s projects use cobra and we'll most likely have it in our transitive dependencies anyway. I know a lot of people somehow don't like cobra, but are there good reasons not to use it?
Do you have an alternative structure suggestion?
Yeah, I'm not that much fan of singletons either, but here I compromised.
I disagree with the statement that all K8s use cobra and for me that isn't a good enough reason to use a specific lib, especially since their versions might differ anyway. I mean also a lot of K8s projects use either klog or zap so they're already not even using the same logging framework 🤷 |
I personally find the k8up example even more confusing. It's called
What I personally often do (and copied from
|
I think I can come up with something like this 👍 |
e2aad97
to
8d54ed5
Compare
with json log format it looks like this: go run ./... --log-format json -d example --flag "asdf"
{"level":"info","ts":1641483801.989945,"logger":"go-bootstrap","caller":"go-bootstrap/main.go:98","msg":"Starting up go-bootstrap","version":"unknown","date":"2022-01-06","commit":"-dirty-","go_os":"linux","go_arch":"amd64","go_version":"go1.17.5","uid":1000,"gid":1000}
{"level":"debug","ts":1641483801.989997,"logger":"go-bootstrap.example","caller":"go-bootstrap/example_command.go:31","msg":"validating config","version":"unknown"}
{"level":"info","ts":1641483801.9900086,"logger":"go-bootstrap.example","caller":"go-bootstrap/example_command.go:40","msg":"Hello from example command!","version":"unknown","config":{"ExampleFlag":"asdf"}} |
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.
LGTM! I really like how you handle configs now and this makes commands easy to test.
3d45c16
to
a5dcfe8
Compare
884a31c
to
e4e3a17
Compare
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.
Looks good overall 🚀 One question for my understanding...
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.
Looks good now 👍
The sample could include a a WaitGroup
to make it more real life. Usually you have to wait for the shutdown.
@bastjan Hopefully the last objections are now integrated :) |
Thread-safe retrieval of logging instance
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.
Looks good to me now! 🎉 Thanks for the work 😊
Summary
Checklist
bug
,enhancement
,documentation
,change
,breaking
,dependency
as they show up in the changelog