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

Support reading config vars from ENV #2090

Closed
nicdoye opened this Issue Apr 18, 2016 · 6 comments

Comments

Projects
None yet
2 participants
@nicdoye

nicdoye commented Apr 18, 2016

In addition to --baseurl, I'd like to be able to pass the site title. Other people may have use cases for other options that are normally set in config.toml etc.

Is this a worthy enhancement?

Command line arguments appear to be set in [initHugoBuildCommonFlags()] in commands/hugo.go but I can't see immediately where they're merged with the config contents.

An alternative would be to use environment variables, but I don't think Hugo uses them at all.

Note: In Jekyll, I currently do this by abusing ENV['JEKYLL_ENV'] and using {{jekyll.environment}} (there may be a "correct" way, but I don't know it).

@bep

This comment has been minimized.

Show comment
Hide comment
@bep

bep Apr 18, 2016

Member

An alternative would be to use environment variables, but I don't think Hugo uses them at all.

I'm pretty sure you can set config vars from environment vars. See https://github.com/spf13/viper ... if it doesn't work, Viper is the correct repo to report issues about it.

We will not create flags for all the config.

Member

bep commented Apr 18, 2016

An alternative would be to use environment variables, but I don't think Hugo uses them at all.

I'm pretty sure you can set config vars from environment vars. See https://github.com/spf13/viper ... if it doesn't work, Viper is the correct repo to report issues about it.

We will not create flags for all the config.

@bep bep closed this Apr 18, 2016

@nicdoye

This comment has been minimized.

Show comment
Hide comment
@nicdoye

nicdoye Apr 18, 2016

Thank you. Apologies for not having looked at viper to work it out.

nicdoye commented Apr 18, 2016

Thank you. Apologies for not having looked at viper to work it out.

@bep

This comment has been minimized.

Show comment
Hide comment
@bep

bep Apr 18, 2016

Member

No problem - and don't hesitate to revisit this issue if you don't get it working.

Member

bep commented Apr 18, 2016

No problem - and don't hesitate to revisit this issue if you don't get it working.

@nicdoye

This comment has been minimized.

Show comment
Hide comment
@nicdoye

nicdoye Apr 18, 2016

I don't think environment variables are currently set up in hugo. Testing $BASEURL (as an example, because it's already accepted by --baseurl) doesn't work.

Adding a quick hack of

snagglepuss:~ nic$ diff -bu GitHub/hugo/commands/hugo.go Go/src/github.com/spf13/hugo/commands/hugo.go
--- GitHub/hugo/commands/hugo.go    2016-04-18 15:36:28.000000000 +0100
+++ Go/src/github.com/spf13/hugo/commands/hugo.go   2016-04-18 15:27:24.000000000 +0100
@@ -334,6 +334,8 @@
    }

    viper.RegisterAlias("indexes", "taxonomies")
+        viper.SetEnvPrefix("hugo")
+        viper.AutomaticEnv()

    loadDefaultSettings()

Allows me to run

HUGO_TITLE='Example Title' HUGO_BASEURL='http://example.com' /Users/nic/Go/src/github.com/spf13/hugo/hugo

Obviously:

  • the SetEnvPrefix() wasn't necessary;
  • the two lines may not be in the right place;
  • AutomaticEnv() may not be exactly the right thing should you wish to enable environment variables;
  • you may still not want to enable environment variables.

nicdoye commented Apr 18, 2016

I don't think environment variables are currently set up in hugo. Testing $BASEURL (as an example, because it's already accepted by --baseurl) doesn't work.

Adding a quick hack of

snagglepuss:~ nic$ diff -bu GitHub/hugo/commands/hugo.go Go/src/github.com/spf13/hugo/commands/hugo.go
--- GitHub/hugo/commands/hugo.go    2016-04-18 15:36:28.000000000 +0100
+++ Go/src/github.com/spf13/hugo/commands/hugo.go   2016-04-18 15:27:24.000000000 +0100
@@ -334,6 +334,8 @@
    }

    viper.RegisterAlias("indexes", "taxonomies")
+        viper.SetEnvPrefix("hugo")
+        viper.AutomaticEnv()

    loadDefaultSettings()

Allows me to run

HUGO_TITLE='Example Title' HUGO_BASEURL='http://example.com' /Users/nic/Go/src/github.com/spf13/hugo/hugo

Obviously:

  • the SetEnvPrefix() wasn't necessary;
  • the two lines may not be in the right place;
  • AutomaticEnv() may not be exactly the right thing should you wish to enable environment variables;
  • you may still not want to enable environment variables.

@bep bep reopened this Apr 18, 2016

@bep bep changed the title from Command line options and/or environment variables for more configuration options to Verify config from ENV Apr 18, 2016

@bep bep changed the title from Verify config from ENV to Support reading config vars from ENV Apr 18, 2016

@bep

This comment has been minimized.

Show comment
Hide comment
@bep

bep Apr 18, 2016

Member

OK, I calling AutomaticEnv() works ... but Hugo has a lot of very generic variable names, so without any namespace this will get confusing. Will have to think about this. @spf13 might chime in with an idea.

Member

bep commented Apr 18, 2016

OK, I calling AutomaticEnv() works ... but Hugo has a lot of very generic variable names, so without any namespace this will get confusing. Will have to think about this. @spf13 might chime in with an idea.

@bep

This comment has been minimized.

Show comment
Hide comment
@bep

bep Apr 18, 2016

Member

Never mind, I misunderstood the prefix feature. That solves it. Will fix.

Member

bep commented Apr 18, 2016

Never mind, I misunderstood the prefix feature. That solves it. Will fix.

@bep bep closed this in b7fd410 Apr 18, 2016

bep added a commit that referenced this issue Apr 18, 2016

tychoish added a commit to tychoish/hugo that referenced this issue Aug 13, 2017

commands: Support config from ENV
So

```bash
env HUGO_TITLE="Some Title" hugo server
```

Will change the `title`.

Fixes #2090

tychoish added a commit to tychoish/hugo that referenced this issue Aug 13, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment