Class: Kube::Ctl::CommandTree
- Inherits:
-
Object
- Object
- Kube::Ctl::CommandTree
- Defined in:
- lib/kube/ctl/command_tree.rb,
lib/kube/ctl/command_tree/node.rb,
lib/kube/ctl/command_tree/validator.rb
Defined Under Namespace
Classes: Node, Result, Validator
Instance Method Summary collapse
- #evaluate(builder, resource_dataset: nil) ⇒ Object
-
#initialize(data) ⇒ CommandTree
constructor
A new instance of CommandTree.
Constructor Details
#initialize(data) ⇒ CommandTree
Returns a new instance of CommandTree.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/kube/ctl/command_tree.rb', line 20 def initialize(data) @root = Node.new(name: 'kubectl') data.fetch('toplevelcommandgroups', []).each do |group| group.fetch('commands', []).each do |entry| main = entry['maincommand'] next unless main node = build_node(main) (entry['subcommands'] || []).each do |sub| node.add_subcommand(build_node(sub)) end @root.add_subcommand(node) end end end |
Instance Method Details
#evaluate(builder, resource_dataset: nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/kube/ctl/command_tree.rb', line 36 def evaluate(builder, resource_dataset: nil) buffer = builder.to_a commands = [] resources = [] flags = [] errors = [] node = @root i = 0 # 1. Walk commands/subcommands while i < buffer.length entry = buffer[i] break unless entry.is_a?(Array) name, args = entry break unless args.empty? child = node.find_subcommand(name) break unless child commands << name node = child i += 1 # Consume :dash separated subcommand parts (e.g. set-last-applied) while i < buffer.length && buffer[i] == :dash next_i = i + 1 break unless next_i < buffer.length && buffer[next_i].is_a?(Array) next_name, next_args = buffer[next_i] break unless next_args.empty? hyphenated = "#{commands.last}-#{next_name}" child = node.find_subcommand(hyphenated) if child commands[-1] = hyphenated node = child i = next_i + 1 else break end end end if commands.empty? && buffer.any? first = buffer[0].is_a?(Array) ? buffer[0][0] : buffer[0].to_s errors << "invalid command start: `#{first}`" end # 2. Walk remaining buffer: classify as resources or flags current_resource = nil while i < buffer.length entry = buffer[i] case entry when :dash current_resource = "#{current_resource}-" if current_resource i += 1 when :slash current_resource = "#{current_resource}/" if current_resource i += 1 when Array name, args = entry if args.nil? || args.empty? # Bare token — resource segment if current_resource && !current_resource.end_with?("-") && !current_resource.end_with?("/") current_resource = "#{current_resource}.#{name}" elsif current_resource current_resource = "#{current_resource}#{name}" else current_resource = name end i += 1 else # Has args — it's a flag flush_resource(resources, current_resource) current_resource = nil flag_name = name.tr("_", "-") if args == [true] flags << "--#{flag_name}" else value = args.map(&:to_s).join(",") flags << "--#{flag_name} #{value}" end i += 1 end else i += 1 end end flush_resource(resources, current_resource) # 3. Validate resources against dataset if provided if resource_dataset resources.each do |r| errors << "unknown resource: `#{r}`" unless resource_dataset.include?(r) end end Result.new(commands, resources, flags, errors, errors.empty?) end |