0

I have an API key which I've saved to /home/user/api/keys that I would like to save as a global variable in my Rails app. I thought this would work, this is my config/initializers/my_constants.rb:

`source "/home/user/api/keys"`
API_PASS = ENV["API_PASSWORD"]

And this is /home/user/api/keys (without the real value, obviously):

#!/bin/bash
export API_PASSWORD="--------"

The source command doesn't seem to do anything. API_PASS is just set to nil. How can I do this?

Isaac
  • 2,246
  • 5
  • 21
  • 34
  • Why not just set the environment variable in your shell? – Dave Newton May 22 '15 at 23:03
  • @DaveNewton - I'd like to automate it. So I think I will make a script that calls the source command and then deploys the server. But I think I will still have to open the file and parse it out when I deal with my cron tasks. – Isaac May 22 '15 at 23:17

1 Answers1

3

The above won't work because the backtick command starts its own shell and ends it so those variables are never included in the rest of your script.

I think you'll have to parse the file yourself and add the entries to ENV. I would look at the dotenv gem and see if you can point it at your own environment file. They've done all the hard work for you.

https://github.com/bkeepers/dotenv

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Philip Hallstrom
  • 19,673
  • 2
  • 42
  • 46
  • I think I'm going to create a script that uses the source command and then deploys for this then. I think I'll have to parse out the file when I'm dealing with this password in a cron task tho. I might just open the file and parse contents with regex myself. Thank you for explaining why the back ticks were failing! – Isaac May 22 '15 at 23:18