Show the application version
fabiodcorreia opened this issue · 14 comments
It's possible to add an option -v/--version to show the application version information?
I think it is a great idea. We could treat it like help where the program does not run.
var cfg struct {
Version string `conf:"defaut:v1.0.0"`
}
Given a field named Version
in the cfg struct, if --version
or -v
is provided, display the version. The version is set as a default value.
Other option is to add a SetVersion
function to be called before Parse
. This would allow you to pass the version in when set from go build
.
conf.SetVersion(build)
conf.Parse(os.Args[1:], "APP", &cfg)
Third option is to break the Parse API, which would mean a upgrade to version 2.
I was thinking on something like option two, having a function to set the version. Option one is not very handy and also adding breaking change because of this feature is too much.
My original idea was something like
conf.ParseWithVersion(os.Args[1:], "APP", &cfg, version)
But this can escalate very quickly if later we also want to add other stuff like Description, ...
Right, probably need something like:
type AppDetails struct {
Version string
Description string
}
conf.SetAppDetails(appDetails AppDetails)
Maybe in this case we use a struct to extend the information someone might want in the future. I don't like this API design but it will elminate breaking changes.
I would go with just the conf.SetVersion
the description can always be added with Print but -v can't.
I will try to make the conf.SetVersion and make a PR it's ok? I started to learn Go in part-time so I think it will be good for me to at least try it.
Let's go with this since people will want to add app name and copyright information.
func SetAppDetails(version string, description string)
conf.SetAppDetails("v1.0.0", "Service (Copyright 2020)\nThis app does this.")
./app -v
Version v1.0.0
Service (Copyright 2020)
This app does this.
I added a PR #10, I'm not 100% happy with it but didn't manage to make it better. Any tips are welcome :)
What if we required adding a Version to the cfg struct? You pass it in that way. This elminates the global variables and extra call.
cfg2 := struct {
Version conf.Version
Web struct {
APIHost string `conf:"default:0.0.0.0:3000"`
DebugHost string `conf:"default:0.0.0.0:4000"`
ReadTimeout time.Duration `conf:"default:5s"`
WriteTimeout time.Duration `conf:"default:5s"`
ShutdownTimeout time.Duration `conf:"default:5s"`
}
}{
Version: conf.Version {
SVN: "v1.0.0",
Description: "Service (Copyright 2020)\nThis app does this.",
},
}
This would also let you set it from configuration or other ways.
Yes! We can have the Version struct and the user can add it or not to the root config struct. I will change my implementation to work that way and update the PR
Thanks!
We should be able to embed conf.Version
and it work as well.
Through reflection, if we see the type, we can use it.
Ok makes sense, I need to dig on the reflection part since I didn't focus much on that part of the Go language. But I will :)
PR updated, It's more clean now at least I think so :)
I manage all the points except this one #10 (review) I think my solution is a little unflexible but I can't see another without breaking changes.
The PR is merged and released so we can close this.