Class: CodeOwnership::Cli

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

Constant Summary collapse

EXECUTABLE =
'bin/codeownership'

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.



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

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: #{EXECUTABLE} for_file [options]"

    opts.on('--json', 'Output as JSON') do
      options[:json] = true
    end

    opts.on('--verbose', 'Output verbose information') do
      options[:verbose] = 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 `#{EXECUTABLE} for_file --help` for more info"
  end

  puts CodeOwnership::Private::ForFileOutputBuilder.build(file_path: files.first, json: !!options[:json], verbose: !!options[:verbose])
end

.for_team(argv) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/code_ownership/cli.rb', line 128

def self.for_team(argv)
  parser = OptionParser.new do |opts|
    opts.banner = "Usage: #{EXECUTABLE} 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 `#{EXECUTABLE} for_team --help` for more info"
  end

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

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

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 command == 'version'
    version
  elsif [nil, 'help'].include?(command)
    puts "      Usage: \#{EXECUTABLE} <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 `\#{EXECUTABLE} help`.\"\n  end\nend\n"

.validate!(argv) ⇒ Object



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

def self.validate!(argv)
  options = {}

  parser = OptionParser.new do |opts|
    opts.banner = "Usage: #{EXECUTABLE} validate [options] [files...]"

    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)

  # Collect any remaining arguments as file paths
  specified_files = argv.reject { |arg| arg.start_with?('--') }

  files = if !specified_files.empty?
            # Files explicitly provided on command line
            if options[:diff]
              warn 'Warning: Ignoring --diff flag because explicit files were provided'
            end
            specified_files.select { |file| File.exist?(file) }
          elsif options[:diff]
            # Staged files from git
            ENV.fetch('CODEOWNERS_GIT_STAGED_FILES') { `git diff --staged --name-only` }.split("\n").select do |file|
              File.exist?(file)
            end
          else
            # No files specified, validate all
            nil
          end

  CodeOwnership.validate!(
    files: files,
    autocorrect: !options[:skip_autocorrect],
    stage_changes: !options[:skip_stage]
  )
end

.versionObject



88
89
90
# File 'lib/code_ownership/cli.rb', line 88

def self.version
  puts CodeOwnership.version.join("\n")
end