Method: Cf::Config#set_api_key

Defined in:
lib/cf/cli/config.rb

#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::.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::.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