class Hodor::Command

Public Class Methods

load_topic(title) click to toggle source
# File lib/hodor/command.rb, line 118
def self.load_topic(title)
  topics = File.join(File.dirname(__FILE__), '..', '..', 'topics', name.split('::').last.downcase)
  contents = File.open( File.join(topics, "#{title}.txt"), 'rt') { |f| f.read }
  contents.gsub(/^\\x5/, "\x5")
end

Public Instance Methods

dest_path() click to toggle source
# File lib/hodor/command.rb, line 96
def dest_path
  options[:to] || "."
end
env() click to toggle source
# File lib/hodor/command.rb, line 8
def env
  Environment.instance
end
erb_expand_command_line(trailing) click to toggle source

Expand any ERB variables on the command line against the loaded environment. If the environment has no value for the specified key, leave the command line unchanged.

Examples:

$ bthor sandbox:oozie --oozie "<%= env[:oozie_url] %>"
$ bthor sandbox:oozie --oozie :oozie_url

Note: Either of above works, since :oozie_url is gsub'd to <%= env %>

# File lib/hodor/command.rb, line 75
def erb_expand_command_line(trailing)
  trailing.map! { |subarg| 
    env.erb_sub(
      subarg.gsub(/(?<!\[):[a-zA-Z][_0-9a-zA-Z~]+/) { |match|
        if env.settings.has_key?(match[1..-1].to_sym)
          "<%= env[#{match}] %>" 
        else
          match
        end
      }
    )
  }
end
hadoop_command(cmd, trailing) click to toggle source
# File lib/hodor/command.rb, line 89
def hadoop_command(cmd, trailing)
  @was_intercepted = true
  cmdline = cmd ? "#{cmd} " : ""
  cmdline << trailing.join(' ')
  env.ssh cmdline, echo: true, echo_cmd: true
end
invoke(name=nil, *args) click to toggle source

Part of workaround to prevent parent command arguments from being appended to child commands

NOTE: the args argument below should actually be *args.
Calls superclass method
# File lib/hodor/command.rb, line 23
def invoke(name=nil, *args)

  name.sub!(/^Hodor:/, '') if name && $hodor_runner
  super(name, args + ["-EOLSTOP"])
end
invoke_command(command, trailing) click to toggle source
Calls superclass method
# File lib/hodor/command.rb, line 29
def invoke_command(command, trailing)
  env.options = options
  @invoking_command = command.name
  workaround_thor_trailing_bug(trailing)
  erb_expand_command_line(trailing)
  @trailing = trailing

  if self.respond_to?(:intercept_dispatch)
    @was_intercepted = false
    intercept_dispatch(command.name.to_sym, trailing)
    super unless @was_intercepted
  else
    super
  end
rescue Hodor::Cli::Usage => ex
  logger.error "CLI Usage: #{ex.message}"
rescue SystemExit, Interrupt
rescue => ex
  if env.prefs[:debug_mode]
    logger.error "EXCEPTION! #{ex.class.name} :: #{ex.message}\nBACKTRACE:\n\t#{ex.backtrace.join("\n\t")}"
  else
    logger.error "#{ex.message}\nException Class: '#{ex.class.name}'"
  end
end
load_topics() click to toggle source
# File lib/hodor/command.rb, line 124
def load_topics
  topics = File.join(File.dirname(__FILE__), '..', '..', 'topics', self.class.name.split('::').last)
  Dir.glob(File.join(topics, '*.txt'))
end
logger() click to toggle source
# File lib/hodor/command.rb, line 16
def logger
  env.logger
end
scp_file(file) click to toggle source
# File lib/hodor/command.rb, line 100
def scp_file(file)
  # If the file has .erb extension, perform ERB expansion of the file first
  if file.end_with?('.erb')
    dest_file = file.sub(/\.erb$/,'')
    erb_expanded = env.erb_load(file)
    src_file = "/tmp/#{File.basename(dest_file)}"
    File.open(src_file, 'w') { |f| f.write(erb_expanded) }
  else
    dest_file = "#{options[:parent] || ''}#{file}"
    src_file = file
  end

  file_path = "#{dest_path}/#{File.basename(src_file)}"
  env.run_local %Q[scp #{src_file} #{env.ssh_user}@#{env[:ssh_host]}:#{file_path}],
  echo: true, echo_cmd: true
  return file_path
end
target() click to toggle source
# File lib/hodor/command.rb, line 12
def target
  env.settings[:target]
end
topic(title) click to toggle source
# File lib/hodor/command.rb, line 131
def topic(title)
  say self.class.load_topic(title)
end
topics() click to toggle source
# File lib/hodor/command.rb, line 136
def topics
  say "The following topics (in no particular order) are available within the namespace:"
  load_topics.each_with_index { |topic, i|
    say "  Topic: #{File.basename(topic).sub(/.txt$/, '')}"
  }
end
workaround_thor_trailing_bug(trailing) click to toggle source

This function works around a bug in thor. Basically, when one thor command calls another (ie. via “invoke”), the parent command's last argument is appended to the arguments array of the invoked command. This function just chops off the extra arguments that shouldn't be in the trailing string.

# File lib/hodor/command.rb, line 58
def workaround_thor_trailing_bug(trailing)
  sentinel = false
  trailing.select! { |element| 
    sentinel = true if element.eql?("-EOLSTOP")
    !sentinel
  }
end