Class: FastCodeOwners::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_code_owners/cli.rb

Class Method Summary collapse

Class Method Details

.for_file(argv) ⇒ Object

For now, this just returns team ownership Later, this could also return code ownership errors about that file.



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

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/codeowners 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/codeowners for_file --help` for more info'
  end

  team = FastCodeOwners.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 "      Team: \#{team_name}\n      Team YML: \#{team_yml}\n    MSG\n  end\nend\n"

.for_team(argv) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/fast_code_owners/cli.rb', line 123

def self.for_team(argv)
  parser = OptionParser.new do |opts|
    opts.banner = 'Usage: bin/codeowners 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/codeowners for_team --help` for more info'
  end

  puts FastCodeOwners.for_team(teams.first).join("\n")
end

.run!(argv) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fast_code_owners/cli.rb', line 10

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: bin/codeowners <subcommand>\n\n      Subcommands:\n        validate - run all validations\n        for_file - find code ownership for a single file\n        for_team - find code ownership information for a team\n        help  - display help information about code_ownership\n    USAGE\n  else\n    puts \"'\#{command}' is not a code_ownership command. See `bin/codeowners help`.\"\n  end\nend\n"