Skip to content
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

Configuring startup parameters from environment. #71

Conversation

GaiusMartius
Copy link

Defaults are included in cmd/commento/.env.development

.gitignore Outdated
@@ -2,3 +2,10 @@ vendor/
cmd/commento/commento
*.db
.idea
.env

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use .env* here instead of manually specifying each suffix, will allow ignoring any other .env's used

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DarkThrone mentioned having a default environment that developers can use out of the box so there is one environment file that is not gitignored and that is .env.development.
Because I allow one of the 8 or 9 environments to be checked in, I had to specify the others individually.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can still use the .env*, then exclude the single one to allow with ! e.g. !.env.notignored

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, made the change.

@@ -15,23 +16,23 @@ func main() {
if err != nil {
Die(err)
}


// Load configuration from the environment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The env file can be specified explicitly e.g. via a env variable or a flag, and default to .env .
e.g.
A global $COMMENTOENV=development.local will provide specificity, where the env file will be ".env."+os.Getenv("COMMENTOENV"), and preventing best-guess config selection using this loop.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The godotenv comes from the Ruby world where they have the 'convention over configuration' outlook. The order of searching through the environments is specified by the ruby package but not built into godotenv yet. I'm going to contact the developer to nudge him to add the functionality.

}

if demoEnv := os.Getenv("DEMO"); demoEnv == "true" {
if demoEnv := os.Getenv("COMMENTO_DEMO"); demoEnv == "true" {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about if os.Getenv("COMMENTO_DEMO") == "true" {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not bad. What if the user enters 'truee' or 'fals' or has some other writing error? We should probably check that a COMMENTO_DEMO actually parses to a boolean:

if s, err := strconv.ParseBool(os.Getenv("COMMENTO_DEMO")); err != nil {
		Logger.Infof("COMMENTO_DEMO not set properly, defaulting to no demo")
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case we're literally looking for the string "true" and not a bool, as its returned by os.Getenv. Basically =="true" would be the best way to check for it, hence ignore anything else

@@ -6,6 +6,7 @@ import (
"time"

_ "github.com/mattn/go-sqlite3"
"github.com/joho/godotenv"

. "github.com/adtac/commento/lib"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this isn't part of this commit, but dot imports are not nice. From golang.org: We do not recommend the use of import . outside of tests, and using it may cause a program to fail to compile in future releases.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually like those dot imports when they are used sparingly but, I recognize they are a discouraged practice. Almost all (probably all) dot imports will be removed eventually.
I include the dot imports to avoid changing too much of the code at once.

Without the dot import there would be, for example, references to
lib.LoadDatabase instead of LoadDatabase. But if the package changes then all the libs have to change to that new package name which is annoying.
When the structure of the code is more settled the dots will get removed. Really, they are just there now for ease of readability (that's why I like them even if I know they have to go...)

@adtac
Copy link
Owner

adtac commented May 9, 2017

@GaiusMartius it looks like the commit author information is different here? Do you still wanna go ahead with that email ID or do you want to amend that?

@GaiusMartius GaiusMartius force-pushed the GaiusMartius/LoadingConfigFromEnvironment branch from 4ec57a5 to 4b3408c Compare May 9, 2017 03:10


// Load configuration from the environment.
// Values in earlier files will take precedence over later values
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean "values in later files will take precedence" right? Because a setting in .env.development.local will be overwritten if the same setting has a different value in .env, which mean .env takes precedence over .env.development.local

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Earlier files do take precedence. If you have a .env.development.local, it is assumed that you want those values to be primary because you're developing.
Deployments would just contain only the .env they intend to use, like .env.production

However, if you do not set a value then later files will set the value. The first one to set wins.

@GaiusMartius GaiusMartius force-pushed the GaiusMartius/LoadingConfigFromEnvironment branch from 4b3408c to 153ffe0 Compare May 9, 2017 03:24
@adtac
Copy link
Owner

adtac commented May 10, 2017

@GaiusMartius no merge commits please: I prefer to have a linear master commit history.

Also you can squash c325149 and 153ffe0 into one commit.

@GaiusMartius GaiusMartius force-pushed the GaiusMartius/LoadingConfigFromEnvironment branch 3 times, most recently from 46a9b46 to 113d1d2 Compare May 11, 2017 21:24
Copy link
Owner

@adtac adtac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just the one tiny review comment.

Oh and rebase please :) Thanks!

@@ -0,0 +1,2 @@
COMMENTO_PORT=8080
COMMENTO_DEMO=false
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline at EOF

@adtac adtac added wip and removed pending review labels May 19, 2017
@GaiusMartius GaiusMartius force-pushed the GaiusMartius/LoadingConfigFromEnvironment branch from 113d1d2 to 0889181 Compare May 20, 2017 05:32
@adtac adtac added pending review and removed wip labels May 20, 2017
@GaiusMartius GaiusMartius force-pushed the GaiusMartius/LoadingConfigFromEnvironment branch from fde9e95 to dbdb0a2 Compare May 20, 2017 05:45
@adtac
Copy link
Owner

adtac commented May 25, 2017

Cherry picked in #88

@adtac
Copy link
Owner

adtac commented May 25, 2017

Merged through #88

@adtac adtac closed this May 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants