0

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?

Sergii Bishyr
  • 8,331
  • 6
  • 40
  • 69

1 Answers1

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