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",
  help: "Print this message"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout:, stderr:) ⇒ CLI

Returns a new instance of CLI.



8
9
10
11
# File 'lib/goodcheck/cli.rb', line 8

def initialize(stdout:, stderr:)
  @stdout = stdout
  @stderr = stderr
end

Instance Attribute Details

#stderrObject (readonly)

Returns the value of attribute stderr.



6
7
8
# File 'lib/goodcheck/cli.rb', line 6

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



5
6
7
# File 'lib/goodcheck/cli.rb', line 5

def stdout
  @stdout
end

Instance Method Details

#check(args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
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
# File 'lib/goodcheck/cli.rb', line 37

def check(args)
  config_path = Pathname("goodcheck.yml")
  targets = []
  rules = []
  format = nil

  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=FORMAT") do |f|
      format = f
    end
  end.parse!(args)

  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

  Commands::Check.new(reporter: reporter, config_path: config_path, rules: rules, targets: targets, stderr: stderr).run
end

#help(args) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/goodcheck/cli.rb', line 102

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

#init(args) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/goodcheck/cli.rb', line 86

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/goodcheck/cli.rb', line 21

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



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/goodcheck/cli.rb', line 74

def test(args)
  config_path = Pathname("goodcheck.yml")

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

  Commands::Test.new(stdout: stdout, stderr: stderr, config_path: config_path).run
end