Class: PackageCloud::CLI::MasterToken

Inherits:
Base
  • Object
show all
Defined in:
lib/package_cloud/cli/master_token.rb

Instance Method Summary collapse

Instance Method Details

#create(repo_name, token_name) ⇒ Object


27
28
29
30
31
32
33
34
35
36
# File 'lib/package_cloud/cli/master_token.rb', line 27

def create(repo_name, token_name)
  print "Looking for repository at #{repo_name}... "
  repo = client.repository(repo_name)
  print "success!\n"

  print "Attempting to create token named #{token_name}... "
  resp = repo.create_master_token(token_name)
  print "success!\n".color(:green)
  print "Master token #{resp['name']} with value #{resp['value']} created.\n"
end

#destroy(repo_name, token_name) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/package_cloud/cli/master_token.rb', line 40

def destroy(repo_name, token_name)
  ARGV.clear # otherwise gets explodes

  if token_name == "default"
    abort("You can't delete the default master_token.".color(:red))
  end

  print "Looking for repository at #{repo_name}... "
  repo = client.repository(repo_name)
  print "success!\n"

  token = repo.master_tokens.detect do |token|
    token.name == token_name
  end

  if token
    msg = "\nAre you sure you want to delete #{token_name}?"
    msg << " #{token.read_tokens.length} read tokens will no longer work afterwards (y/n)" if token.read_tokens.length > 0
    answer = get_valid(msg) do |s|
      s == "y" || s == "n"
    end
    if answer == "y"
      print "Attempting to destroy token named #{token_name}... "
      token.destroy
      print "success!\n".color(:green)
    else
      puts "Aborting...".color(:red)
    end
  else
    puts "Wasn't able to find a token named #{token_name}.".color(:red)
    exit(1)
  end
end

#list(repo_name) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/package_cloud/cli/master_token.rb', line 6

def list(repo_name)
  print "Looking for repository at #{repo_name}... "
  repo = client.repository(repo_name)
  print "success!\n"

  tokens = repo.master_tokens
  puts "Tokens for #{repo_name}:"
  puts ""
  tokens.each_with_index do |token, i|
    puts "  #{token.name} (#{token.value})"
    puts "  read tokens:"
    token.read_tokens.each do |read_token|
    puts "    { id: #{read_token.id}, name: #{read_token.name}, value: #{read_token.value} }"
    puts
    end
    puts "" unless i == tokens.length - 1
  end
end