class Serverkit::Command

This class is responsible for command line interface. An instance of this class builds an instance of Actions::Base class, then calls its ‘#call` method, and it exits with exit status 0 unless any error found. This class should be used only from bin/serverkit executable. If you need to use serverkit’s any feature from Ruby code, use a child of Actions::Base class instead because this class only focuses on command line interface.

Constants

LOG_LEVELS_TABLE

Public Class Methods

new(argv) click to toggle source

@param [Array<String>] argv

# File lib/serverkit/command.rb, line 34
def initialize(argv)
  @argv = argv
end

Public Instance Methods

call() click to toggle source
# File lib/serverkit/command.rb, line 38
def call
  setup
  case action_name
  when nil
    raise Errors::MissingActionNameArgumentError
  when "apply"
    apply
  when "check"
    check
  when "inspect"
    _inspect
  when "validate"
    validate
  else
    raise Errors::UnknownActionNameError, action_name
  end
rescue Errors::Base, Psych::SyntaxError, Slop::MissingArgumentError, Slop::MissingOptionError => e
  abort "Error: #{e}"
end

Private Instance Methods

_inspect() click to toggle source

@note inspect is reserved ;(

# File lib/serverkit/command.rb, line 61
def _inspect
  Actions::Inspect.new(**action_options).call
end
action_name() click to toggle source

@return [String, nil]

# File lib/serverkit/command.rb, line 66
def action_name
  @argv.first
end
action_options() click to toggle source

@return [Hash<Symbol => String>]

# File lib/serverkit/command.rb, line 71
def action_options
  {
    hosts: hosts,
    log_level: log_level,
    recipe_path: recipe_path,
    variables_path: variables_path,
    ssh_options: ssh_options,
  }.reject do |key, value|
    value.nil?
  end
end
apply() click to toggle source
# File lib/serverkit/command.rb, line 83
def apply
  Actions::Apply.new(**action_options).call
end
check() click to toggle source
# File lib/serverkit/command.rb, line 87
def check
  Actions::Check.new(**action_options).call
end
command_line_options() click to toggle source

@note This options are commonly used from all actions for now, however,

someday a new action that requires options might appear.

@return [Slop]

# File lib/serverkit/command.rb, line 94
def command_line_options
  @command_line_options ||= Slop.parse!(@argv, help: true) do
    banner "Usage: serverkit ACTION [options]"
    on "--hosts=", "Hostname to execute command over SSH"
    on "--log-level=", "Log level (DEBUG, INFO, WARN, ERROR, FATAL)"
    on "--no-color", "Disable coloring"
    on "--variables=", "Path to variables file for ERB recipe"
    on "--ssh-user=", "SSH user name"
    on "--ssh-identity=", "SSH identity (private key) file path"
  end
end
hosts() click to toggle source

@return [String, nil]

# File lib/serverkit/command.rb, line 107
def hosts
  command_line_options["hosts"]
end
log_level() click to toggle source

@return [Fixnum]

# File lib/serverkit/command.rb, line 112
def log_level
  LOG_LEVELS_TABLE[command_line_options["log-level"].to_s.upcase] || ::Logger::UNKNOWN
end
recipe_path() click to toggle source

@return [String]

# File lib/serverkit/command.rb, line 117
def recipe_path
  @argv[1] or raise Errors::MissingRecipePathArgumentError
end
setup() click to toggle source
# File lib/serverkit/command.rb, line 121
def setup
  ::Rainbow.enabled = !command_line_options["no-color"]
end
ssh_options() click to toggle source
# File lib/serverkit/command.rb, line 133
def ssh_options
  identity = command_line_options["ssh-identity"]
  keys = identity ? [identity] : nil
  {
    user: command_line_options["ssh-user"],
    keys: keys,
  }.reject do |key, value|
    value.nil?
  end
end
validate() click to toggle source
# File lib/serverkit/command.rb, line 125
def validate
  Actions::Validate.new(**action_options).call
end
variables_path() click to toggle source
# File lib/serverkit/command.rb, line 129
def variables_path
  command_line_options["variables"]
end