Class: Glb::Lb::Args
- Inherits:
-
Object
- Object
- Glb::Lb::Args
- Extended by:
- Memoist
- Includes:
- Util::Sh
- Defined in:
- lib/glb/lb/args.rb
Instance Method Summary collapse
- #allowed_options(command) ⇒ Object
-
#filter_special_cases(lines) ⇒ Object
Note: Tried filtering out lines with bold text, but it didn’t work.
-
#initialize(command, opts) ⇒ Args
constructor
command: “firewall-rule create”.
- #invalid_command? ⇒ Boolean
-
#transform ⇒ Object
Converts a hash of options to a string of gcloud arguments.
Methods included from Util::Sh
Constructor Details
#initialize(command, opts) ⇒ Args
command: “firewall-rule create”
7 8 9 |
# File 'lib/glb/lb/args.rb', line 7 def initialize(command, opts) @command, @opts = command, opts end |
Instance Method Details
#allowed_options(command) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/glb/lb/args.rb', line 38 def (command) lines = capture("gcloud help compute #{command}", show_command: false).split("\n") lines = lines.grep(/--/) lines = filter_special_cases(lines) lines.map do |line| md = line.match(/--([\w-]+)/) md[1] if md end.compact.sort.uniq end |
#filter_special_cases(lines) ⇒ Object
Note: Tried filtering out lines with bold text, but it didn’t work. It introduced too many bugs.
" \e[1m--priority\e[m=\e[4mPRIORITY\e[m",
" \e[1m--rules\e[m=[\e[4mPROTOCOL\e[m[:\e[4mPORT\e[m[-\e[4mPORT\e[m]],...]",
" \e[1m--source-ranges\e[m=[\e[4mCIDR_RANGE\e[m,...]",
regexp to match bold text: /^s+.?[1m–/ (attempt)
line.match(/^\s+.?\[1m--/)
58 59 60 61 62 63 64 |
# File 'lib/glb/lb/args.rb', line 58 def filter_special_cases(lines) case @command when "backend-services update" lines = lines.reject { |line| line.include?("--load-balancing-scheme") } end lines end |
#invalid_command? ⇒ Boolean
34 35 36 |
# File 'lib/glb/lb/args.rb', line 34 def invalid_command? @command == "url-maps update" end |
#transform ⇒ Object
Converts a hash of options to a string of gcloud arguments
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/glb/lb/args.rb', line 12 def transform return "" if invalid_command? = @opts.dup .compact! [:global] = true unless [:region] allowed = (@command) allowed.map! { |o| o.gsub("-", "_") } = .select { |k,v| allowed.include?(k.to_s) } .map do |k,v| k = k.to_s.gsub("_", "-") k = "--#{k}" if v == true k # IE: --global elsif v == false "" else "#{k}=#{v}" end end.sort.join(" ").squish end |