Class: Goodcheck::CLI
- Inherits:
-
Object
- Object
- Goodcheck::CLI
- Includes:
- ExitStatus
- Defined in:
- lib/goodcheck/cli.rb
Constant Summary collapse
- COMMANDS =
{ init: "Generate a sample configuration file", check: "Run check with a configuration", test: "Test your configuration", pattern: "Print regexp for rules", version: "Print version", help: "Show help and quit" }.freeze
Constants included from ExitStatus
ExitStatus::EXIT_ERROR, ExitStatus::EXIT_MATCH, ExitStatus::EXIT_SUCCESS, ExitStatus::EXIT_TEST_FAILED
Instance Attribute Summary collapse
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Instance Method Summary collapse
- #check(args) ⇒ Object
- #common_options(opts) ⇒ Object
- #config_option(opts) ⇒ Object
- #debug_option(opts) ⇒ Object
- #force_download_option(opts, &block) ⇒ Object
- #help(args) ⇒ Object
- #home_path ⇒ Object
- #init(args) ⇒ Object
-
#initialize(stdout:, stderr:) ⇒ CLI
constructor
A new instance of CLI.
- #pattern(args) ⇒ Object
- #run(args) ⇒ Object
- #test(args) ⇒ Object
- #verbose_option(opts) ⇒ Object
- #version(_args = nil) ⇒ Object
Constructor Details
#initialize(stdout:, stderr:) ⇒ CLI
Returns a new instance of CLI.
10 11 12 13 |
# File 'lib/goodcheck/cli.rb', line 10 def initialize(stdout:, stderr:) @stdout = stdout @stderr = stderr end |
Instance Attribute Details
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
8 9 10 |
# File 'lib/goodcheck/cli.rb', line 8 def stderr @stderr end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
7 8 9 |
# File 'lib/goodcheck/cli.rb', line 7 def stdout @stdout end |
Instance Method Details
#check(args) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/goodcheck/cli.rb', line 58 def check(args) config_path = Pathname(DEFAULT_CONFIG_FILE) targets = [] rules = [] formats = [:text, :json] format = :text loglevel = nil force_download = false OptionParser.new("Usage: goodcheck check [options] paths...") do |opts| config_option(opts) { |config| config_path = config } verbose_option(opts) { |level| loglevel = level } debug_option(opts) { |level| loglevel = level } force_download_option(opts) { force_download = true } (opts) opts.on("-R RULE", "--rule=RULE", "Only rule(s) to check") do |rule| rules << rule end opts.on("--format=<#{formats.join('|')}>", formats, "Output format [default: '#{format}']") do |f| format = f end end.parse!(args) Goodcheck.logger.level = loglevel if loglevel if args.empty? targets << Pathname(".") else args.each {|arg| targets << Pathname(arg) } end reporter = case format when :text Reporters::Text.new(stdout: stdout) when :json Reporters::JSON.new(stdout: stdout, stderr: stderr) else raise ArgumentError, format.inspect end Goodcheck.logger.info "Configuration = #{config_path}" Goodcheck.logger.info "Rules = [#{rules.join(", ")}]" Goodcheck.logger.info "Format = #{format}" Goodcheck.logger.info "Targets = [#{targets.join(", ")}]" Goodcheck.logger.info "Force download = #{force_download}" Goodcheck.logger.info "Home path = #{home_path}" Commands::Check.new(reporter: reporter, config_path: config_path, rules: rules, targets: targets, stderr: stderr, force_download: force_download, home_path: home_path).run end |
#common_options(opts) ⇒ Object
193 194 195 196 197 198 199 200 201 202 |
# File 'lib/goodcheck/cli.rb', line 193 def (opts) opts.on_tail("--version", COMMANDS.fetch(:version)) do version exit EXIT_SUCCESS end opts.on_tail("-h", "--help", COMMANDS.fetch(:help)) do stdout.puts opts.help exit EXIT_SUCCESS end end |
#config_option(opts) ⇒ Object
175 176 177 178 179 |
# File 'lib/goodcheck/cli.rb', line 175 def config_option(opts) opts.on("-c CONFIG", "--config=CONFIG", "Configuration file path [default: '#{DEFAULT_CONFIG_FILE}']") do |config| yield Pathname(config) end end |
#debug_option(opts) ⇒ Object
185 186 187 |
# File 'lib/goodcheck/cli.rb', line 185 def debug_option(opts) opts.on("-d", "--debug", "Set log level to debug") { yield ::Logger::DEBUG } end |
#force_download_option(opts, &block) ⇒ Object
189 190 191 |
# File 'lib/goodcheck/cli.rb', line 189 def force_download_option(opts, &block) opts.on("--force", "Download importing files always", &block) end |
#help(args) ⇒ Object
153 154 155 156 157 158 159 160 161 |
# File 'lib/goodcheck/cli.rb', line 153 def help(args) stdout.puts "Usage: goodcheck <command> [options] [args...]" stdout.puts "" stdout.puts "Commands:" COMMANDS.each do |c, msg| stdout.puts " goodcheck #{c}\t#{msg}" end EXIT_SUCCESS end |
#home_path ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/goodcheck/cli.rb', line 50 def home_path if (path = ENV["GOODCHECK_HOME"]) Pathname(path) else Pathname(Dir.home) + ".goodcheck" end end |
#init(args) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/goodcheck/cli.rb', line 132 def init(args) config_path = Pathname(DEFAULT_CONFIG_FILE) force = false OptionParser.new("Usage: goodcheck init [options]") do |opts| config_option(opts) { |config| config_path = config } (opts) opts.on("--force", "Overwrite an existing file") do force = true end end.parse!(args) Commands::Init.new(stdout: stdout, stderr: stderr, path: config_path, force: force).run end |
#pattern(args) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/goodcheck/cli.rb', line 163 def pattern(args) config_path = Pathname(DEFAULT_CONFIG_FILE) OptionParser.new do |opts| opts. = "Usage: goodcheck pattern [options] ids..." config_option(opts) { |config| config_path = config } (opts) end.parse!(args) Commands::Pattern.new(stdout: stdout, stderr: stderr, path: config_path, ids: Set.new(args), home_path: home_path).run end |
#run(args) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/goodcheck/cli.rb', line 24 def run(args) command = args.shift&.to_sym if COMMANDS.key?(command) __send__(command, args) elsif command == :"--version" version(args) else if command stderr.puts "invalid command: #{command}" stderr.puts "" end help(args) EXIT_ERROR end rescue OptionParser::ParseError => exn stderr.puts exn EXIT_ERROR rescue => exn stderr.puts exn.inspect exn.backtrace.each do |bt| stderr.puts " #{bt}" end EXIT_ERROR end |
#test(args) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/goodcheck/cli.rb', line 110 def test(args) config_path = Pathname(DEFAULT_CONFIG_FILE) loglevel = ::Logger::ERROR force_download = false OptionParser.new("Usage: goodcheck test [options]") do |opts| config_option(opts) { |config| config_path = config } verbose_option(opts) { |level| loglevel = level } debug_option(opts) { |level| loglevel = level } force_download_option(opts) { force_download = true } (opts) end.parse!(args) Goodcheck.logger.level = loglevel Goodcheck.logger.info "Configuration = #{config_path}" Goodcheck.logger.info "Force download = #{force_download}" Goodcheck.logger.info "Home path = #{home_path}" Commands::Test.new(stdout: stdout, stderr: stderr, config_path: config_path, force_download: force_download, home_path: home_path).run end |
#verbose_option(opts) ⇒ Object
181 182 183 |
# File 'lib/goodcheck/cli.rb', line 181 def verbose_option(opts) opts.on("-v", "--verbose", "Set log level to verbose") { yield ::Logger::INFO } end |
#version(_args = nil) ⇒ Object
148 149 150 151 |
# File 'lib/goodcheck/cli.rb', line 148 def version(_args = nil) stdout.puts "goodcheck #{VERSION}" EXIT_SUCCESS end |