Class: Kube::Ctl::CommandTree::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/kube/ctl/command_tree/validator.rb

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Validator

Returns a new instance of Validator.



9
10
11
# File 'lib/kube/ctl/command_tree/validator.rb', line 9

def initialize(root)
  @root = root
end

Instance Method Details

#validate(input) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kube/ctl/command_tree/validator.rb', line 13

def validate(input)
  tokens = Shellwords.split(input)
  errors = []
  path   = [@root]
  node   = @root

  # 1. Descend through subcommands (handles arbitrary depth)
  while tokens.any? && (child = node.find_subcommand(tokens.first))
    node = child
    path << node
    tokens.shift
  end

  # If we never moved past root, the first token wasn't a valid command
  if path.size == 1
    errors << "Unknown command: #{tokens.first.inspect}"
    return Result.new(false, path, [], {}, errors)
  end

  # 2. Parse remaining tokens as flags + positionals
  positional, flags, parse_errors = parse_args(tokens, node)
  errors.concat(parse_errors)

  Result.new(errors.empty?, path, positional, flags, errors)
end