Class: SpreenPr::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/spreen_pr/cli.rb,
sig/generated/spreen_pr/cli.rbs

Overview

Command line interface behind the pr-title executable: pr-title [BRANCH_NAME] [--format text|json].

Constant Summary collapse

FORMATS =

: Array

Returns:

  • (Array[String])
%w[text json].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.

Parameters:

  • argv (Array[String])


23
24
25
26
27
# File 'lib/spreen_pr/cli.rb', line 23

def initialize(argv)
  @argv   = argv.dup
  @format = 'text'
  @action = :print_title
end

Instance Attribute Details

#actionSymbol (readonly)

: Symbol

Returns:

  • (Symbol)


42
43
44
# File 'lib/spreen_pr/cli.rb', line 42

def action
  @action
end

#argvArray[String] (readonly)

: Array

Returns:

  • (Array[String])


40
41
42
# File 'lib/spreen_pr/cli.rb', line 40

def argv
  @argv
end

#formatString (readonly)

: String

Returns:

  • (String)


41
42
43
# File 'lib/spreen_pr/cli.rb', line 41

def format
  @format
end

Class Method Details

.start(argv = ARGV) ⇒ Integer

Parameters:

  • argv (Array[String]) (defaults to: ARGV)

Returns:

  • (Integer)


17
18
19
# File 'lib/spreen_pr/cli.rb', line 17

def self.start(argv = ARGV)
  new(argv).run
end

Instance Method Details

#branch_nameString

Returns:

  • (String)


64
65
66
67
68
69
70
# File 'lib/spreen_pr/cli.rb', line 64

def branch_name
  branch = argv.shift || current_git_branch
  return branch unless branch.nil? || branch.empty?

  raise ArgumentError,
        'No branch name given and the current git branch could not be detected'
end

#current_git_branchString?

Returns:

  • (String, nil)


73
74
75
76
77
# File 'lib/spreen_pr/cli.rb', line 73

def current_git_branch
  IO.popen(%w[git branch --show-current], err: File::NULL, &:read).strip
rescue SystemCallError
  nil
end

#parserOptionParser

Returns:

  • (OptionParser)


80
81
82
83
84
85
86
87
# File 'lib/spreen_pr/cli.rb', line 80

def parser
  @parser ||= OptionParser.new('Usage: pr-title [BRANCH_NAME] [options]') do |opt|
    opt.on('--format FORMAT', FORMATS,
           "Output format (#{FORMATS.join(' or ')}, default: text)") { |value| @format = value }
    opt.on('--version', 'Print the version') { @action = :print_version }
    opt.on('-h', '--help', 'Print this help') { @action = :print_help }
  end
end

Returns:

  • (Integer)


58
59
60
61
# File 'lib/spreen_pr/cli.rb', line 58

def print_help
  puts parser
  0
end

Returns:

  • (Integer)


45
46
47
48
49
# File 'lib/spreen_pr/cli.rb', line 45

def print_title
  application = Application.new(branch_name:)
  puts(format == 'json' ? JSON.generate(application.to_h) : application.title)
  0
end

Returns:

  • (Integer)


52
53
54
55
# File 'lib/spreen_pr/cli.rb', line 52

def print_version
  puts VERSION
  0
end

#runInteger

Returns:

  • (Integer)


30
31
32
33
34
35
36
# File 'lib/spreen_pr/cli.rb', line 30

def run
  parser.parse!(argv)
  __send__(action)
rescue ArgumentError, OptionParser::ParseError => e
  warn e.message
  1
end