Skip to content

Commit

Permalink
Merge pull request #393 from thomasfedb/dotenv-templates
Browse files Browse the repository at this point in the history
Updated #377 for merge
  • Loading branch information
Andrew Nordman authored Jun 22, 2019
2 parents 4c5c83b + d44bf6f commit 858ecfe
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,29 @@ If you use this gem to handle env vars for multiple Rails environments (developm

Credentials should only be accessible on the machines that need access to them. Never commit sensitive information to a repository that is not needed by every development machine and server.


You can use the `-t` or `--template` flag on the dotenv cli to create a template of your `.env` file.
```shell
$ dotenv -t .env
```
A template will be created in your working directory named `{FINAME}.template`. So in the above example, it would create a `.env.template` file.

The template will contain all the environment variables in your `.env` file but with their values set to the variable names.

```shell
# .env
S3_BUCKET=YOURS3BUCKET
SECRET_KEY=YOURSECRETKEYGOESHERE
```

Would become

```shell
# .env.template
S3_BUCKET=S3_BUCKET
SECRET_KEY=SECRET_KEY
```

Personally, I prefer to commit the `.env` file with development-only settings. This makes it easy for other developers to get started on the project without compromising credentials for other environments. If you follow this advice, make sure that all the credentials for your development environment are different from your other deployments and that the development credentials do not have access to any confidential data.

### Why is it not overriding existing `ENV` variables?
Expand Down
9 changes: 9 additions & 0 deletions lib/dotenv/cli.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "dotenv"
require "dotenv/version"
require "dotenv/template"
require "optparse"

module Dotenv
Expand Down Expand Up @@ -39,6 +40,7 @@ def add_options(parser)
add_files_option(parser)
add_help_option(parser)
add_version_option(parser)
add_template_option(parser)
end

def add_files_option(parser)
Expand All @@ -61,6 +63,13 @@ def add_version_option(parser)
end
end

def add_template_option(parser)
parser.on("-t", "--template=FILE", "Create a template env file") do |file|
template = Dotenv::EnvTemplate.new(file)
template.create_template
end
end

def create_option_parser
OptionParser.new do |parser|
parser.banner = "Usage: dotenv [options]"
Expand Down
19 changes: 19 additions & 0 deletions lib/dotenv/template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Dotenv
# Class for creating a template from a env file
class EnvTemplate
def initialize(env_file)
@env_file = env_file
end

def create_template
File.open(@env_file, "r") do |env_file|
File.open("#{@env_file}.template", "w") do |env_template|
env_file.each do |line|
variable = line.split("=").first
env_template.puts "#{variable}=#{variable}"
end
end
end
end
end
end
18 changes: 18 additions & 0 deletions spec/dotenv/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ def run(*args)
expect(cli.argv).to eql(["foo something"])
end

it "templates a file specified by -t" do
@buffer = StringIO.new
@input = StringIO.new("FOO=BAR\nFOO2=BAR2")
@origin_filename = "plain.env"
@template_filename = "plain.env.template"
@content = "the content fo the file"
allow(File).to receive(:open).with(@origin_filename, "r").and_yield(@input)
# rubocop:disable LineLength
allow(File).to receive(:open).with(@template_filename, "w").and_yield(@buffer)

# call the function that writes to the file
cli = Dotenv::CLI.new(["-t", "plain.env"])
cli.send(:parse_argv!, cli.argv)

# reading the buffer and checking its content.
expect(@buffer.string).to eq("FOO=FOO\nFOO2=FOO2\n")
end

# Capture output to $stdout and $stderr
def capture_output(&_block)
original_stderr = $stderr
Expand Down

0 comments on commit 858ecfe

Please sign in to comment.