-
Notifications
You must be signed in to change notification settings - Fork 5
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
Switch to kong (aka support reading flags from env) #51
base: master
Are you sure you want to change the base?
Conversation
Again, this was just a fun excersize, i have no objections to not being merged, i just thought it was a win and worth sharing |
Kong supports env variables out of the box, and a little easier to make commands (it seems).
a5f9a0a
to
824d1e9
Compare
"github.com/gebn/unifibackup/v2/cmd/unifibackup/monitor" | ||
"github.com/gebn/unifibackup/v2/cmd/unifibackup/uploader" | ||
|
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.
This is interesting, I've always grouped imports by stdlib, project packages, then other packages. Do you have a reference for swapping other and project?
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.
oh my editor is setup to auto organize imports, i think via gopls or https://github.com/incu6us/goimports-reviser, i havn't really thought about it as long as its consistent.
cmd/unifibackup/main.go
Outdated
@@ -32,73 +32,56 @@ var ( | |||
}) | |||
) | |||
|
|||
var CLI struct { |
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.
Could this be made a local variable within main()
, or at least be unexported?
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.
yea. Their getting started docs are a little rough, so i didn't realize the name didn't matter till much later.
cmd/unifibackup/main.go
Outdated
err := genmeta(CLI.Genmeta.Dir) | ||
if err != nil { |
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.
err := genmeta(CLI.Genmeta.Dir) | |
if err != nil { | |
if err := genmeta(CLI.Genmeta.Dir); err != nil { |
cmd/unifibackup/main.go
Outdated
Args: cobra.NoArgs, | ||
RunE: func(_ *cobra.Command, _ []string) error { | ||
return genmeta(flgBackupDir) | ||
ctx := kong.Parse( |
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.
ctx
is conventionally reserved in Go for context.Context
, and there is some shadowing going on as a result. How about kongCtx
?
cmd/unifibackup/main.go
Outdated
cfg, err := config.LoadDefaultConfig(ctx, | ||
config.WithUseDualStackEndpoint(aws.DualStackEndpointStateEnabled)) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to initialise AWS SDK: %w", err)) |
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.
It's a good idea not to panic()
, perhaps it would be worth copying the pattern here and returning the error from this function instead?
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.
i mean printing the error and exiting with code 1 is the same as panic
but sure, i can switch it to the same pattern
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.
Ack, it's more about having main()
be the (single) point of exit - knowing all other functions will return in all cases simplifies the mental model, and means we can choose how to handle those errors at the caller rather than callee.
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.
yep yep, very good practice. I didn't see a setup like that with cobra so was trying to stick with what was already done.
cmd/unifibackup/main.go
Outdated
if err != nil { | ||
panic(fmt.Errorf("failed to initialize daemon: %w", err)) | ||
} | ||
break |
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.
Go switch
cases do not fall-through, so these are redundant.
cmd/unifibackup/main.go
Outdated
} | ||
break | ||
default: | ||
panic(ctx.Command()) |
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.
panic(ctx.Command()) | |
return fmt.Errorf("unknown command: %v", kongCtx.Command()) |
oh cool, this sounds like its not a no. I'll try to clean it up this afternoon. That'll save me from having some weird hacking of the arguments to make all but my bucket name public (I like having my infra as code public for others to see from) |
The use of env vars vs. flags shouldn't change that, but I get the former are easier to template and compose. Cobra does have 37.9k stars vs. Kong's 2.1k. They are both mature, stable libraries. The main thing I care about is not breaking people. |
in my kubernetes setup i can inject secrets as env variables but not as command arguments. I was gonna try out doing bash -c, but its inside of a scratch container, so no shell either. so thats what got me thinking if this was doable. |
You should be able to reference env vars in arguments e.g. args: [
"--bucket", "$(BUCKET)",
] |
What syntax is that? |
Nothing more than this. |
thanks so much for this. I didn't realize it was an option, let alone reading from envFrom, a lot less bash -c in my future :) Do you want to close this or merge it?
|
Kong supports env variables out of the box, and a little easier to make commands (it seems).
This really just started as an excersize to see if i could hook up env variables easily, and the more i read the more i saw kong was suggested over cobra + viper these days. and I can kinda see why.
I tried to keep the same "unifibackup --bucket name" support so by default if no other args are given, it'll run backup
or
The only thing I couldn't keep was --help defaulting to "backup"