Class: CopyGithubLabels::CLI

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

Class Method Summary collapse

Class Method Details

.parse_args(args) ⇒ Object



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

def parse_args(args)
  options = { override: false }
  credentials = {}

  OptionParser.new do |opts|
    opts.banner = "Usage: copy_github_labels [OPTION]... SOURCE_REPO TARGET_REPO"

    opts.on("-f", "--force", "Override existing labels") do
      options[:override] = true
    end

    opts.on("-uUSERNAME", "--username==USERNAME", "GitHub username") do |username|
      credentials[:login] = username
    end

    opts.on("-tTOKEN", "--token==TOKEN", "GitHub access token") do |token|
      credentials[:access_token] = token
    end

    opts.on("-h", "--help", "Print help") do
      puts opts
      exit
    end
  end.parse!(args)

  {
    args: args,
    credentials: credentials,
    options: options
  }
end

.run(args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/copy_github_labels/cli.rb', line 6

def run(args)
  parsed_options = parse_args(args)
  options = parsed_options.fetch(:options)
  credentials = parsed_options.fetch(:credentials)
  args = parsed_options.fetch(:args)

  if args.length != 2
    puts parse_args(%W(--help))
  else
    source, target = args

    if credentials[:login]
      credentials[:password] = TTY::Prompt.new.mask("GitHub account password: ")
    end

    CopyGithubLabels::Client.new(credentials)
                            .copy_labels(source, target, options.slice(:override))
  end
end