travis_dpl_test 2.0.3.beta.4.ror
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +172 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/CONTRIBUTING.md +392 -0
- data/Gemfile +32 -0
- data/Gemfile.lock +611 -0
- data/LICENSE +19 -0
- data/README.md +2744 -0
- data/Rakefile +210 -0
- data/bin/dpl +11 -0
- data/config/transliterate.yml +733 -0
- data/dpl.gemspec +23 -0
- data/lib/dpl/assets/atlas/install +19 -0
- data/lib/dpl/assets/convox/install +11 -0
- data/lib/dpl/assets/dpl/README.erb.md +138 -0
- data/lib/dpl/assets/dpl/git_ssh +8 -0
- data/lib/dpl/assets/git/detect_private_key +8 -0
- data/lib/dpl/assets/hephy/filter_log +3 -0
- data/lib/dpl/assets/pypi/install +4 -0
- data/lib/dpl/assets/scalingo/install +6 -0
- data/lib/dpl/cli.rb +100 -0
- data/lib/dpl/ctx/bash.rb +549 -0
- data/lib/dpl/ctx/test.rb +255 -0
- data/lib/dpl/ctx.rb +4 -0
- data/lib/dpl/helper/assets.rb +38 -0
- data/lib/dpl/helper/cmd.rb +169 -0
- data/lib/dpl/helper/config_file.rb +49 -0
- data/lib/dpl/helper/cookbook_site_streaming_uploader.rb +249 -0
- data/lib/dpl/helper/env.rb +92 -0
- data/lib/dpl/helper/github.rb +22 -0
- data/lib/dpl/helper/interpolate.rb +160 -0
- data/lib/dpl/helper/memoize.rb +23 -0
- data/lib/dpl/helper/squiggle.rb +24 -0
- data/lib/dpl/helper/transliterate.rb +13 -0
- data/lib/dpl/helper/wrap.rb +11 -0
- data/lib/dpl/helper/zip.rb +71 -0
- data/lib/dpl/provider/dsl.rb +410 -0
- data/lib/dpl/provider/examples.rb +132 -0
- data/lib/dpl/provider/status.rb +61 -0
- data/lib/dpl/provider.rb +651 -0
- data/lib/dpl/providers/anynines.rb +71 -0
- data/lib/dpl/providers/azure_web_apps.rb +63 -0
- data/lib/dpl/providers/bintray.rb +324 -0
- data/lib/dpl/providers/bluemixcloudfoundry.rb +98 -0
- data/lib/dpl/providers/boxfuse.rb +52 -0
- data/lib/dpl/providers/cargo.rb +32 -0
- data/lib/dpl/providers/chef_supermarket.rb +132 -0
- data/lib/dpl/providers/cloud66.rb +46 -0
- data/lib/dpl/providers/cloudfiles.rb +62 -0
- data/lib/dpl/providers/cloudformation.rb +281 -0
- data/lib/dpl/providers/cloudfoundry.rb +89 -0
- data/lib/dpl/providers/codedeploy.rb +190 -0
- data/lib/dpl/providers/convox.rb +130 -0
- data/lib/dpl/providers/datica.rb +64 -0
- data/lib/dpl/providers/ecr.rb +129 -0
- data/lib/dpl/providers/elasticbeanstalk.rb +207 -0
- data/lib/dpl/providers/engineyard.rb +113 -0
- data/lib/dpl/providers/firebase.rb +45 -0
- data/lib/dpl/providers/flynn.rb +35 -0
- data/lib/dpl/providers/gae.rb +78 -0
- data/lib/dpl/providers/gcs.rb +132 -0
- data/lib/dpl/providers/git_push.rb +273 -0
- data/lib/dpl/providers/gleis.rb +74 -0
- data/lib/dpl/providers/hackage.rb +53 -0
- data/lib/dpl/providers/hephy.rb +107 -0
- data/lib/dpl/providers/heroku/api.rb +123 -0
- data/lib/dpl/providers/heroku/git.rb +54 -0
- data/lib/dpl/providers/heroku.rb +111 -0
- data/lib/dpl/providers/lambda.rb +211 -0
- data/lib/dpl/providers/launchpad.rb +80 -0
- data/lib/dpl/providers/netlify.rb +38 -0
- data/lib/dpl/providers/npm.rb +130 -0
- data/lib/dpl/providers/nuget.rb +41 -0
- data/lib/dpl/providers/openshift.rb +52 -0
- data/lib/dpl/providers/opsworks.rb +146 -0
- data/lib/dpl/providers/packagecloud.rb_ +194 -0
- data/lib/dpl/providers/pages/api.rb +106 -0
- data/lib/dpl/providers/pages/git.rb +262 -0
- data/lib/dpl/providers/pages.rb +18 -0
- data/lib/dpl/providers/puppetforge.rb +50 -0
- data/lib/dpl/providers/pypi.rb +125 -0
- data/lib/dpl/providers/releases.rb +234 -0
- data/lib/dpl/providers/rubygems.rb +97 -0
- data/lib/dpl/providers/s3.rb +251 -0
- data/lib/dpl/providers/scalingo.rb +69 -0
- data/lib/dpl/providers/script.rb +32 -0
- data/lib/dpl/providers/snap.rb +68 -0
- data/lib/dpl/providers/surge.rb +59 -0
- data/lib/dpl/providers/testfairy.rb +101 -0
- data/lib/dpl/providers/transifex.rb +72 -0
- data/lib/dpl/providers.rb +48 -0
- data/lib/dpl/string_ext.rb +23 -0
- data/lib/dpl/support/aws_sdk_patch.rb +26 -0
- data/lib/dpl/support/gems.rb +73 -0
- data/lib/dpl/support/gstore_patch.rb +8 -0
- data/lib/dpl/support/version.rb +84 -0
- data/lib/dpl/version.rb +5 -0
- data/lib/dpl.rb +23 -0
- data/status.json +237 -0
- metadata +161 -0
data/lib/dpl/ctx/test.rb
ADDED
@@ -0,0 +1,255 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'cl'
|
4
|
+
require 'stringio'
|
5
|
+
require 'dpl/helper/squiggle'
|
6
|
+
|
7
|
+
module Dpl
|
8
|
+
module Ctx
|
9
|
+
class Test < Cl::Ctx
|
10
|
+
include Squiggle
|
11
|
+
|
12
|
+
attr_reader :cmds, :stdout, :stderr, :last_out, :last_err
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
@cmds = []
|
16
|
+
@stderr = StringIO.new
|
17
|
+
@stdout = {}
|
18
|
+
super('dpl')
|
19
|
+
end
|
20
|
+
|
21
|
+
def fold(name)
|
22
|
+
cmds << "[fold] #{name}"
|
23
|
+
yield.tap { cmds << "[unfold] #{name}" }
|
24
|
+
end
|
25
|
+
|
26
|
+
def validate_runtimes(runtimes)
|
27
|
+
runtimes.each do |name, requirements|
|
28
|
+
cmds << "[validate:runtime] #{name} (#{requirements.join(', ')})"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def apts_get(apts)
|
33
|
+
apts.each { |apt| apt_get(*apt) }
|
34
|
+
end
|
35
|
+
|
36
|
+
def apt_get(name, cmd = name)
|
37
|
+
cmds << "[apt:get] #{name} (#{cmd})"
|
38
|
+
end
|
39
|
+
|
40
|
+
def gems_require(gems)
|
41
|
+
gems.each { |gem| gem_require(*gem) }
|
42
|
+
end
|
43
|
+
|
44
|
+
def gem_require(name, version = nil, opts = {})
|
45
|
+
# not sure why this is needed. bundler should take care of this, but
|
46
|
+
# it does not for octokit for whatever reason
|
47
|
+
begin
|
48
|
+
require opts[:require] || name
|
49
|
+
rescue StandardError
|
50
|
+
nil
|
51
|
+
end
|
52
|
+
cmds << "[gem:require] #{name} (#{version}, #{opts})"
|
53
|
+
end
|
54
|
+
|
55
|
+
def npm_install(name, cmd = name)
|
56
|
+
cmds << "[npm:install] #{name} (#{cmd})"
|
57
|
+
end
|
58
|
+
|
59
|
+
def pip_install(name, cmd = name, version = nil)
|
60
|
+
cmds << "[pip:install] #{name} (#{cmd}, #{version})"
|
61
|
+
end
|
62
|
+
|
63
|
+
def ssh_keygen(_name, file)
|
64
|
+
File.open(file, 'w+') { |f| f.write('private-key') }
|
65
|
+
File.open("#{file}.pub", 'w+') { |f| f.write('ssh-rsa public-key') }
|
66
|
+
end
|
67
|
+
|
68
|
+
def shell(cmd, _opts = {})
|
69
|
+
info cmd.msg if cmd.msg?
|
70
|
+
info cmd.echo if cmd.echo?
|
71
|
+
cmds << cmd.cmd
|
72
|
+
return stdout[cmd.key] if stdout.key?(cmd.key)
|
73
|
+
|
74
|
+
cmd.capture? ? 'captured_stdout' : true
|
75
|
+
end
|
76
|
+
|
77
|
+
def success?
|
78
|
+
true
|
79
|
+
end
|
80
|
+
|
81
|
+
def info(msg)
|
82
|
+
cmds << "[info] #{msg}"
|
83
|
+
end
|
84
|
+
|
85
|
+
def print(chars)
|
86
|
+
cmds << "[print] #{chars}"
|
87
|
+
end
|
88
|
+
|
89
|
+
def warn(msg)
|
90
|
+
cmds << "[warn] #{msg}"
|
91
|
+
end
|
92
|
+
|
93
|
+
def error(message)
|
94
|
+
raise Error, message
|
95
|
+
end
|
96
|
+
|
97
|
+
def deprecate_opt(key, msg)
|
98
|
+
msg = "please use #{msg}" if msg.is_a?(Symbol)
|
99
|
+
warn "Deprecated option #{key} used (#{msg})."
|
100
|
+
end
|
101
|
+
|
102
|
+
def repo_name
|
103
|
+
'dpl'
|
104
|
+
end
|
105
|
+
|
106
|
+
def repo_slug
|
107
|
+
'travis-ci/dpl'
|
108
|
+
end
|
109
|
+
|
110
|
+
def build_dir
|
111
|
+
'.'
|
112
|
+
end
|
113
|
+
|
114
|
+
def build_number
|
115
|
+
1
|
116
|
+
end
|
117
|
+
|
118
|
+
def git_branch
|
119
|
+
'git branch'
|
120
|
+
end
|
121
|
+
|
122
|
+
def git_commit_msg
|
123
|
+
'commit msg'
|
124
|
+
end
|
125
|
+
|
126
|
+
def git_author_name
|
127
|
+
'author name'
|
128
|
+
end
|
129
|
+
|
130
|
+
def git_author_email
|
131
|
+
'author email'
|
132
|
+
end
|
133
|
+
|
134
|
+
def git_dirty?
|
135
|
+
true
|
136
|
+
end
|
137
|
+
|
138
|
+
def git_log(_args)
|
139
|
+
'commits'
|
140
|
+
end
|
141
|
+
|
142
|
+
def git_ls_files
|
143
|
+
%w[one two]
|
144
|
+
end
|
145
|
+
|
146
|
+
def git_ls_remote?(_url, _ref)
|
147
|
+
true
|
148
|
+
end
|
149
|
+
|
150
|
+
def git_remote_urls
|
151
|
+
['git://origin.git']
|
152
|
+
end
|
153
|
+
|
154
|
+
def git_rev_parse(ref)
|
155
|
+
"ref: #{ref}"
|
156
|
+
end
|
157
|
+
|
158
|
+
def git_tag
|
159
|
+
'tag'
|
160
|
+
end
|
161
|
+
|
162
|
+
def git_sha
|
163
|
+
'sha'
|
164
|
+
end
|
165
|
+
|
166
|
+
def machine_name
|
167
|
+
'machine_name'
|
168
|
+
end
|
169
|
+
|
170
|
+
def node_version
|
171
|
+
'11.0.0'
|
172
|
+
end
|
173
|
+
|
174
|
+
def npm_version
|
175
|
+
'1'
|
176
|
+
end
|
177
|
+
|
178
|
+
def which(_cmd)
|
179
|
+
false
|
180
|
+
end
|
181
|
+
|
182
|
+
def tmp_dir
|
183
|
+
FileUtils.mkdir_p('tmp')
|
184
|
+
'tmp'
|
185
|
+
end
|
186
|
+
|
187
|
+
def sleep(*); end
|
188
|
+
|
189
|
+
def encoding(_path)
|
190
|
+
'text'
|
191
|
+
end
|
192
|
+
|
193
|
+
def logger(level = :info)
|
194
|
+
logger = Logger.new(stderr)
|
195
|
+
logger.level = Logger.const_get(level.to_s.upcase)
|
196
|
+
logger
|
197
|
+
end
|
198
|
+
|
199
|
+
def test?
|
200
|
+
true
|
201
|
+
end
|
202
|
+
|
203
|
+
def file_size(path)
|
204
|
+
File.size(path.sub(File.expand_path('~').to_s, './home'))
|
205
|
+
end
|
206
|
+
|
207
|
+
def move_files(paths)
|
208
|
+
paths.each do |path|
|
209
|
+
mv(path, "/tmp/#{File.basename(path)}")
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
def unmove_files(paths)
|
214
|
+
paths.each do |path|
|
215
|
+
mv("/tmp/#{File.basename(path)}", path)
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
def mv(src, dest)
|
220
|
+
cmds << [:mv, src, dest].join(' ')
|
221
|
+
end
|
222
|
+
|
223
|
+
def rm_rf(path)
|
224
|
+
cmds << [:rm_rf, path].join(' ')
|
225
|
+
end
|
226
|
+
|
227
|
+
def chmod(perm, path)
|
228
|
+
cmds << [:chmod, perm, path].join(' ')
|
229
|
+
end
|
230
|
+
|
231
|
+
def write_file(path, content, _chmod = nil)
|
232
|
+
path = File.expand_path(path)
|
233
|
+
path = path.sub(File.expand_path('~').to_s, './home')
|
234
|
+
FileUtils.mkdir_p(File.dirname(path))
|
235
|
+
File.open(path, 'w+') { |f| f.write(content) }
|
236
|
+
end
|
237
|
+
|
238
|
+
def write_netrc(machine, login, password)
|
239
|
+
write_file('~/.netrc', sq(<<-RC))
|
240
|
+
machine #{machine}
|
241
|
+
login #{login}
|
242
|
+
password #{password}
|
243
|
+
RC
|
244
|
+
end
|
245
|
+
|
246
|
+
def tty?
|
247
|
+
false
|
248
|
+
end
|
249
|
+
|
250
|
+
def except(hash, *keys)
|
251
|
+
hash.reject { |key, _| keys.include?(key) }
|
252
|
+
end
|
253
|
+
end
|
254
|
+
end
|
255
|
+
end
|
data/lib/dpl/ctx.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'fileutils'
|
4
|
+
|
5
|
+
module Dpl
|
6
|
+
module Assets
|
7
|
+
class Asset < Struct.new(:provider, :namespace, :name)
|
8
|
+
include FileUtils
|
9
|
+
|
10
|
+
DIR = File.expand_path('../assets', __dir__)
|
11
|
+
|
12
|
+
def copy(target)
|
13
|
+
cp path, File.expand_path(target)
|
14
|
+
end
|
15
|
+
|
16
|
+
def read
|
17
|
+
exists? ? provider.interpolate(File.read(path)) : unknown
|
18
|
+
end
|
19
|
+
|
20
|
+
def exists?
|
21
|
+
File.exist?(path)
|
22
|
+
end
|
23
|
+
|
24
|
+
def unknown
|
25
|
+
raise "Could not find asset #{path}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def path
|
29
|
+
"#{DIR}/#{namespace}/#{name}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def asset(*args)
|
34
|
+
name, namespace = args.reverse
|
35
|
+
Asset.new(self, namespace || registry_key, name)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,169 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Dpl
|
4
|
+
# Represents a shell command
|
5
|
+
class Cmd < Struct.new(:provider, :key, :opts)
|
6
|
+
# Returns the shell command string
|
7
|
+
#
|
8
|
+
# If a Symbol was passed as a command then the command will be looked
|
9
|
+
# up from the provider class' `cmds` declaration.
|
10
|
+
#
|
11
|
+
# The command will be interpolated, and can include secrets. See
|
12
|
+
# `Dpl::Interpolate` for details on interpolating variables.
|
13
|
+
#
|
14
|
+
# If the option `silence` was passed then stdout and stderr will be
|
15
|
+
# redirected to `/dev/null`.
|
16
|
+
#
|
17
|
+
# If the option `python` was passed then the virtualenv with the given
|
18
|
+
# Python version will be activated before executing the command.
|
19
|
+
def cmd(secure = true)
|
20
|
+
cmd = lookup(:cmd, key) || missing(:cmd, key)
|
21
|
+
cmd = interpolate(cmd, opts, secure: secure).strip
|
22
|
+
cmd = silence(cmd) if silence?
|
23
|
+
cmd = python(cmd) if python?
|
24
|
+
cmd
|
25
|
+
end
|
26
|
+
|
27
|
+
# Returns a log message version of the command string
|
28
|
+
#
|
29
|
+
# This is the same as #cmd, except that included secrets will be
|
30
|
+
# obfuscated, and a prompt (dollar and space) will be prepended. See
|
31
|
+
# `Dpl::Interpolate` for details on interpolating variables.
|
32
|
+
def echo
|
33
|
+
"$ #{cmd(false)}"
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns the log message for announcing a command
|
37
|
+
#
|
38
|
+
# If the option `msg` was given as a String then it will be used. If the
|
39
|
+
# option `msg` was given as a Symbol then it will be looked up from the
|
40
|
+
# provider class' `msgs` declaration.
|
41
|
+
#
|
42
|
+
# The message string will be interpolated, but included secrets will be
|
43
|
+
# obfuscated. See `Dpl::Interpolate` for details on interpolating
|
44
|
+
# variables.
|
45
|
+
def msg
|
46
|
+
msg = lookup(:msg, opts[:msg], key.is_a?(Symbol) ? key : nil)
|
47
|
+
msg || missing(:msg, opts[:msg], key)
|
48
|
+
msg = interpolate(msg, opts, secure: false).strip
|
49
|
+
msg
|
50
|
+
end
|
51
|
+
|
52
|
+
def msg?
|
53
|
+
!!lookup(:msg, opts[:msg], key.is_a?(Symbol) ? key : nil)
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns the log message for a failed command
|
57
|
+
#
|
58
|
+
# The message string will be interpolated, but included secrets will be
|
59
|
+
# obfuscated. See `Dpl::Interpolate` for details on interpolating
|
60
|
+
# variables.
|
61
|
+
#
|
62
|
+
# If the option `assert` was given as a String then it will be used. If the
|
63
|
+
# option `assert` was given as a Symbol then it will be looked up from the
|
64
|
+
# provider class' `errs` declaration. If the command was given as a Symbol,
|
65
|
+
# and it can be found in `errs` then this String will be used.
|
66
|
+
def error
|
67
|
+
keys = [opts[:assert], key]
|
68
|
+
err = lookup(:err, *keys)
|
69
|
+
err ? interpolate(err, opts).strip : 'Failed'
|
70
|
+
end
|
71
|
+
|
72
|
+
# Whether or not to assert that the command has exited with 0
|
73
|
+
#
|
74
|
+
# Returns `true` if the option `assert` was given as `true` or not given at
|
75
|
+
# all. Returns `false` if the option `assert` was given as `false`.
|
76
|
+
def assert?
|
77
|
+
!opts[:assert].is_a?(FalseClass)
|
78
|
+
end
|
79
|
+
|
80
|
+
# Whether or not to announce the command with an info level log message
|
81
|
+
#
|
82
|
+
# Returns `true` if the option `assert` was given as `true` or not given at
|
83
|
+
# all. Returns `false` if the option `assert` was given as `false`.
|
84
|
+
def echo?
|
85
|
+
!opts[:echo].is_a?(FalseClass)
|
86
|
+
end
|
87
|
+
|
88
|
+
# Whether or not to capture the commands stdout and stderr
|
89
|
+
#
|
90
|
+
# Returns `true` if the option `capture` was given as `true`. Returns
|
91
|
+
# `false` if the option `capture` was given as `false` or not given.
|
92
|
+
def capture?
|
93
|
+
!!opts[:capture]
|
94
|
+
end
|
95
|
+
|
96
|
+
# Whether or not to output a log message before the command is run
|
97
|
+
#
|
98
|
+
# Returns `true` if the option `info` was given. Returns `false` if the
|
99
|
+
# option `info` was given as `false` or not given.
|
100
|
+
def info?
|
101
|
+
!!opts[:info]
|
102
|
+
end
|
103
|
+
|
104
|
+
# Returns the log message to output after the command has succeeded
|
105
|
+
def info
|
106
|
+
opts[:info]
|
107
|
+
end
|
108
|
+
|
109
|
+
# Whether or not to output a log message after the command has succeeded
|
110
|
+
#
|
111
|
+
# Returns `true` if the option `success` was given. Returns `false` if the
|
112
|
+
# option `success` was given as `false` or not given.
|
113
|
+
def success?
|
114
|
+
!!opts[:success]
|
115
|
+
end
|
116
|
+
|
117
|
+
# Returns the log message to output after the command has succeeded
|
118
|
+
def success
|
119
|
+
opts[:success]
|
120
|
+
end
|
121
|
+
|
122
|
+
# Whether or not to activate a Python virtualenv before executing the
|
123
|
+
# command
|
124
|
+
#
|
125
|
+
# Returns `true` if the option `python` was given. Returns `false` if the
|
126
|
+
# option `python` was given as `false` or not given.
|
127
|
+
def python?
|
128
|
+
!!opts[:python]
|
129
|
+
end
|
130
|
+
|
131
|
+
# Returns how often to retry the command
|
132
|
+
def retry
|
133
|
+
opts[:retry]
|
134
|
+
end
|
135
|
+
|
136
|
+
# Whether or not to redirect the command's stdout and stderr to `/dev/null`
|
137
|
+
#
|
138
|
+
# Returns `true` if the option `silence` was given. Returns `false` if the
|
139
|
+
# option `silence` was given as `false` or not given.
|
140
|
+
def silence?
|
141
|
+
!!opts[:silence]
|
142
|
+
end
|
143
|
+
|
144
|
+
private
|
145
|
+
|
146
|
+
def lookup(type, *keys)
|
147
|
+
str = provider.send(type, *keys) if provider
|
148
|
+
str || keys.detect { |key| key.is_a?(String) }
|
149
|
+
end
|
150
|
+
|
151
|
+
def missing(type, *keys)
|
152
|
+
raise("Could not find #{type}: #{keys.compact.map(&:inspect).join(', ')}")
|
153
|
+
end
|
154
|
+
|
155
|
+
def interpolate(str, args, opts = {})
|
156
|
+
provider ? provider.interpolate(str, args, opts) : str
|
157
|
+
end
|
158
|
+
|
159
|
+
def silence(str)
|
160
|
+
"#{str} > /dev/null 2>&1"
|
161
|
+
end
|
162
|
+
|
163
|
+
# Activates the Python virtualenv for the given Python version.
|
164
|
+
def python(cmd)
|
165
|
+
# "bash -c 'source $HOME/virtualenv/python#{opts[:python]}/bin/activate; #{cmd.gsub(/'/, "'\\\\''")}'"
|
166
|
+
"source $HOME/virtualenv/python#{opts[:python]}/bin/activate && #{cmd}"
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Dpl
|
4
|
+
module ConfigFile
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
# should this sit in Cl?
|
10
|
+
module ClassMethods
|
11
|
+
attr_reader :config_files
|
12
|
+
|
13
|
+
def config(*paths)
|
14
|
+
if paths.any?
|
15
|
+
@config_files = paths
|
16
|
+
elsif config_files
|
17
|
+
paths = config_files.dup
|
18
|
+
opts = paths.last.is_a?(Hash) ? paths.pop : {}
|
19
|
+
conf = ConfigFiles.new(paths, opts).config
|
20
|
+
known = self.opts.map(&:name).map(&:to_sym)
|
21
|
+
conf.select { |key, _| known.include?(key) }
|
22
|
+
else
|
23
|
+
{}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class ConfigFiles < Struct.new(:paths, :opts)
|
29
|
+
def config
|
30
|
+
paths.map { |path| parse(path) }.inject(&:merge) || {}
|
31
|
+
end
|
32
|
+
|
33
|
+
def parse(path)
|
34
|
+
str = File.exist?(path) ? File.read(path) : ''
|
35
|
+
opts = str.lines.select { |line| line.include?('=') }.map(&:strip)
|
36
|
+
opts = opts.map { |pair| pair.split('=', 2) }.to_h
|
37
|
+
opts.transform_keys { |key| strip_prefix(key).to_sym }
|
38
|
+
end
|
39
|
+
|
40
|
+
def strip_prefix(str)
|
41
|
+
opts[:prefix] ? str.sub(/^#{opts[:prefix]}[-_]?/, '') : str
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def opts
|
46
|
+
@opts ||= self.class.config.merge(super)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|