Skip to content

Commit

Permalink
refactor cli
Browse files Browse the repository at this point in the history
  • Loading branch information
wr0ngway committed Jun 22, 2021
1 parent 5a8f7d0 commit a24edbb
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 182 deletions.
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
12 changes: 6 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -124,8 +124,8 @@ DEPENDENCIES
logging
pry
pry-byebug
rake (~> 12.0)
rspec (~> 3.0)
rake
rspec
simplecov
vcr
webmock
Expand Down
39 changes: 3 additions & 36 deletions lib/kubetruth/cli.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand Down
42 changes: 42 additions & 0 deletions lib/kubetruth/cli_base.rb
Original file line number Diff line number Diff line change
@@ -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
42 changes: 7 additions & 35 deletions lib/kubetruth/cli_liquid_tester.rb
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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?
Expand Down
71 changes: 71 additions & 0 deletions spec/kubetruth/cli_base_spec.rb
Original file line number Diff line number Diff line change
@@ -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
51 changes: 0 additions & 51 deletions spec/kubetruth/cli_liquid_tester_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 0 additions & 52 deletions spec/kubetruth/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a24edbb

Please sign in to comment.