Class: Ebm

Inherits:
Object
  • Object
show all
Includes:
Subcommands
Defined in:
lib/ebm.rb

Constant Summary collapse

CMD =
"ebm"

Instance Method Summary collapse

Instance Method Details

#define_sub_commandsObject

define sub commands here



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ebm.rb', line 44

def define_sub_commands
  sub_commands[:prepare] = Commands::Prepare.new
  sub_commands[:configs] = Commands::Configs.new
  sub_commands[:make_sample] = Commands::MakeSample.new
  sub_commands[:remove_merged_branches] = Commands::RemoveMergedBranches.new
  sub_commands[:tag] = Commands::Tag.new
  sub_commands[:status] = Commands::Status.new
  sub_commands[:periodic] = Commands::Periodic.new
  sub_commands[:pull] = Commands::Pull.new
  sub_commands[:format_helper] = Commands::FormatHelper.new
  sub_commands[:prune] = Commands::Prune.new
  sub_commands[:push] = Commands::Push.new
  sub_commands[:commit] = Commands::Commit.new
  sub_commands[:version] = Commands::Version.new
end

#force_fail(cmd = nil) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/ebm.rb', line 85

def force_fail(cmd = nil)
  # force failure and show options
  ARGV.clear
  ARGV << "help"
  ARGV << cmd unless cmd.nil?
  opt_parse
end

#optionsObject

the options that were set



31
32
33
# File 'lib/ebm.rb', line 31

def options
  @options ||= {}
end

#parseObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ebm.rb', line 110

def parse
  if ARGV.empty?
    ARGV << "help"
  end

  cmd = opt_parse()
  if cmd.nil?
    force_fail
  end

  # ok, we have a valid command so dispatch it
#    puts "cmd: #{cmd}"
#    puts "options ......"
#    p options
#    puts "ARGV:"
#    p ARGV

  sub_cmd = sub_commands[cmd.to_sym]
  validate(cmd, sub_cmd)

  # track both types of options
  EbmSharedLib::Options.global_options = options
  EbmSharedLib::Options.cmd_options = sub_cmd.options

  sub_cmd.send("run", options)
end


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/ebm.rb', line 159

def print_actions
  cmdtext = "Commands are:"
  @commands.sort.map do |c, opt|
    puts "inside opt.call loop"
    desc = opt.call.description
    unless c == "remove_merged_branches"
      cmdtext << "\n   #{c} : #{desc}"
    end
  end

  # print aliases
  unless @aliases.empty?
    cmdtext << "\n\nAliases: \n"
    @aliases.each_pair { |name, val| cmdtext << "   #{name} - #{val}\n"  }
  end

  cmdtext
end

#printerObject



39
40
41
# File 'lib/ebm.rb', line 39

def printer
  EbmSharedLib.printer
end

#required_optionsObject

required options



26
27
28
# File 'lib/ebm.rb', line 26

def required_options
  @required_options ||= Set.new []
end

#run(argv = ARGV) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/ebm.rb', line 139

def run(argv = ARGV)
  exit_code = true
  begin
    setup
    parse
  rescue SystemExit => ex
    # ignore direct calls to exit
  rescue Exception => ex
    printer.error printer.color(ex.message, :red)
#      puts ex.backtrace
    exit_code = false
  end

  # make sure buffer is flushed
  # debugger doesn't seem to do this always
  STDOUT.flush

  return exit_code
end

#setupObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ebm.rb', line 60

def setup
  options.clear
  # global options
  global_options do |opts|
    opts.banner = "Version: #{Info.version} - Usage: #{CMD} [options] [subcommand [options]]"
    opts.description = "eBay Mobile configuration and deploy tool.  You must specify a valid sub command."
    opts.separator ""
    opts.separator "Global options are:"
    opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
      options[:verbose] = v
    end

  end
  add_help_option

  define_sub_commands

  sub_commands.each_pair do |command_name, sub_cmd|
    command command_name do |opts|
      sub_cmd.send("register", opts, options)
    end
  end

end

#sub_commandsObject



35
36
37
# File 'lib/ebm.rb', line 35

def sub_commands
  @sub_commands ||= {}
end

#validate(cmd, sub_cmd) ⇒ Object

validate the options passed



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ebm.rb', line 94

def validate(cmd, sub_cmd)
  required_options.each do |r|
    if options.has_key?(r) == false
      puts "Missing options"
      force_fail
    end
  end

  sub_cmd.required_options.each do |r|
    if sub_cmd.options.has_key?(r) == false
      puts "Missing options"
      force_fail(cmd)
    end
  end
end