Class: Gitballs::CLI

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

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



7
8
9
10
# File 'lib/gitballs/cli.rb', line 7

def initialize(argv)
  @argv = argv
  @options = {}
end

Instance Method Details

#build_parserObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gitballs/cli.rb', line 33

def build_parser
  OptionParser.new do |opts|
    opts.banner = "Usage: gitballs <command> [options]"
    opts.separator ""
    opts.separator "Commands:"
    opts.separator "  init <purl>     Download and compress package versions into git repo"
    opts.separator "  stats <path>    Show stats for an existing gitballs repo"
    opts.separator "  version         Show version"
    opts.separator ""
    opts.separator "Options:"

    opts.on("-o", "--output DIR", "Output directory (default: ./gitballs/<package>)") do |dir|
      @options[:output] = dir
    end

    opts.on("-q", "--quiet", "Suppress progress output") do
      @options[:quiet] = true
    end

    opts.on("-h", "--help", "Show this help") do
      puts opts
      exit
    end
  end
end

#runObject



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

def run
  parser = build_parser
  args = parser.parse(@argv)
  command = args.shift

  case command
  when "init"
    run_init(args)
  when "stats"
    run_stats(args)
  when "version", "-v", "--version"
    puts "gitballs #{VERSION}"
  when "help", nil
    puts parser
  else
    warn "Unknown command: #{command}"
    puts parser
    exit 1
  end
end

#run_init(args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gitballs/cli.rb', line 59

def run_init(args)
  purl = args.first
  unless purl
    warn "Error: purl argument required"
    warn "Usage: gitballs init <purl>"
    warn "Example: gitballs init pkg:gem/rails"
    exit 1
  end

  compressor = Compressor.new(purl, output: @options[:output], quiet: @options[:quiet])
  compressor.run
  puts compressor.stats unless @options[:quiet]
  puts "output: #{compressor.output_dir}"
rescue Error => e
  warn "Error: #{e.message}"
  exit 1
end

#run_stats(args) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gitballs/cli.rb', line 77

def run_stats(args)
  path = args.first
  unless path
    warn "Error: path argument required"
    warn "Usage: gitballs stats <path>"
    exit 1
  end

  unless File.directory?(path)
    warn "Error: #{path} is not a directory"
    exit 1
  end

  stats = Stats.new(path)
  puts stats
rescue Error => e
  warn "Error: #{e.message}"
  exit 1
end