Module: Kontena::Plugin::Packet::ProjectOption

Included in:
Master::CreateCommand, Nodes::CreateCommand, Nodes::TerminateCommand
Defined in:
lib/kontena/plugin/packet/project_option.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kontena/plugin/packet/project_option.rb', line 4

def self.included(base)
  base.option "--project", "PROJECT_ID", "Packet project ID", environment_variable: 'PACKET_PROJECT'
  base.class_eval do
    def default_project
      require 'packet'

      client = Packet::Client.new(self.token || (self.respond_to?(:default_token) && self.default_token))

      projects = []
      spinner "Retrieving a list of available projects on Packet" do
        projects = client.list_projects
      end

      case projects.size
      when 0
        abort 'You do not have access to any projects on Packet'
      when 1
        unless Kontena.prompt.yes?("You have access to project '#{projects.first.name}'. Use?")
          abort 'Aborted'
        end
        projects.first.id
      else
        Kontena.prompt.select "Packet project:" do |menu|
          projects.each do |project|
            menu.choice project.name, project.id
          end
        end
      end
    end
  end
end