Class: Cloudcost::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/cloudcost/cli.rb

Overview

Implementaion of CLI functionality

Constant Summary collapse

Error =

Error raised by this runner

Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/cloudcost/cli.rb', line 12

def self.exit_on_failure?
  true
end

Instance Method Details

#server_tagsObject



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
# File 'lib/cloudcost/cli.rb', line 65

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
        begin
          api_connection(options).set_server_tags(server.uuid, tags)
          spinner.success
        rescue Excon::Error => e
          spinner.error "ERROR: #{e.message}"
        end
      end
    end
    spinners.auto_spin
  end
rescue Cloudcost::TokenError, Cloudcost::ProfileError => e
  puts "ERROR: #{e.message}"
end

#serversObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cloudcost/cli.rb', line 36

def servers
  servers = load_servers(options)
  if options[:output] == "table"
    spinner = TTY::Spinner.new("[:spinner] Calculating costs...", clear: options[:csv])
    spinner.auto_spin
  end
  output_servers(servers, options) do |result|
    spinner&.success("(done)")
    puts result
  end
rescue Excon::Error, TokenError, ProfileError, PricingError => e
  error_message = "ERROR: #{e.message}"
  spinner ? spinner.error(error_message) : puts(error_message)
end

#versionObject



25
26
27
# File 'lib/cloudcost/cli.rb', line 25

def version
  puts "cloudcost v#{Cloudcost::VERSION}"
end

#volumesObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cloudcost/cli.rb', line 100

def volumes
  volumes = load_volumes(options)
  if options[:output] == "table"
    spinner = TTY::Spinner.new("[:spinner] Calculating costs...", clear: options[:csv])
    spinner.auto_spin
  end
  output_volumes(volumes, options) do |result|
    spinner&.success("(done)")
    puts result
  end
rescue Excon::Error, Cloudcost::TokenError, Cloudcost::ProfileError, Cloudcost::PricingError => e
  error_message = "ERROR: #{e.message}"
  spinner ? spinner.error(error_message) : puts(error_message)
end