module Linode::Ip

Method for fetching ip of linode matching a label.

Constants

VERSION

Public Instance Methods

fetch(name) click to toggle source
# File lib/linode/ip.rb, line 11
def fetch(name)
  matched_linodes = maching_linodes(linodes, name)

  return if matched_linodes.empty?

  return matched_linodes[0]['ipv4'].first if matched_linodes.count == 1

  n = select_linodes_index(matched_linodes)

  if n == 'u'
    fetch(read_matcher)
  else
    matched_linodes[n]['ipv4'].first
  end
end

Private Instance Methods

linodes() click to toggle source
# File lib/linode/ip.rb, line 29
def linodes
  @linodes ||= JSON.parse(`linode-cli --json linodes list`)
end
maching_linodes(linodes, name) click to toggle source
# File lib/linode/ip.rb, line 43
def maching_linodes(linodes, name)
  linodes
    .select { |linode| linode['label'].match?(/#{name}/) }
end
process_linodes_selection(linodes, selection) click to toggle source
# File lib/linode/ip.rb, line 62
def process_linodes_selection(linodes, selection)
  if (0...linodes.count).map(&:to_s).include?(selection)
    selection.to_i
  elsif selection == 'e'
    puts_good_bye
  elsif selection == 'u'
    selection
  else
    select_linodes_index(linodes)
  end
end
puts_good_bye() click to toggle source
# File lib/linode/ip.rb, line 74
def puts_good_bye
  puts 'Good bye!'
  exit
end
puts_which_linodes(linodes) click to toggle source
# File lib/linode/ip.rb, line 48
def puts_which_linodes(linodes)
  puts 'Multiple matching linodes found:'
  puts '-------------------------------'
  linodes.each_with_index do |linode, n|
    puts "(#{n}) #{linode['label']}"
  end

  puts
  puts '[u] Update linode matcher'
  puts '[e] exit'
  puts
  puts 'Choose a lionde or command?'
end
read_matcher() click to toggle source
# File lib/linode/ip.rb, line 38
def read_matcher
  puts 'What matcher should I use?'
  STDIN.gets.chomp
end
select_linodes_index(linodes) click to toggle source
# File lib/linode/ip.rb, line 33
def select_linodes_index(linodes)
  puts_which_linodes(linodes)
  process_linodes_selection(linodes, STDIN.gets.chomp)
end