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

Support reading config vars from ENV #2090

Closed
nicdoye opened this issue Apr 18, 2016 · 7 comments
Closed

Support reading config vars from ENV #2090

nicdoye opened this issue Apr 18, 2016 · 7 comments
Labels

Comments

@nicdoye
Copy link

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
Copy link
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 as completed Apr 18, 2016
@nicdoye
Copy link
Author

nicdoye commented Apr 18, 2016

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

@bep
Copy link
Member

bep commented Apr 18, 2016

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

@nicdoye
Copy link
Author

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 Command line options and/or environment variables for more configuration options Verify config from ENV Apr 18, 2016
@bep bep changed the title Verify config from ENV Support reading config vars from ENV Apr 18, 2016
@bep
Copy link
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
Copy link
Member

bep commented Apr 18, 2016

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

@bep bep closed this as completed in b7fd410 Apr 18, 2016
bep added a commit that referenced this issue Apr 18, 2016
tychoish pushed a commit to tychoish/hugo that referenced this issue Aug 13, 2017
So

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

Will change the `title`.

Fixes gohugoio#2090
tychoish pushed a commit to tychoish/hugo that referenced this issue Aug 13, 2017
@github-actions
Copy link

github-actions bot commented Apr 7, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants