Class: Goodcheck::CLI

Inherits:
Object
  • Object
show all
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",
  version: "Print version",
  help: "Show goodcheck version and quit"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

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



48
49
50
51
52
53
54
55
56
57
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
# File 'lib/goodcheck/cli.rb', line 48

def check(args)
  config_path = Pathname("goodcheck.yml")
  targets = []
  rules = []
  format = nil
  loglevel = Logger::ERROR
  force_download = false

  OptionParser.new("Usage: goodcheck check [options] dirs...") do |opts|
    opts.on("-c CONFIG", "--config=CONFIG") do |config|
      config_path = Pathname(config)
    end
    opts.on("-R RULE", "--rule=RULE") do |rule|
      rules << rule
    end
    opts.on("--format=<text|json> [default: 'text']") do |f|
      format = f
    end
    opts.on("-v", "--verbose") do
      loglevel = Logger::INFO
    end
    opts.on("-d", "--debug") do
      loglevel = Logger::DEBUG
    end
    opts.on("--force") do
      force_download = true
    end
  end.parse!(args)

  Goodcheck.logger.level = loglevel

  if args.empty?
    targets << Pathname(".")
  else
    targets.push *args.map {|arg| Pathname(arg) }
  end

  reporter = case format
             when "text", nil
               Reporters::Text.new(stdout: stdout)
             when "json"
               Reporters::JSON.new(stdout: stdout, stderr: stderr)
             else
               stderr.puts "Unknown format: #{format}"
               return 1
             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

#help(args) ⇒ Object



155
156
157
158
159
160
161
162
163
# File 'lib/goodcheck/cli.rb', line 155

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
  0
end

#home_path ⇒ Object



40
41
42
43
44
45
46
# File 'lib/goodcheck/cli.rb', line 40

def home_path
  if (path = ENV["GOODCHECK_HOME"])
    Pathname(path)
  else
    Pathname(Dir.home) + ".goodcheck"
  end
end

#init(args) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/goodcheck/cli.rb', line 134

def init(args)
  config_path = Pathname("goodcheck.yml")
  force = false

  OptionParser.new("Usage: goodcheck init [options]") do |opts|
    opts.on("-c CONFIG", "--config=CONFIG") do |config|
      config_path = Pathname(config)
    end
    opts.on("--force") do
      force = true
    end
  end.parse!(args)

  Commands::Init.new(stdout: stdout, stderr: stderr, path: config_path, force: force).run
end

#run(args) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/goodcheck/cli.rb', line 24

def run(args)
  command = args.shift&.to_sym

  if COMMANDS.key?(command)
    __send__(command, args)
  else
    help(args)
  end
rescue => exn
  stderr.puts exn.inspect
  exn.backtrace.each do |bt|
    stderr.puts "  #{bt}"
  end
  1
end

#test(args) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/goodcheck/cli.rb', line 105

def test(args)
  config_path = Pathname("goodcheck.yml")
  loglevel = Logger::ERROR
  force_download = false

  OptionParser.new("Usage: goodcheck test [options]") do |opts|
    opts.on("-c CONFIG", "--config=CONFIG") do |config|
      config_path = Pathname(config)
    end
    opts.on("-v", "--verbose") do
      loglevel = Logger::INFO
    end
    opts.on("-d", "--debug") do
      loglevel = Logger::DEBUG
    end
    opts.on("--force") do
      force_download = true
    end
  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

#version(args) ⇒ Object



150
151
152
153
# File 'lib/goodcheck/cli.rb', line 150

def version(args)
  stdout.puts "goodcheck #{VERSION}"
  0
end