Skip to content

Commit

Permalink
Merge pull request #456 from bkeepers/cli-update
Browse files Browse the repository at this point in the history
Refactor: Simplify CLI setup
  • Loading branch information
bkeepers authored Jul 26, 2022
2 parents d989981 + 029df7a commit d93a95b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 75 deletions.
81 changes: 21 additions & 60 deletions lib/dotenv/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,93 +4,54 @@
require "optparse"

module Dotenv
# The CLI is a class responsible of handling all the command line interface
# logic.
class CLI
# The command line interface
class CLI < OptionParser
attr_reader :argv, :filenames, :overload

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)

@filenames
end
super "Usage: dotenv [options]"
separator ""

def load_dotenv(overload, filenames)
if overload
Dotenv.overload!(*filenames)
else
Dotenv.load!(*filenames)
end
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

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

def add_overload_option(parser)
parser.on("-o", "--overload", "override existing ENV variables") do
on("-o", "--overload", "override existing ENV variables") do
@overload = true
end
end

def add_help_option(parser)
parser.on("-h", "--help", "Display help") do
puts parser
on("-h", "--help", "Display help") do
puts self
exit
end
end

def add_version_option(parser)
parser.on("-v", "--version", "Show version") do
on("-v", "--version", "Show version") do
puts "dotenv #{Dotenv::VERSION}"
exit
end
end

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

order!(@argv)
end

def create_option_parser
OptionParser.new do |parser|
parser.banner = "Usage: dotenv [options]"
parser.separator ""
def run
if @overload
Dotenv.overload!(*@filenames)
else
Dotenv.load!(*@filenames)
end
rescue Errno::ENOENT => e
abort e.message
else
exec(*@argv) unless @argv.empty?
end
end
end
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 d93a95b

Please sign in to comment.