Skip to content

Commit

Permalink
rework encrypt --add, fixes #44
Browse files Browse the repository at this point in the history
  • Loading branch information
rkh committed May 24, 2013
1 parent 4496265 commit 47ad2fc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,7 @@ If you have the old `travis-cli` gem installed, you should `gem uninstall travis
**not yet released**

* Fixed `travis disable`.
* Fix edge cases around `travis encrypt`.

**v1.2.1** (May 24, 2013)

Expand Down
56 changes: 47 additions & 9 deletions lib/travis/cli/encrypt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,7 @@ def run(*args)
encrypted = data.map { |data| repository.encrypt(data) }

if config_key
travis_config = YAML.load_file(travis_yaml)
travis_config = {} if [[], false, nil].include? travis_config
keys = config_key.split('.')
last_key = keys.pop
nested_config = keys.inject(travis_config) { |c,k| c[k] ||= {}}
nested_config = nested_config[last_key] ||= []
encrypted.each do |encrypted|
nested_config << { 'secure' => encrypted }
end
set_config encrypted.map { |e| { 'secure' => e } }
File.write(travis_yaml, travis_config.to_yaml)
else
list = encrypted.map { |data| format(data.inspect, " secure: %s") }
Expand All @@ -50,6 +42,52 @@ def run(*args)

private

def travis_config
@travis_config ||= begin
payload = YAML.load_file(travis_yaml)
payload.respond_to?(:to_hash) ? payload.to_hash : {}
end
end

def set_config(result)
parent_config[last_key] = merge_config(result)
end

def merge_config(result)
case subconfig = parent_config[last_key]
when nil then result.size == 1 ? result.first : result
when Array then subconfig + result
else result.unshift(subconfig)
end
end

def subconfig
end

def key_chain
@key_chain ||= config_key.split('.')
end

def last_key
key_chain.last
end

def parent_config
@parent_config ||= traverse_config(travis_config, *key_chain[0..-2])
end

def traverse_config(hash, key = nil, *rest)
return hash unless key

hash[key] = case value = hash[key]
when nil then {}
when Hash then value
else { 'matrix' => Array(value) }
end

traverse_config(hash[key], *rest)
end

def travis_yaml(dir = Dir.pwd)
path = File.expand_path('.travis.yml', dir)
if File.exist? path
Expand Down

0 comments on commit 47ad2fc

Please sign in to comment.