Class: CodeOwnership::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/code_ownership/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.



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

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

.for_team(argv) ⇒ Object



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

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

.run!(argv) ⇒ Object



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

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