Skip to content

Commit

Permalink
Simplify CLI setup
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeepers committed Jul 26, 2022
1 parent d989981 commit 961c535
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 79 deletions.
98 changes: 34 additions & 64 deletions lib/dotenv/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,83 +13,53 @@ def initialize(argv = [])
@argv = argv.dup
@filenames = []
@overload = false
end

def run
parse_argv!(@argv)

begin
load_dotenv(@overload, @filenames)
rescue Errno::ENOENT => e
abort e.message
else
exec(*@argv) unless @argv.empty?
end
end

private

def parse_argv!(argv)
parser = create_option_parser
add_options(parser)
parser.order!(argv)
@parser = OptionParser.new do |parser|
parser.banner = "Usage: dotenv [options]"
parser.separator ""

@filenames
end
parser.on("-f FILES", Array, "List of env files to parse") do |list|
@filenames = list
end

def load_dotenv(overload, filenames)
if overload
Dotenv.overload!(*filenames)
else
Dotenv.load!(*filenames)
end
end
parser.on("-o", "--overload", "override existing ENV variables") do
@overload = true
end

def add_options(parser)
add_files_option(parser)
add_overload_option(parser)
add_help_option(parser)
add_version_option(parser)
add_template_option(parser)
end
parser.on("-h", "--help", "Display help") do
puts parser
exit
end

def add_files_option(parser)
parser.on("-f FILES", Array, "List of env files to parse") do |list|
@filenames = list
end
end
parser.on("-v", "--version", "Show version") do
puts "dotenv #{Dotenv::VERSION}"
exit
end

def add_overload_option(parser)
parser.on("-o", "--overload", "override existing ENV variables") do
@overload = true
parser.on("-t", "--template=FILE", "Create a template env file") do |file|
template = Dotenv::EnvTemplate.new(file)
template.create_template
end
end
end

def add_help_option(parser)
parser.on("-h", "--help", "Display help") do
puts parser
exit
end
@parser.order!(@argv)
end

def add_version_option(parser)
parser.on("-v", "--version", "Show version") do
puts "dotenv #{Dotenv::VERSION}"
exit
end
def run
load_dotenv(@overload, @filenames)
rescue Errno::ENOENT => e
abort e.message
else
exec(*@argv) unless @argv.empty?
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
private

def create_option_parser
OptionParser.new do |parser|
parser.banner = "Usage: dotenv [options]"
parser.separator ""
def load_dotenv(overload, filenames)
if overload
Dotenv.overload!(*filenames)
else
Dotenv.load!(*filenames)
end
end
end
Expand Down
21 changes: 6 additions & 15 deletions spec/dotenv/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,20 @@ def run(*args)

it "does not consume non-dotenv flags by accident" do
cli = Dotenv::CLI.new(["-f", "plain.env", "foo", "--switch"])
cli.send(:parse_argv!, cli.argv)

expect(cli.filenames).to eql(["plain.env"])
expect(cli.argv).to eql(["foo", "--switch"])
end

it "does not consume dotenv flags from subcommand" do
cli = Dotenv::CLI.new(["foo", "-f", "something"])
cli.send(:parse_argv!, cli.argv)

expect(cli.filenames).to eql([])
expect(cli.argv).to eql(["foo", "-f", "something"])
end

it "does not mess with quoted args" do
cli = Dotenv::CLI.new(["foo something"])
cli.send(:parse_argv!, cli.argv)

expect(cli.filenames).to eql([])
expect(cli.argv).to eql(["foo something"])
Expand All @@ -73,8 +70,7 @@ def run(*args)
allow(File).to receive(:open).with(@origin_filename, "r").and_yield(@input)
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", @origin_filename])
cli.send(:parse_argv!, cli.argv)
Dotenv::CLI.new(["-t", @origin_filename])
# reading the buffer and checking its content.
expect(@buffer.string).to eq("FOO=FOO\nFOO2=FOO2\n")
end
Expand All @@ -83,44 +79,39 @@ def run(*args)
@input = StringIO.new("export FOO=BAR\nexport FOO2=BAR2")
allow(File).to receive(:open).with(@origin_filename, "r").and_yield(@input)
allow(File).to receive(:open).with(@template_filename, "w").and_yield(@buffer)
cli = Dotenv::CLI.new(["-t", @origin_filename])
cli.send(:parse_argv!, cli.argv)
Dotenv::CLI.new(["-t", @origin_filename])
expect(@buffer.string).to eq("export FOO=FOO\nexport FOO2=FOO2\n")
end

it "ignores blank lines" do
@input = StringIO.new("\nFOO=BAR\nFOO2=BAR2")
allow(File).to receive(:open).with(@origin_filename, "r").and_yield(@input)
allow(File).to receive(:open).with(@template_filename, "w").and_yield(@buffer)
cli = Dotenv::CLI.new(["-t", @origin_filename])
cli.send(:parse_argv!, cli.argv)
Dotenv::CLI.new(["-t", @origin_filename])
expect(@buffer.string).to eq("\nFOO=FOO\nFOO2=FOO2\n")
end

it "ignores comments" do
@comment_input = StringIO.new("#Heading comment\nFOO=BAR\nFOO2=BAR2\n")
allow(File).to receive(:open).with(@origin_filename, "r").and_yield(@comment_input)
allow(File).to receive(:open).with(@template_filename, "w").and_yield(@buffer)
cli = Dotenv::CLI.new(["-t", @origin_filename])
cli.send(:parse_argv!, cli.argv)
Dotenv::CLI.new(["-t", @origin_filename])
expect(@buffer.string).to eq("#Heading comment\nFOO=FOO\nFOO2=FOO2\n")
end

it "ignores comments with =" do
@comment_with_equal_input = StringIO.new("#Heading=comment\nFOO=BAR\nFOO2=BAR2")
allow(File).to receive(:open).with(@origin_filename, "r").and_yield(@comment_with_equal_input)
allow(File).to receive(:open).with(@template_filename, "w").and_yield(@buffer)
cli = Dotenv::CLI.new(["-t", @origin_filename])
cli.send(:parse_argv!, cli.argv)
Dotenv::CLI.new(["-t", @origin_filename])
expect(@buffer.string).to eq("#Heading=comment\nFOO=FOO\nFOO2=FOO2\n")
end

it "ignores comments with leading spaces" do
@comment_leading_spaces_input = StringIO.new(" #Heading comment\nFOO=BAR\nFOO2=BAR2")
allow(File).to receive(:open).with(@origin_filename, "r").and_yield(@comment_leading_spaces_input)
allow(File).to receive(:open).with(@template_filename, "w").and_yield(@buffer)
cli = Dotenv::CLI.new(["-t", @origin_filename])
cli.send(:parse_argv!, cli.argv)
Dotenv::CLI.new(["-t", @origin_filename])
expect(@buffer.string).to eq(" #Heading comment\nFOO=FOO\nFOO2=FOO2\n")
end
end
Expand Down

0 comments on commit 961c535

Please sign in to comment.