diff --git a/Gemfile b/Gemfile index b9b1d69..c9c4be1 100644 --- a/Gemfile +++ b/Gemfile @@ -2,14 +2,14 @@ source "https://rubygems.org" # development dependencies group :development do - gem "rake", "~> 12.0" + gem "rake" gem "pry" gem "pry-byebug" end # test dependencies group :development, :test do - gem "rspec", "~> 3.0" + gem "rspec" gem "vcr" gem "webmock" gem 'codecov', require: false, group: 'test' diff --git a/Gemfile.lock b/Gemfile.lock index 5dd3d7b..6e9ca9a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -64,14 +64,14 @@ GEM minitest (5.14.4) multi_json (1.15.0) netrc (0.11.0) - pry (0.14.1) + pry (0.13.1) coderay (~> 1.1) method_source (~> 1.0) - pry-byebug (3.8.0) + pry-byebug (3.9.0) byebug (~> 11.0) - pry (~> 0.10) + pry (~> 0.13.0) public_suffix (4.0.6) - rake (12.3.3) + rake (13.0.3) recursive-open-struct (1.1.3) rest-client (2.1.0) http-accept (>= 1.7.0, < 2.0) @@ -124,8 +124,8 @@ DEPENDENCIES logging pry pry-byebug - rake (~> 12.0) - rspec (~> 3.0) + rake + rspec simplecov vcr webmock diff --git a/lib/kubetruth/cli.rb b/lib/kubetruth/cli.rb index f4e3220..3021833 100755 --- a/lib/kubetruth/cli.rb +++ b/lib/kubetruth/cli.rb @@ -1,12 +1,9 @@ -require 'clamp' +require_relative 'cli_base' require_relative 'project' require_relative 'etl' -require_relative 'clamp_help_formatter' module Kubetruth - class CLI < Clamp::Command - - include GemLogger::LoggerSupport + class CLI < CLIBase banner <<~EOF Scans cloudtruth parameters for `name-pattern`, using the `name` match as @@ -44,40 +41,10 @@ class CLI < Clamp::Command :flag, "Perform a dry run", default: false - option ["-q", "--quiet"], - :flag, "Suppress output", - default: false - - option ["-d", "--debug"], - :flag, "Debug output", - default: false - - option ["-c", "--[no-]color"], - :flag, "colorize output (or not) (default: $stdout.tty?)", - default: true - - option ["-v", "--version"], - :flag, "show version", - default: false - # TODO: option to map template to configmap? - # hook into clamp lifecycle to force logging setup even when we are calling - # a subcommand - def parse(arguments) - super - - level = :info - level = :debug if debug? - level = :error if quiet? - Kubetruth::Logging.setup_logging(level: level, color: color?) - end - def execute - if version? - logger.info "Kubetruth Version #{VERSION}" - exit(0) - end + super ct_context = { organization: organization, diff --git a/lib/kubetruth/cli_base.rb b/lib/kubetruth/cli_base.rb new file mode 100644 index 0000000..80db7ad --- /dev/null +++ b/lib/kubetruth/cli_base.rb @@ -0,0 +1,42 @@ +require 'clamp' +require_relative 'clamp_help_formatter' + +module Kubetruth + class CLIBase < Clamp::Command + + include GemLogger::LoggerSupport + + option ["-q", "--quiet"], + :flag, "Suppress output", + default: false + + option ["-d", "--debug"], + :flag, "Debug output", + default: false + + option ["-c", "--[no-]color"], + :flag, "colorize output (or not) (default: $stdout.tty?)", + default: true + + option ["-v", "--version"], + :flag, "show version" do + logger.info "Version #{VERSION}" + exit(0) + end + + # hook into clamp lifecycle to force logging setup even when we are calling + # a subcommand + def parse(arguments) + super + + level = :info + level = :debug if debug? + level = :error if quiet? + Kubetruth::Logging.setup_logging(level: level, color: color?) + end + + def execute + end + + end +end diff --git a/lib/kubetruth/cli_liquid_tester.rb b/lib/kubetruth/cli_liquid_tester.rb index bbb1a56..27f038e 100644 --- a/lib/kubetruth/cli_liquid_tester.rb +++ b/lib/kubetruth/cli_liquid_tester.rb @@ -1,11 +1,13 @@ -require 'clamp' +require_relative 'cli_base' require_relative 'template' -require_relative 'clamp_help_formatter' module Kubetruth - class CLILiquidTester < Clamp::Command + class CLILiquidTester < CLIBase - include GemLogger::LoggerSupport + banner <<~EOF + Allows one to experiment with liquid templates by evaluating the given + template with the supplied variable context + EOF option ["-v", "--variable"], 'VAR', "variable=value to be used in evaluating the template", @@ -17,38 +19,8 @@ class CLILiquidTester < Clamp::Command option ["-f", "--template-file"], 'FILE', "A file containing the template. use '-' for stdin" - option ["-q", "--quiet"], - :flag, "Suppress output", - default: false - - option ["-d", "--debug"], - :flag, "Debug output", - default: false - - option ["-c", "--[no-]color"], - :flag, "colorize output (or not) (default: $stdout.tty?)", - default: true - - option ["-v", "--version"], - :flag, "show version", - default: false - - # hook into clamp lifecycle to force logging setup even when we are calling - # a subcommand - def parse(arguments) - super - - level = :info - level = :debug if debug? - level = :error if quiet? - Kubetruth::Logging.setup_logging(level: level, color: color?) - end - def execute - if version? - logger.info "Kubetruth Version #{VERSION}" - exit(0) - end + super tmpl = template if template.present? if template_file.present? diff --git a/spec/kubetruth/cli_base_spec.rb b/spec/kubetruth/cli_base_spec.rb new file mode 100644 index 0000000..4c9b22c --- /dev/null +++ b/spec/kubetruth/cli_base_spec.rb @@ -0,0 +1,71 @@ +require 'rspec' +require 'kubetruth/cli_base' + +module Kubetruth + describe CLIBase do + + let(:cli) { described_class.new("") } + + describe "--help" do + + it "produces help text under standard width" do + all_usage(described_class).each do |m| + expect(m[:usage]).to be_line_width_for_cli(m[:name]) + end + end + + end + + describe "version" do + + it "uses flag to produce version text" do + expect { cli.run(['--version']) }.to raise_error(SystemExit) + expect(Logging.contents).to include(VERSION) + end + + end + + describe "--debug" do + + it "defaults to info log level" do + expect(Logging).to receive(:setup_logging).with(hash_including(level: :info)) + cli.run([]) + end + + it "sets log level to debug" do + expect(Logging).to receive(:setup_logging).with(hash_including(level: :debug)) + cli.run(['--debug']) + end + + end + + describe "--quiet" do + + it "defaults to info log level" do + expect(Logging).to receive(:setup_logging).with(hash_including(level: :info)) + cli.run([]) + end + + it "sets log level to warn" do + expect(Logging).to receive(:setup_logging).with(hash_including(level: :error)) + cli.run(['--quiet']) + end + + end + + describe "--no-color" do + + it "defaults to color" do + expect(Logging).to receive(:setup_logging).with(hash_including(color: true)) + cli.run([]) + end + + it "outputs plain text" do + expect(Logging).to receive(:setup_logging).with(hash_including(color: false)) + cli.run(['--no-color']) + end + + end + + end +end diff --git a/spec/kubetruth/cli_liquid_tester_spec.rb b/spec/kubetruth/cli_liquid_tester_spec.rb index 8703b54..959115b 100644 --- a/spec/kubetruth/cli_liquid_tester_spec.rb +++ b/spec/kubetruth/cli_liquid_tester_spec.rb @@ -16,57 +16,6 @@ module Kubetruth end - describe "version" do - - it "uses flag to produce version text" do - expect { cli.run(['--version']) }.to raise_error(SystemExit) - expect(Logging.contents).to include(VERSION) - end - - end - - describe "--debug" do - - it "defaults to info log level" do - expect(Logging).to receive(:setup_logging).with(hash_including(level: :info)) - expect { cli.run(['--version']) }.to raise_error(SystemExit) - end - - it "sets log level to debug" do - expect(Logging).to receive(:setup_logging).with(hash_including(level: :debug)) - expect { cli.run(['--debug', '--version']) }.to raise_error(SystemExit) - end - - end - - describe "--quiet" do - - it "defaults to info log level" do - expect(Logging).to receive(:setup_logging).with(hash_including(level: :info)) - expect { cli.run(['--version']) }.to raise_error(SystemExit) - end - - it "sets log level to warn" do - expect(Logging).to receive(:setup_logging).with(hash_including(level: :error)) - expect { cli.run(['--quiet', '--version']) }.to raise_error(SystemExit) - end - - end - - describe "--no-color" do - - it "defaults to color" do - expect(Logging).to receive(:setup_logging).with(hash_including(color: true)) - expect { cli.run(['--version']) }.to raise_error(SystemExit) - end - - it "outputs plain text" do - expect(Logging).to receive(:setup_logging).with(hash_including(color: false)) - expect { cli.run(['--no-color', '--version']) }.to raise_error(SystemExit) - end - - end - describe "no args" do it "fails for no templates" do diff --git a/spec/kubetruth/cli_spec.rb b/spec/kubetruth/cli_spec.rb index 72d6641..e1c3531 100644 --- a/spec/kubetruth/cli_spec.rb +++ b/spec/kubetruth/cli_spec.rb @@ -16,58 +16,6 @@ module Kubetruth end - describe "version" do - - it "uses flag to produce version text" do - expect { cli.run(['--version']) }.to raise_error(SystemExit) - expect(Logging.contents).to include(VERSION) - end - - end - - describe "--debug" do - - it "defaults to info log level" do - expect(Logging).to receive(:setup_logging).with(hash_including(level: :info)) - expect { cli.run(['--version']) }.to raise_error(SystemExit) - end - - it "sets log level to debug" do - expect(Logging).to receive(:setup_logging).with(hash_including(level: :debug)) - expect { cli.run(['--debug', '--version']) }.to raise_error(SystemExit) - end - - end - - describe "--quiet" do - - it "defaults to info log level" do - expect(Logging).to receive(:setup_logging).with(hash_including(level: :info)) - expect { cli.run(['--version']) }.to raise_error(SystemExit) - end - - it "sets log level to warn" do - expect(Logging).to receive(:setup_logging).with(hash_including(level: :error)) - expect { cli.run(['--quiet', '--version']) }.to raise_error(SystemExit) - end - - end - - describe "--no-color" do - - it "defaults to color" do - expect(Logging).to receive(:setup_logging).with(hash_including(color: true)) - expect { cli.run(['--version']) }.to raise_error(SystemExit) - end - - it "outputs plain text" do - expect(Logging).to receive(:setup_logging).with(hash_including(color: false)) - expect { cli.run(['--no-color', '--version']) }.to raise_error(SystemExit) - end - - end - - describe "execute" do it "passes args to etl" do