Class: Tddium::ApiConfig

Inherits:
Object
  • Object
show all
Includes:
TddiumConstant
Defined in:
lib/tddium/cli/config.rb

Instance Method Summary collapse

Constructor Details

#initialize(tddium_client, host, cli_options) ⇒ ApiConfig

BOTCH: should be a state object rather than entire CLI object



80
81
82
83
84
85
86
# File 'lib/tddium/cli/config.rb', line 80

def initialize(tddium_client, host, cli_options)
  @scm = Tddium::SCM.configure
  @tddium_client = tddium_client
  @config = Hash.new
  @host = host
  @cli_options = cli_options
end

Instance Method Details

#delete_suite(branch, account = nil) ⇒ Object



143
144
145
146
147
148
# File 'lib/tddium/cli/config.rb', line 143

def delete_suite(branch, =nil)
  branches = @config["branches"] || {}
  branches.delete_if do |k, v|
    v['branch'] == branch && (.nil? || v['account'] == )
  end
end

#get_api_key(options = {}) ⇒ Object



117
118
119
# File 'lib/tddium/cli/config.rb', line 117

def get_api_key(options = {})
  options.any? ? load_config(options)['api_key'] : @config['api_key']
end

#get_branch(branch, var, options = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/tddium/cli/config.rb', line 104

def get_branch(branch, var, options={})
  if options['account'].nil? && @cli_options[:account] then
    options['account'] = @cli_options[:account]
  end

  val = fetch_branch(branch, var, options)
  return val unless val.nil?

  populate_branches(branch)

  return fetch_branch(branch, var, options)
end

#load_config(options = {}) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/tddium/cli/config.rb', line 150

def load_config(options = {})
  global_config = load_config_from_file(:global)
  return global_config if options[:global]

  repo_config = load_config_from_file
  return repo_config if options[:repo]

  @config = global_config.merge(repo_config)
end

#logoutObject



93
94
95
# File 'lib/tddium/cli/config.rb', line 93

def logout
  remove_tddium_files
end

#populate_branches(branch) ⇒ Object



97
98
99
100
101
102
# File 'lib/tddium/cli/config.rb', line 97

def populate_branches(branch)
  suites = @tddium_api.get_suites(:repo_url => @scm.origin_url, :branch=>branch)
  suites.each do |ste|
    set_suite(ste)
  end
end

#scm_ready_sleepObject



125
126
127
128
# File 'lib/tddium/cli/config.rb', line 125

def scm_ready_sleep
  s = ENV["TDDIUM_SCM_READY_SLEEP"] || Default::SCM_READY_SLEEP
  s.to_f
end

#set_api(tddium_api) ⇒ Object

BOTCH: fugly



89
90
91
# File 'lib/tddium/cli/config.rb', line 89

def set_api(tddium_api)
  @tddium_api = tddium_api
end

#set_api_key(api_key, user) ⇒ Object



121
122
123
# File 'lib/tddium/cli/config.rb', line 121

def set_api_key(api_key, user)
  @config['api_key'] = api_key
end

#set_suite(suite) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/tddium/cli/config.rb', line 130

def set_suite(suite)
  id = suite['id']
  branch = suite['branch']
  return if id.nil? || branch.nil? || branch.empty?

  keys = %w(id branch account repo_id ci_ssh_pubkey)
   = keys.inject({}) { |h, v| h[v] = suite[v]; h }

  branches = @config["branches"] || {}
  branches.merge!({id => })
  @config.merge!({"branches" => branches})
end

#tddium_deploy_key_file_nameObject



212
213
214
# File 'lib/tddium/cli/config.rb', line 212

def tddium_deploy_key_file_name
  return tddium_file_name(:repo, '-deploy-key')
end

#tddium_file_name(scope = :repo, kind = '', root = nil) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/tddium/cli/config.rb', line 198

def tddium_file_name(scope=:repo, kind='', root=nil)
  ext = (@host == 'api.tddium.com' || @host == 'ci.solanolabs.com') ? '' : ".#{@host}"

  case scope
  when :repo
    root ||= @scm.repo? ? @scm.root : Dir.pwd

  when :global
    root = ENV['HOME']
  end

  return File.join(root, ".tddium#{kind}#{ext}")
end

#write_configObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/tddium/cli/config.rb', line 160

def write_config
  path = tddium_file_name(:global)
  File.open(path, File::CREAT|File::TRUNC|File::RDWR, 0600) do |file|
    config = Hash.new
    config['api_key'] = @config['api_key'] if @config.member?('api_key')
    file.write(config.to_json)
  end

  path = tddium_file_name(:repo)
  File.open(path, File::CREAT|File::TRUNC|File::RDWR, 0600) do |file|
    file.write(@config.to_json)
  end

  if @scm.repo? then
    branch = @scm.current_branch
    id = get_branch(branch, 'id', {})
    suite = @config['branches'][id] rescue nil

    if suite then
      path = tddium_deploy_key_file_name
      File.open(path, File::CREAT|File::TRUNC|File::RDWR, 0644) do |file|
        file.write(suite["ci_ssh_pubkey"])
      end
    end
    write_scm_ignore		# BOTCH: no need to write every time
  end
end

#write_scm_ignoreObject



188
189
190
191
192
193
194
195
196
# File 'lib/tddium/cli/config.rb', line 188

def write_scm_ignore
  path = @scm.ignore_path
  content = File.exists?(path) ? File.read(path) : ''
  unless content.include?(".tddium*\n")
    File.open(path, File::CREAT|File::APPEND|File::RDWR, 0644) do |file|
      file.write(".tddium*\n")
    end
  end
end