Class: Kube::Ctl::CommandTree
- Inherits:
-
Object
- Object
- Kube::Ctl::CommandTree
- Includes:
- Enumerable
- Defined in:
- lib/kube/ctl/command_tree.rb
Defined Under Namespace
Classes: Evaluation
Instance Attribute Summary collapse
-
#tree ⇒ Object
readonly
Returns the value of attribute tree.
Class Method Summary collapse
Instance Method Summary collapse
- #each {|@tree_data| ... } ⇒ Object
- #evaluate(string_builder, resource_dataset: nil) ⇒ Object
-
#initialize(tree_data) ⇒ CommandTree
constructor
A new instance of CommandTree.
Constructor Details
#initialize(tree_data) ⇒ CommandTree
Returns a new instance of CommandTree.
23 24 25 |
# File 'lib/kube/ctl/command_tree.rb', line 23 def initialize(tree_data) @tree_data = tree_data end |
Instance Attribute Details
#tree ⇒ Object (readonly)
Returns the value of attribute tree.
17 18 19 |
# File 'lib/kube/ctl/command_tree.rb', line 17 def tree @tree end |
Class Method Details
.from_file(path) ⇒ Object
19 20 21 |
# File 'lib/kube/ctl/command_tree.rb', line 19 def self.from_file(path) new(JSON.parse(File.read(path))) end |
Instance Method Details
#each {|@tree_data| ... } ⇒ Object
27 28 29 |
# File 'lib/kube/ctl/command_tree.rb', line 27 def each yield @tree_data end |
#evaluate(string_builder, resource_dataset: nil) ⇒ Object
31 32 33 34 35 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 |
# File 'lib/kube/ctl/command_tree.rb', line 31 def evaluate(string_builder, resource_dataset: nil) units = extract_units(string_builder) resources = [] command_tokens = [] flags = [] errors = [] command_cursor = children_of(@tree_data) in_command_phase = true command_ended = false pending_flag_arg = false resource_set = resource_dataset ? normalize_resource_set(resource_dataset) : nil resource_parts = [] units.each do |unit| if unit[:type] == :separator resource_parts.last[:separator] = unit[:value] unless resource_parts.empty? next end token = unit[:value] args = unit[:args] rendered = unit[:rendered] || render_token(token) if in_command_phase if command_cursor.key?(token) command_tokens << token command_cursor = children_of(command_cursor[token]) next end in_command_phase = false command_ended = true if command_tokens.empty? errors << "invalid command start: `#{token}`" end end if pending_flag_arg && !token.start_with?("-") flags[-1] = "#{flags[-1]} #{rendered}" pending_flag_arg = false next end if token.start_with?("-") flags << rendered pending_flag_arg = true next end if args.any? if args.length == 1 && args[0] == true flags << "--#{render_flag(token)}" else flags << "--#{render_flag(token)} #{render_token(token, args).split(" ", 2).last}" end next end separator = resource_parts.empty? ? nil : "." resource_parts << { value: rendered, separator: separator } end if command_ended resource = serialize_resource(resource_parts) resources << resource unless resource.empty? if resource_set && !resource_set.empty? && !resource_set.include?(resource) errors << "unknown resource: `#{resource}`" end end rendered_resources = resources.map { |resource| render_resource(resource) } rendered_flags = flags.map { |flag| flag.start_with?("--output ") ? flag.sub("--output ", "-o ") : flag } rendered = [command_tokens.join(" "), rendered_resources.join(" "), rendered_flags.join(" ")].reject(&:empty?).join(" ") Evaluation.new( command_tokens, resources, flags, errors, rendered, errors.empty? ) end |