-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathcli.rb
More file actions
144 lines (118 loc) · 3.86 KB
/
cli.rb
File metadata and controls
144 lines (118 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# typed: true
require 'optparse'
require 'pathname'
require 'fileutils'
module CodeOwnership
class Cli
def self.run!(argv)
command = argv.shift
if command == 'validate'
validate!(argv)
elsif command == 'for_file'
for_file(argv)
elsif command == 'for_team'
for_team(argv)
elsif [nil, 'help'].include?(command)
puts <<~USAGE
Usage: bin/codeownership <subcommand>
Subcommands:
validate - run all validations
for_file - find code ownership for a single file
for_team - find code ownership information for a team
help - display help information about code_ownership
USAGE
else
puts "'#{command}' is not a code_ownership command. See `bin/codeownership help`."
end
end
def self.validate!(argv)
options = {}
parser = OptionParser.new do |opts|
opts.banner = 'Usage: bin/codeownership validate [options]'
opts.on('--skip-autocorrect', 'Skip automatically correcting any errors, such as the .github/CODEOWNERS file') do
options[:skip_autocorrect] = true
end
opts.on('-d', '--diff', 'Only run validations with staged files') do
options[:diff] = true
end
opts.on('-s', '--skip-stage', 'Skips staging the CODEOWNERS file') do
options[:skip_stage] = true
end
opts.on('--help', 'Shows this prompt') do
puts opts
exit
end
end
args = parser.order!(argv)
parser.parse!(args)
files = if options[:diff]
ENV.fetch('CODEOWNERS_GIT_STAGED_FILES') { %x(git diff --staged --name-only) }.split("\n").select do |file|
File.exist?(file)
end
else
nil
end
CodeOwnership.validate!(
files: files,
autocorrect: !options[:skip_autocorrect],
stage_changes: !options[:skip_stage]
)
end
# For now, this just returns team ownership
# Later, this could also return code ownership errors about that file.
def self.for_file(argv)
options = {}
# Long-term, we probably want to use something like `thor` so we don't have to implement logic
# like this. In the short-term, this is a simple way for us to use the built-in OptionParser
# while having an ergonomic CLI.
files = argv.reject { |arg| arg.start_with?('--') }
parser = OptionParser.new do |opts|
opts.banner = 'Usage: bin/codeownership for_file [options]'
opts.on('--json', 'Output as JSON') do
options[:json] = true
end
opts.on('--help', 'Shows this prompt') do
puts opts
exit
end
end
args = parser.order!(argv)
parser.parse!(args)
if files.count != 1
raise 'Please pass in one file. Use `bin/codeownership for_file --help` for more info'
end
team = CodeOwnership.for_file(files.first)
team_name = team&.name || 'Unowned'
team_yml = team&.config_yml || 'Unowned'
if options[:json]
json = {
team_name: team_name,
team_yml: team_yml,
}
puts json.to_json
else
puts <<~MSG
Team: #{team_name}
Team YML: #{team_yml}
MSG
end
end
def self.for_team(argv)
parser = OptionParser.new do |opts|
opts.banner = "Usage: bin/codeownership for_team 'Team Name'"
opts.on('--help', 'Shows this prompt') do
puts opts
exit
end
end
teams = argv.reject { |arg| arg.start_with?('--') }
args = parser.order!(argv)
parser.parse!(args)
if teams.count != 1
raise 'Please pass in one team. Use `bin/codeownership for_team --help` for more info'
end
puts CodeOwnership.for_team(teams.first)
end
private_class_method :validate!
end
end