Method: PackageCloud::ConfigFile#read_or_create

Defined in:
lib/package_cloud/config_file.rb

#read_or_createObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/package_cloud/config_file.rb', line 16

def read_or_create
  if ENV["PACKAGECLOUD_TOKEN"]
    if ENV["PACKAGECLOUD_TOKEN"].length < 48
      puts "Found PACKAGECLOUD_TOKEN environment variable but is empty or too short! Visit https://packagecloud.io/api_token and confirm it is correct."
      exit!
    end
    @token = ENV["PACKAGECLOUD_TOKEN"]
    @url   = URI(ENV["PACKAGECLOUD_URL"]) if ENV["PACKAGECLOUD_URL"]
    output_host_and_token
  elsif File.exist?(@filename)
    attrs = JSON.parse(File.read(@filename))
    @token = attrs["token"] if attrs.has_key?("token")
    @url   = URI(attrs["url"]) if attrs.has_key?("url")
    fix_config_file!
    output_host_and_token
  else
    puts "No config file exists at #{@filename}. Login to create one."

    @token = 
    print "Got your token. Writing a config file to #{@filename}... "
    write
    puts "success!"
  end
end