Class: Gizzard::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/gizzard/commands.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manager, job_injector, global_options, argv, command_options) ⇒ Command

Returns a new instance of Command.



47
48
49
50
51
52
53
# File 'lib/gizzard/commands.rb', line 47

def initialize(manager, job_injector, global_options, argv, command_options)
  @manager      = manager
  @job_injector    = job_injector
  @global_options  = global_options
  @argv            = argv
  @command_options = command_options
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



45
46
47
# File 'lib/gizzard/commands.rb', line 45

def argv
  @argv
end

#bufferObject (readonly)

Returns the value of attribute buffer.



8
9
10
# File 'lib/gizzard/commands.rb', line 8

def buffer
  @buffer
end

#command_optionsObject (readonly)

Returns the value of attribute command_options.



45
46
47
# File 'lib/gizzard/commands.rb', line 45

def command_options
  @command_options
end

#global_optionsObject (readonly)

Returns the value of attribute global_options.



45
46
47
# File 'lib/gizzard/commands.rb', line 45

def global_options
  @global_options
end

#job_injectorObject (readonly)

Returns the value of attribute job_injector.



45
46
47
# File 'lib/gizzard/commands.rb', line 45

def job_injector
  @job_injector
end

#managerObject (readonly)

Returns the value of attribute manager.



45
46
47
# File 'lib/gizzard/commands.rb', line 45

def manager
  @manager
end

Class Method Details

.classify(string) ⇒ Object



25
26
27
# File 'lib/gizzard/commands.rb', line 25

def classify(string)
  string.split(/\W+/).map{|s| s.capitalize }.join("")
end

.make_job_injector(global_options, log) ⇒ Object



39
40
41
42
# File 'lib/gizzard/commands.rb', line 39

def make_job_injector(global_options, log)
  RetryProxy.new global_options.retry,
    JobInjector.new(global_options.hosts.first, global_options.injector_port, log, true, global_options.dry)
end

.make_manager(global_options, log) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/gizzard/commands.rb', line 29

def make_manager(global_options, log)
  raise "No hosts specified" unless global_options.hosts
  hosts = global_options.hosts.map {|h| [h, global_options.port].join(":") }

  Nameserver.new(hosts, :retries => global_options.retry,
                        :log     => log,
                        :framed  => global_options.framed,
                        :dry_run => global_options.dry)
end

.run(command_name, global_options, argv, subcommand_options, log) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gizzard/commands.rb', line 11

def run(command_name, global_options, argv, subcommand_options, log)
  command_class = Gizzard.const_get("#{classify(command_name)}Command")

  @manager      ||= make_manager(global_options, log)
  @job_injector ||= make_job_injector(global_options, log)

  command = command_class.new(@manager, @job_injector, global_options, argv, subcommand_options)
  command.run

  if command.buffer && command_name = global_options.render.shift
    run(command_name, global_options, command.buffer, OpenStruct.new, log)
  end
end

Instance Method Details

#confirm!(message = "Continue?") ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/gizzard/commands.rb', line 103

def confirm!(message="Continue?")
  unless global_options.force
    begin
    print "#{message} (y/n) "; $stdout.flush
    resp = $stdin.gets.chomp.downcase
    puts ""
    end while resp != 'y' && resp != 'n'
    exit if resp == 'n'
  end
end

#get_base_name(transformations) ⇒ Object

Infer the shard base_name from a list of transformations



96
97
98
99
100
101
# File 'lib/gizzard/commands.rb', line 96

def get_base_name(transformations)
  # Gets the first valid tree from the map of transformations -> applicable trees
  # Trees are maps of forwardings -> shards. Gets the first valid shard from the this tree.
  # Gets the ShardId of that shard and pulls the base name out of the table prefix
  transformations.values.find {|v| v.is_a?(Hash) && !v.values.empty? }.values.find {|v| !v.nil?}.id.table_prefix.split('_').first
end

#help!(message = nil) ⇒ Object

Raises:



55
56
57
# File 'lib/gizzard/commands.rb', line 55

def help!(message = nil)
  raise HelpNeededError, message
end

#output(string) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/gizzard/commands.rb', line 59

def output(string)
  if global_options.render.any?
    @buffer ||= []
    @buffer << string.strip
  else
    puts string
  end
end

#require_tablesObject



68
69
70
71
72
73
# File 'lib/gizzard/commands.rb', line 68

def require_tables
  if !global_options.tables
    puts "Please specify tables to repair with the --tables flag" 
    exit 1
  end
end

#require_template_optionsObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/gizzard/commands.rb', line 75

def require_template_options
  options = command_options.template_options || {}
  if global_options.template_options && global_options.template_options[:simple]
      fail = false
      if !options[:concrete]
        fail = true
        STDERR.puts "Please specify a concrete shard type with --concrete flag when using --simple"
      end
      if !options[:source_type]
        fail = true
        STDERR.puts "Please specify a source data type with --source-type flag when using --simple"
      end
      if !options[:dest_type]
        fail = true
        STDERR.puts "Please specify a destination data type with --dest-type flag when using --simple"
      end
      exit 1 if fail
    end
end