Class: Sshez::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/sshez/command.rb

Overview

Keeps track of the which command the user called

Constant Summary collapse

PRINTER =
PrintingManager.instance
ALL =

a list of all commands

{
  'add' => Command.new('add',
    (proc { |args| (args.length == 2) && (args[1].include?('@')) }),
    'sshez add <alias> (role@host) [options]',
    (proc { |alias_name, role_host| [alias_name] + role_host.split('@') })),
  'remove' => Command.new('remove', (proc { |args| args.length == 1 }),
    'sshez remove <alias>'),
  'rm' => Command.new('remove', (proc { |args| args.length == 1 }),
    'sshez rm <alias>'),
  'connect' => Command.new('connect', (proc { |args| args.length == 1 }),
    'sshez connect <alias>'),
  'reset' => Command.new('reset', (proc { |args| args.length == 0 }),
    'sshez reset'),
  'list' => Command.new('list', (proc { |args| args.empty? }), 'sshez list')
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, validator, correct_format, args_processor = nil) ⇒ Command

no one should create a command except from this class!

name: can only be one of these [add, remove, list] validator: a proc that returns true only if the input is valid for the command args: the args it self! correct_format: the way the user should run this command args_processor: (optional) a proc that will process the args before setting them



19
20
21
22
23
24
25
# File 'lib/sshez/command.rb', line 19

def initialize(name, validator, correct_format, args_processor = nil)
  @name = name
  @validator = validator
  @args = []
  @correct_format = correct_format
  @args_processor = args_processor
end

Instance Attribute Details

#argsObject

Exposes the name and arguments



8
9
10
# File 'lib/sshez/command.rb', line 8

def args
  @args
end

#nameObject (readonly)

Exposes the name and arguments



8
9
10
# File 'lib/sshez/command.rb', line 8

def name
  @name
end

Instance Method Details

#errorObject

returns the text that should appear for a user in case of invalid input for this command



60
61
62
# File 'lib/sshez/command.rb', line 60

def error
  "Invalid input `#{args.join(',')}` for #{@name}.\nUsage: #{@correct_format}"
end

#valid?(args) ⇒ Boolean

validateds the args using the proc previously defined

Returns:

  • (Boolean)


53
54
55
# File 'lib/sshez/command.rb', line 53

def valid?(args)
  @validator.call(args)
end