Class: Glb::Lb::Args

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Util::Sh
Defined in:
lib/glb/lb/args.rb

Instance Method Summary collapse

Methods included from Util::Sh

#capture, #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 allowed_options(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

Returns:

  • (Boolean)


34
35
36
# File 'lib/glb/lb/args.rb', line 34

def invalid_command?
  @command == "url-maps update"
end

#transformObject

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?
  options = @opts.dup
  options.compact!
  options[:global] = true unless options[:region]
  allowed = allowed_options(@command)
  allowed.map! { |o| o.gsub("-", "_") }
  options = options.select { |k,v| allowed.include?(k.to_s) }

  options.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