Module: Cf::Config

Included in:
CLI, Form, FormPreview, Line, Newform, Newline, Production
Defined in:
lib/cf/cli/config.rb

Overview

:nodoc: all

Instance Method Summary collapse

Instance Method Details

#config_fileObject



3
4
5
# File 'lib/cf/cli/config.rb', line 3

def config_file
  File.join(find_home, '.cf_credentials')
end

#find_homeObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/cf/cli/config.rb', line 90

def find_home
  unless RUBY_VERSION > '1.9' then
    ['HOME', 'USERPROFILE'].each do |homekey|
      return File.expand_path(ENV[homekey]) if ENV[homekey]
    end

    if ENV['HOMEDRIVE'] && ENV['HOMEPATH'] then
      return File.expand_path("#{ENV['HOMEDRIVE']}#{ENV['HOMEPATH']}")
    end
  end

  File.expand_path "~"
rescue
  if File::ALT_SEPARATOR then
    drive = ENV['HOMEDRIVE'] || ENV['SystemDrive']
    File.join(drive.to_s, '/')
  else
    "/"
  end
end

#get_api_key(yaml_file) ⇒ Object



43
44
45
46
# File 'lib/cf/cli/config.rb', line 43

def get_api_key(yaml_file)
  yml = YAML::load(File.read(yaml_file))
  yml[:api_key].presence || yml['api_key'].presence
end

#load_configObject



7
8
9
# File 'lib/cf/cli/config.rb', line 7

def load_config
  YAML::load(File.read(config_file)) if File.exist?(config_file)
end

#save_config(target_url) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cf/cli/config.rb', line 11

def save_config(target_url)
  if target_url == "staging"
    target_set_url = "http://sandbox.staging.cloudfactory.com/api/"
  elsif target_url == "development"
    target_set_url = "http://lvh.me:3000/api/"
  elsif target_url == "production"
    target_set_url = "http://sandbox.cloudfactory.com/api/"
  end
  File.open(config_file, 'w') {|f| f.write({ :target_url => "#{target_set_url}", :api_version => "v1" }.to_yaml) }
  return target_set_url
end

#set_api_key(yaml_source = "") ⇒ Object



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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cf/cli/config.rb', line 48

def set_api_key(yaml_source = "")
  api_key = nil
  # debugger
  api_key = get_api_key(yaml_source) if File.exists?(yaml_source)
  if api_key.blank?
    
    # api_key not found in line.yml file, so checking in the .cf_credentials
    if File.exist?(config_file)
      api_key = get_api_key(config_file)
      if api_key.blank?
        say("Error: No valid api key found. Do login first with: cf login", :red) and exit(1)
      else
        CF.api_key = api_key# if CF.api_key.blank?
        if CF::Account.valid?
          return
        else
          say("Error: Invalid api key => #{CF.api_key} for target #{CF.api_url}", :red) and exit(1)
        end
      end
    end
    say("Error: No valid api key found. Do login first with: cf login", :red) and exit(1)
  else
    CF.api_key = api_key if CF.api_key.blank?
    # Do check whether the api_key is valid by calling the CF::Account#valid?
    # Cf::CliError.new("Error: Invalid api key => #{CF.api_key}") unless CF::Account.valid?
    say("Error: Invalid api key => #{CF.api_key} for target #{CF.api_url}", :red) and exit(1) unless CF::Account.valid?
    
    # Check the file ~/.cf_credentials. If it exists, check for the api_key line. If not set, then append it.
    # This is needed for certain commands like cf line list, cf production list <line-title>
    # if File.exist?(config_file)
    #   yaml_dump = YAML::load(File.read(config_file))
    #   unless yaml_dump.keys.include?(:api_key)
    #     open(config_file, 'a') {|f| f.puts ":api_key: #{api_key}"}
    #   end
    # end
  end
  # debugger
  # puts ""
end

#set_target_uri(live) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cf/cli/config.rb', line 23

def set_target_uri(live)
  if load_config
    CF.api_url = load_config[:target_url]
    CF.api_version = load_config[:api_version]
  else
    CF.api_url = "http://sandbox.cloudfactory.com/api/"
    CF.api_version = "v1"
  end

  if live
    if CF.api_url == "http://sandbox.staging.cloudfactory.com/api/"
      CF.api_url = "http://staging.cloudfactory.com/api/"
    end
    
    if CF.api_url == "http://sandbox.cloudfactory.com/api/"
      CF.api_url = "http://cloudfactory.com/api/"
    end
  end
end