vars 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/README.md +24 -8
- data/lib/vars.rb +10 -12
- data/lib/vars/options.rb +62 -24
- data/lib/vars/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c043f196c7b6650b45d72e294f02133a91ac313950d074fd2a18c02337af6b25
|
4
|
+
data.tar.gz: fea0ae3e896547a98bfa66392de289c596b4984fcce70c0a7175f8ef70245804
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dc978f5dc125086628c3a0004154a1277aa3edd8ae6025f82235240aab4956506795b65e3979104938efa6acb4ac910b8c1c8581a85737da5ae983cb536fd747
|
7
|
+
data.tar.gz: 56f4d2eae98b8495ece7e439f752e29b017efc1fed0b3a4565f1ab753a6673f4dd970d92b28dc9b919000b3562b575da43b39c3f95c8e788798b4f141b87bcee
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -1,28 +1,44 @@
|
|
1
1
|
# Vars
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
`vars` is provide configuration each environments.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
9
7
|
Add this line to your application's Gemfile:
|
10
8
|
|
11
9
|
```ruby
|
12
|
-
gem
|
10
|
+
gem "vars"
|
13
11
|
```
|
14
12
|
|
15
13
|
And then execute:
|
16
14
|
|
17
15
|
$ bundle
|
18
16
|
|
19
|
-
|
17
|
+
## Usage
|
20
18
|
|
21
|
-
|
19
|
+
Load configuretion and resolve templates.
|
22
20
|
|
23
|
-
|
21
|
+
```ruby
|
22
|
+
vars = Vars.new(path: "path/to/environment.yml", name: "production")
|
23
|
+
vars.rails_env # => "production"
|
24
|
+
vars.app_root # => "/var/www/app/current"
|
24
25
|
|
25
|
-
|
26
|
+
vars.resolve_template("path/to/template.erb", "path/to/dest_file") # Create file from template.
|
27
|
+
vars.resolve_templates("config/deploy/templates", "config")
|
28
|
+
```
|
29
|
+
|
30
|
+
Example configuration file.
|
31
|
+
|
32
|
+
```yaml
|
33
|
+
default:
|
34
|
+
rails_env: development
|
35
|
+
app_root: /var/www/app/current
|
36
|
+
db_host: localhost
|
37
|
+
|
38
|
+
production:
|
39
|
+
rails_env: production
|
40
|
+
db_host: app-db-01.cluster.ap-northeast-1.rds.amazonaws.com
|
41
|
+
```
|
26
42
|
|
27
43
|
## Development
|
28
44
|
|
data/lib/vars.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "erb"
|
2
|
+
require "forwardable"
|
2
3
|
require "open3"
|
3
4
|
require "pathname"
|
4
5
|
require "yaml"
|
@@ -7,15 +8,17 @@ require "vars/options"
|
|
7
8
|
require "vars/version"
|
8
9
|
|
9
10
|
class Vars < BasicObject
|
10
|
-
|
11
|
+
extend ::Forwardable
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
attr_reader :options
|
14
|
+
def_delegators :options, :hash
|
15
|
+
|
16
|
+
def initialize(opts = {})
|
17
|
+
@options = opts.is_a?(Options) ? opts : Options.new(opts)
|
15
18
|
end
|
16
19
|
|
17
20
|
def [](key)
|
18
|
-
hash
|
21
|
+
hash[key.to_s]
|
19
22
|
end
|
20
23
|
|
21
24
|
def resolve_templates(template_path, output_path, excludes: [])
|
@@ -30,26 +33,21 @@ class Vars < BasicObject
|
|
30
33
|
|
31
34
|
create_file(
|
32
35
|
template_file,
|
33
|
-
output_path.join(template_file.dirname.join(filename).relative_path_from(template_path))
|
36
|
+
output_path.join(template_file.dirname.join(filename).relative_path_from(template_path))
|
34
37
|
)
|
35
38
|
end
|
36
39
|
end
|
37
40
|
|
38
|
-
def
|
41
|
+
def resolve_template(template_file, output_file)
|
39
42
|
::File.open(output_file, "w") do |f|
|
40
43
|
f.write(::ERB.new(::File.read(template_file), nil, "-").result(__binding__))
|
41
44
|
end
|
42
45
|
end
|
43
46
|
|
44
|
-
def hash
|
45
|
-
@hash ||= options.load_source(path)
|
46
|
-
end
|
47
|
-
|
48
47
|
private
|
49
48
|
|
50
49
|
def method_missing(name, *args, &block)
|
51
50
|
super unless hash.key?(name.to_s)
|
52
|
-
|
53
51
|
hash.fetch(name.to_s)
|
54
52
|
end
|
55
53
|
|
data/lib/vars/options.rb
CHANGED
@@ -2,58 +2,96 @@ class Vars < BasicObject
|
|
2
2
|
class Options
|
3
3
|
attr_reader :opts
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
module Defaults
|
6
|
+
BRANCH = "master".freeze
|
7
|
+
ENV_NAME = "development".freeze
|
8
|
+
SOURCE_PATH = "config/vars.yml".freeze
|
9
|
+
SOURCE_TYPE = :path
|
10
|
+
end
|
11
|
+
|
12
|
+
module EnvKeys
|
13
|
+
BRANCH = "TARGET_BRANCH".freeze
|
14
|
+
ENV_NAME = "APP_ENV".freeze
|
15
|
+
end
|
7
16
|
|
8
17
|
def initialize(opts = {})
|
9
18
|
@opts = opts.transform_keys(&:to_sym)
|
10
19
|
end
|
11
20
|
|
21
|
+
def hash(reload = false)
|
22
|
+
@hash = nil if reload
|
23
|
+
@hash ||= load_source
|
24
|
+
end
|
25
|
+
|
26
|
+
def load_source
|
27
|
+
src = YAML.safe_load(ERB.new(raw_source, nil, "-").result(BasicObject.new.__binding__), [], [], true)
|
28
|
+
src.fetch("default", {}).merge(src.fetch(name.to_s))
|
29
|
+
end
|
30
|
+
|
12
31
|
def name
|
13
|
-
opts.fetch(:name, ENV.fetch(
|
32
|
+
opts.fetch(:name, ENV.fetch(EnvKeys::ENV_NAME, Defaults::ENV_NAME))
|
14
33
|
end
|
15
34
|
|
16
35
|
def repo_path
|
17
|
-
|
18
|
-
|
19
|
-
|
36
|
+
case
|
37
|
+
when opts.key?(:repo_path)
|
38
|
+
opts.fetch(:repo_path)
|
39
|
+
when in_repository?
|
40
|
+
opts[:repo_path] = capture("git rev-parse --show-toplevel")
|
41
|
+
else
|
42
|
+
nil
|
43
|
+
end
|
20
44
|
end
|
21
45
|
|
22
46
|
def branch
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
47
|
+
case
|
48
|
+
when opts.key?(:branch)
|
49
|
+
opts.fetch(:branch)
|
50
|
+
when ENV.key?(EnvKeys::BRANCH)
|
51
|
+
opts[:branch] = ENV.fetch(EnvKeys::BRANCH)
|
52
|
+
when in_repository?
|
53
|
+
opts[:branch] = capture("git symbolic-ref --short HEAD")
|
54
|
+
else
|
55
|
+
Defaults::BRANCH
|
56
|
+
end
|
27
57
|
end
|
28
58
|
|
29
59
|
def source_type
|
30
|
-
opts.fetch(:source_type,
|
31
|
-
end
|
32
|
-
|
33
|
-
def load_source(path)
|
34
|
-
src = YAML.safe_load(ERB.new(raw_source(path), nil, "-").result(binding), [], [], true)
|
35
|
-
src.fetch("default", {}).merge(src.fetch(name.to_s))
|
60
|
+
opts.fetch(:source_type, Defaults::SOURCE_TYPE)
|
36
61
|
end
|
37
62
|
|
38
63
|
def in_repository?
|
39
|
-
|
40
|
-
|
41
|
-
_, _, status = Open3.capture3("git rev-parse --git-dir")
|
42
|
-
@in_repository = status.exitstatus.zero?
|
64
|
+
opts[:in_repository] = success?("git rev-parse --git-dir") unless opts.key?(:in_repository)
|
65
|
+
opts.fetch(:in_repository)
|
43
66
|
end
|
44
67
|
|
45
68
|
private
|
46
69
|
|
47
|
-
def raw_source
|
70
|
+
def raw_source
|
71
|
+
path = Pathname.new(opts.fetch(:path, Defaults::SOURCE_PATH))
|
72
|
+
raise "file not found: #{path}" unless path.exist?
|
73
|
+
|
48
74
|
case source_type
|
49
75
|
when :path
|
50
76
|
File.read(path)
|
51
77
|
when :git
|
52
|
-
|
53
|
-
Dir.chdir(repo_path) { `git show #{branch}:#{path}` }
|
78
|
+
Dir.chdir(repo_path) { capture("git show #{branch}:#{path}") }
|
54
79
|
else
|
55
|
-
raise "
|
80
|
+
raise "unknown source_type: #{source_type}"
|
56
81
|
end
|
57
82
|
end
|
83
|
+
|
84
|
+
def capture(cmd)
|
85
|
+
execute(cmd).first.chomp
|
86
|
+
end
|
87
|
+
|
88
|
+
def success?(cmd)
|
89
|
+
_, _, status = execute(cmd)
|
90
|
+
status.exitstatus.zero?
|
91
|
+
end
|
92
|
+
|
93
|
+
def execute(cmd)
|
94
|
+
Open3.capture3(cmd)
|
95
|
+
end
|
58
96
|
end
|
59
97
|
end
|
data/lib/vars/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vars
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- i2bskn
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-08-
|
11
|
+
date: 2018-08-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|