class Cloudcost::CLI

Constants

Error

Error raised by this runner

Public Class Methods

exit_on_failure?() click to toggle source
# File lib/cloudcost/cli.rb, line 11
def self.exit_on_failure?
  true
end

Public Instance Methods

api_connection(options) click to toggle source
# File lib/cloudcost/cli.rb, line 105
def api_connection(options)
  api_token = options[:api_token] || Cloudcost::ApiToken.new(options).token
  Cloudcost::ApiConnection.new(api_token, options)
end
load_servers(options) click to toggle source
# File lib/cloudcost/cli.rb, line 110
def load_servers(options)
  spinner = TTY::Spinner.new("[:spinner] Loading servers...", clear: options[:csv])
  spinner.auto_spin
  servers = api_connection(options).get_servers(options).map { |server| Server.new(server) }
  spinner.success "(#{servers.size} found)"
  servers
end
output(servers, options) { |server_list.to_csv| ... } click to toggle source
# File lib/cloudcost/cli.rb, line 118
def output(servers, options)
  if options[:output] == "csv"
    yield Cloudcost::ServerList.new(servers, options).to_csv
  else
    yield Cloudcost::ServerList.new(servers, options).cost_table
  end
end
server_tags() click to toggle source
# File lib/cloudcost/cli.rb, line 66
def server_tags
  servers = load_servers(options)
  servers.size.positive? ? puts(Cloudcost::ServerList.new(servers, options).tags_table) : exit
  if (options[:set_tags] || options[:remove_tags]) && ask(
    "Do you want to #{tag_option_to_s(options)}?",
    default: "n"
  ) == "y"
    spinners = TTY::Spinner::Multi.new("[:spinner] Settings server tags")
    servers.each do |server|
      spinners.register("[:spinner] #{server.name}") do |spinner|
        tags = server.tags.merge(options[:set_tags] ? tags_to_h(options[:set_tags]) : {})
        (options[:remove_tags] || []).each do |tag|
          tags.reject! { |k| k == tag.to_sym }
        end
        api_connection(options).set_server_tags(server.uuid, tags)
        spinner.success
      end
    end
    spinners.auto_spin
  end
rescue Excon::Error, TokenError, ProfileError => e
  error_message = "ERROR: #{e.message}"
  if defined?(spinner)
    spinner.error("(#{error_message})")
  else
    puts error_message
  end
end
servers() click to toggle source
# File lib/cloudcost/cli.rb, line 34
def servers
  servers = load_servers(options)
  spinner = TTY::Spinner.new("[:spinner] Calculating costs...", clear: options[:csv])
  spinner.auto_spin
  output(servers, options) do |result|
    spinner.success "(done)"
    puts
    puts result
  end
rescue Excon::Error, TokenError, ProfileError, PricingError => e
  error_message = "ERROR: #{e.message}"
  if spinner
    spinner.error("(#{error_message})")
  else
    puts error_message
  end
end
tag_option_to_s(options) click to toggle source
# File lib/cloudcost/cli.rb, line 126
def tag_option_to_s(options)
  messages = []
  messages << "set tags \"#{options[:set_tags].join(", ")}\"" if options[:set_tags]
  messages << "remove tags \"#{options[:remove_tags].join(", ")}\"" if options[:remove_tags]
  messages.join(" and ")
end
tags_to_h(tags_array) click to toggle source
# File lib/cloudcost/cli.rb, line 96
def tags_to_h(tags_array)
  tags_hash = {}
  tags_array.each do |tag|
    k_v = tag.split("=")
    tags_hash[k_v[0].to_sym] = k_v[1]
  end
  tags_hash
end
version() click to toggle source
# File lib/cloudcost/cli.rb, line 24
def version
  puts "cloudcost v#{Cloudcost::VERSION}"
end