I have a CLI Go application. This application is reading some environment variables, and reading a custom config file.
And I don't want this env-variable to be set manually or the config file to be created manually. It would be great if these actions would be made by go install
command.
However, I haven't found a way to execute some additional actions during go install
.
Is it possible and what are the best practices to do so?
Asked
Active
Viewed 66 times
0

Sergii Bishyr
- 8,331
- 6
- 40
- 69
-
Do you want to compile these settings into the application? – Eugene Lisitsky Oct 04 '17 at 07:35
-
1@EugeneLisitsky what do you mean by compiling the settings into the application? The application is going to use it, of course. – Sergii Bishyr Oct 04 '17 at 07:36
-
Put them in source file and read from it. What task do you want to solve? That the purpose: fix settings? Hide them? – Eugene Lisitsky Oct 04 '17 at 07:39
-
@EugeneLisitsky I need some simple configs that can be changed by the users without changing the source code. Something like `.gitconfig` or so. – Sergii Bishyr Oct 04 '17 at 07:43
-
1No, `go install` cannot do this. – Volker Oct 04 '17 at 08:02
1 Answers
3
go install
can't set environment variables and can't create config files for you.
The best practice is to have sensible defaults "built" into the app. The app should provide some options to override the defaults, e.g. command line flags, environment variables and config files.
If overrides (config files) do not exist, the app may create default config files on startup, and the app may even notify the user with a warning message that configuration did not exist, so the default settings will be used, and the app just created one with the defaults which the user can change / modify.

icza
- 389,944
- 63
- 907
- 827
-
So, taking `git` as an example does it mean that `.gitconfig` file won't exist until the first execution of the `git` command? And if user what to change the configs before running `git` command he has to create that file manually? – Sergii Bishyr Oct 04 '17 at 08:13
-
2@SergiiBishyr Something like that. It's up to your use case. If config file does not exist, your app should create a default one, and may even terminate without doing anything, notifying the user to review the config the app just created. – icza Oct 04 '17 at 08:15
-
Thanks a lot! It's a little disappointing that `go install` can't do that as I tought it's a common use-case. – Sergii Bishyr Oct 04 '17 at 08:24