Module: Envdoctor::CLI

Defined in:
lib/envdoctor/cli.rb

Overview

Command-line entry point.

Class Method Summary collapse

Class Method Details

.run(argv) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
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
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/envdoctor/cli.rb', line 11

def run(argv)
  return run_diff(argv[1..]) if argv.first == "diff"
  return run_sync(argv[1..]) if argv.first == "sync"
  return run_init(argv[1..]) if argv.first == "init"
  return run_fix(argv[1..]) if argv.first == "fix"

  dir = "."
  strict = false
  json = false
  parser = OptionParser.new do |o|
    o.banner = "Usage: envdoctor scan [options]"
    o.on("-d", "--dir DIR", "Project root (default: cwd)") { |v| dir = v }
    o.on("--strict", "Treat warnings as errors") { strict = true }
    o.on("--json", "Emit findings as a JSON array") { json = true }
  end
  args = argv.dup
  args.shift if args.first == "scan"
  parser.parse!(args)

  root = File.expand_path(dir)
  findings = Scanner.scan(root)
  errors = findings.select { |f| f.severity == "error" }
  warnings = findings.select { |f| f.severity == "warning" }

  if json
    puts Scanner.to_json_array(findings)
    return (!errors.empty? || (strict && !warnings.empty?)) ? 1 : 0
  end

  puts "ENVIRONMENT AUDIT"
  puts "=" * 40
  if findings.empty?
    puts "\nNo issues found."
    return 0
  end

  loc = ->(f) { f.origin ? " #{f.origin.file}:#{f.origin.line}" : "" }
  unless errors.empty?
    puts "\nErrors"
    errors.each { |f| puts "  x #{f.name}#{loc.call(f)}  #{f.message}" }
  end
  unless warnings.empty?
    puts "\nWarnings"
    warnings.each { |f| puts "  ! #{f.name}#{loc.call(f)}  #{f.message}" }
  end
  puts "\nSummary: #{errors.length} error(s), #{warnings.length} warning(s)"

  (!errors.empty? || (strict && !warnings.empty?)) ? 1 : 0
end

.run_diff(argv) ⇒ Object



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
# File 'lib/envdoctor/cli.rb', line 61

def run_diff(argv)
  dir = "."
  json = false
  pos = []
  OptionParser.new do |o|
    o.on("-d", "--dir DIR") { |v| dir = v }
    o.on("--json") { json = true }
  end.order!(argv.dup) { |a| pos << a }
  a, b = pos[0], pos[1]
  d = Scanner.diff_labels(File.expand_path(dir), a, b)
  if json
    require "json"
    puts JSON.generate({ "a" => a, "b" => b }.merge(d))
    return 0
  end
  puts "ENVIRONMENT DIFF: #{a} vs #{b}"
  puts "=" * 40
  unless d["onlyInA"].empty?
    puts "Only in #{a}:"
    d["onlyInA"].each { |k| puts "  + #{k}" }
  end
  unless d["onlyInB"].empty?
    puts "Only in #{b}:"
    d["onlyInB"].each { |k| puts "  + #{k}" }
  end
  puts "Common: #{d['common'].length} variable(s)"
  0
end

.run_fix(argv) ⇒ Object

Always (re)write both generated files.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/envdoctor/cli.rb', line 118

def run_fix(argv)
  dir = "."
  OptionParser.new do |o|
    o.on("-d", "--dir DIR") { |v| dir = v }
  end.parse!(argv.dup)

  root = File.expand_path(dir)
  example = Scanner.env_example_content(root)
  doc = Scanner.environment_doc_content(root)
  File.write(File.join(root, ".env.example"), example)
  puts "wrote .env.example"
  File.write(File.join(root, "ENVIRONMENT.md"), doc)
  puts "wrote ENVIRONMENT.md"
  0
end

.run_init(argv) ⇒ Object

Generate .env.example and ENVIRONMENT.md, writing each only if absent (or always with --force).



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/envdoctor/cli.rb', line 92

def run_init(argv)
  dir = "."
  force = false
  OptionParser.new do |o|
    o.on("-d", "--dir DIR") { |v| dir = v }
    o.on("--force") { force = true }
  end.parse!(argv.dup)

  root = File.expand_path(dir)
  files = {
    ".env.example" => Scanner.env_example_content(root),
    "ENVIRONMENT.md" => Scanner.environment_doc_content(root)
  }
  files.each do |name, content|
    path = File.join(root, name)
    if File.exist?(path) && !force
      puts "skipped #{name} (exists)"
    else
      File.write(path, content)
      puts "#{force ? 'wrote' : 'created'} #{name}"
    end
  end
  0
end

.run_sync(argv) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/envdoctor/cli.rb', line 134

def run_sync(argv)
  dir = "."
  json = false
  dry = false
  pos = []
  OptionParser.new do |o|
    o.on("-d", "--dir DIR") { |v| dir = v }
    o.on("--dry-run") { dry = true }
    o.on("--json") { json = true }
  end.order!(argv.dup) { |a| pos << a }
  from, to = pos[0], pos[1]
  added = Scanner.sync_labels(File.expand_path(dir), from, to, dry_run: dry)
  if json
    require "json"
    puts JSON.generate({ "from" => from, "to" => to, "added" => added, "dryRun" => dry })
    return 0
  end
  if added.empty?
    puts "Already in sync."
    return 0
  end
  verb = dry ? "Would sync" : "Synced"
  puts "#{verb} #{added.length} variable(s) from #{from} to #{to}:"
  added.each { |k| puts "  + #{k}" }
  0
end