Class: Hudkins::Command

Inherits:
Object
  • Object
show all
Extended by:
Exec
Includes:
Common
Defined in:
lib/hudkins/command.rb

Overview

Description

Main class for the included bin to interact with Hudson at the command line.

The Commands are setup in Hudkins::Command::Exec

Usage

see ‘hudkins -h’

a host option is required. Can be configured in env variable hudkins_host or config file. all options can be specified in a yaml file in ~/.hudkinsrc and/or ‘pwd`/.hudkinsrc

Defined Under Namespace

Modules: Exec, Irb

Class Method Summary collapse

Methods included from Exec

cmd_list, command_list, config, hud, hud_host, hudkins_rc, job, load_rc, names, required_params, run_build, run_config, run_create, run_default, run_host, run_list, run_start_irb

Methods included from Common

#check_host_availability, #get, #host_available?, #hudkins, #inspect, #post, #url_escape

Class Method Details

.parse_commandObject



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/hudkins/command.rb', line 113

def parse_command
  # select unambiguous command to run.
  cmds = cmd_list.select {|c| Regexp.new(@command.to_s, Regexp::IGNORECASE) === c}
  case cmds.size
  when 0 then
    raise_usage "#{@command} is not recognized as a valid command."
  when 1 then
    @command = "run_" << cmds.first
  else
    raise_usage "Ambiguous command. Please specify:\n  #{cmds.join("\n  ")}\n\n"
  end
end

.parse_optionsObject



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
102
103
104
105
106
107
108
109
110
111
# File 'lib/hudkins/command.rb', line 65

def parse_options
  # initialize cmd_list. This seems hacky, but I like having the cmd_list
  # near the usage statement.
  @options = {}

  OptionParser.new do |opts|
    opts.banner = usage_msg

    opts.separator ""
    opts.separator "Specific options:"

    opts.on("--hud-host HOST", "Hudson host to connect to. Overrides rc file") do |h|
      @options[:host] = h
    end

    opts.on("-i", "--irb", "Drop into irb.") do |i|
      @options[:irb] = i
    end
    opts.on("-v", "--[no-]verbose", "Turn on verbose messages.") do |v|
      $VERBOSE = @options[:verbose] = v
    end

    opts.separator ""
    opts.separator "Common options:"

    opts.on_tail("--version", "Show version") do
      puts "Hudkins (#{Hudkins::VERSION}) (c) 2010"
      exit
    end

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

  end.parse!

  config.merge! @options

  @command, @job_name = ARGV.shift(2)
  @command ||= "default"

  @hud = Hudkins.new hud_host
  job @job_name

  parse_command
end

.raise_usage(msg = nil) ⇒ Object



59
60
61
62
63
# File 'lib/hudkins/command.rb', line 59

def raise_usage msg = nil
  warn msg if msg
  puts usage_msg
  exit 1
end

.runObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hudkins/command.rb', line 25

def run
  parse_options
  begin
    return_string = send( @command )
    puts return_string if return_string
  rescue => e
    raise_usage e.message
  end
  if @options[:irb]
    run_start_irb
  end
end

.usage_msgObject

commands: helpers:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/hudkins/command.rb', line 40

def usage_msg
  usage = <<-EOB

Usage: hudkins [opts] commands [job_name]

  Commands:   unambiguous_partial_command
  build:    start a job building.
  config:   pretty print job config. job_name
  create:   create new hudson job.
  host:     print hudson host url
  list:     list jobs. [job_name used as filter].


  Optional:   job_name (depends on command)
may also be partial (picks first match)

  EOB
end