Class: Rallycat::CLI

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

Instance Method Summary collapse

Constructor Details

#initialize(argv, stdout = STDOUT) ⇒ CLI

Returns a new instance of CLI.



5
6
7
8
# File 'lib/rallycat/cli.rb', line 5

def initialize(argv, stdout=STDOUT)
  @argv   = argv
  @stdout = stdout
end

Instance Method Details

#runObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rallycat/cli.rb', line 10

def run
  options = {}

  global = OptionParser.new do |opts|
    opts.on('-u USERNAME', '--username') do |user|
      options[:user] = user
    end

    opts.on('-p PASSWORD', '--password') do |password|
      options[:password] = password
    end

    opts.on('-h', '--help') do
      @stdout.puts Rallycat::Help.new
      exit
    end
  end

  commands = {
    'cat' => OptionParser.new,

    'update' => OptionParser.new do |opts|
      opts.banner = 'Usage: rallycat update <story number> [options]'

      opts.on('-b', '--blocked') do |blocked|
        options[:blocked] = true
      end

      opts.on('-p', '--in-progress') do |in_progress|
        options[:in_progress] = true
      end

      opts.on('-c', '--completed') do |completed|
        options[:completed] = true
      end

      opts.on('-d', '--defined') do |defined|
        options[:defined] = true
      end

      opts.on('-o OWNER', '--owner') do |owner|
        options[:owner] = owner
      end
    end,

    'help' => OptionParser.new
  }

  global.order! @argv

  command = @argv.shift
  commands[command].order! @argv if commands.has_key? command

  case command
  when 'cat'
    api = Rallycat::Connection.new(options[:user], options[:password]).api

    story_number = @argv.shift

    abort 'The "cat" command requires a story or defect number.' unless story_number

    begin
      @stdout.puts Rallycat::Cat.new(api).story(story_number)
    rescue Rallycat::Cat::StoryNotFound => e
      abort e.message
    end
  when 'update'
    api = Rallycat::Connection.new(options[:user], options[:password]).api

    task_number = @argv.shift

    abort 'The "update" command requires a task number.' unless task_number

    opts = {}
    opts[:blocked] = true            if options[:blocked]
    opts[:state]   = "In-Progress"   if options[:in_progress]
    opts[:state]   = "Completed"     if options[:completed]
    opts[:state]   = "Defined"       if options[:defined]
    opts[:owner]   = options[:owner] if options[:owner]

    begin
      @stdout.puts Rallycat::Update.new(api).task(task_number, opts)
    rescue Rallycat::Update::UserNotFound, Rallycat::Update::TaskNotFound => e
      abort e.message
    end
  when 'help'
    # `puts` calls `to_s`
    @stdout.puts Rallycat::Help.new
  else
    @stdout.puts "'#{command}' is not a supported command. See 'rallycat help'."
  end
end