vars 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e6341fa4ac7c3a051106ecf7fccaa3f38d4002daaeeabe55d6c9b01ef1e6a753
4
- data.tar.gz: 3f46d488a2e31d41488ed72b87bb759ce6ac419c352a23376dd504bbfa38fd59
3
+ metadata.gz: c043f196c7b6650b45d72e294f02133a91ac313950d074fd2a18c02337af6b25
4
+ data.tar.gz: fea0ae3e896547a98bfa66392de289c596b4984fcce70c0a7175f8ef70245804
5
5
  SHA512:
6
- metadata.gz: d303cf2dcfc87c1af59db0ab2556cba4c2a7e54902434c11f11ac9a30d35555ff141d524e41cec60bd2f9619c3b87f00cf4a9f277bda73aa184d328e24dbd985
7
- data.tar.gz: a6036491814ce2b923f467b6d3d8487c7af3e137c6ae7d2b5e0ff366fc1ee60c6b1d7b2ff77f0de7f6c19e20a07e61d6bedc6e6dfb997f499c5f78f4c6f42352
6
+ metadata.gz: dc978f5dc125086628c3a0004154a1277aa3edd8ae6025f82235240aab4956506795b65e3979104938efa6acb4ac910b8c1c8581a85737da5ae983cb536fd747
7
+ data.tar.gz: 56f4d2eae98b8495ece7e439f752e29b017efc1fed0b3a4565f1ab753a6673f4dd970d92b28dc9b919000b3562b575da43b39c3f95c8e788798b4f141b87bcee
data/.rubocop.yml CHANGED
@@ -20,6 +20,9 @@ Layout/IndentationConsistency:
20
20
  Enabled: true
21
21
  EnforcedStyle: rails
22
22
 
23
+ Metrics/MethodLength:
24
+ Max: 20
25
+
23
26
  Metrics/LineLength:
24
27
  Max: 120
25
28
 
data/README.md CHANGED
@@ -1,28 +1,44 @@
1
1
  # Vars
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/vars`. To experiment with that code, run `bin/console` for an interactive prompt.
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 'vars'
10
+ gem "vars"
13
11
  ```
14
12
 
15
13
  And then execute:
16
14
 
17
15
  $ bundle
18
16
 
19
- Or install it yourself as:
17
+ ## Usage
20
18
 
21
- $ gem install vars
19
+ Load configuretion and resolve templates.
22
20
 
23
- ## Usage
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
- TODO: Write usage instructions here
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
- attr_reader :path, :options
11
+ extend ::Forwardable
11
12
 
12
- def initialize(path, opts = {})
13
- @path = path
14
- @options = Options.new(opts)
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.fetch(key.to_s, nil)
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 create_file(template_file, output_file)
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
- DEFAULT_NAME = "default".freeze
6
- DEFAULT_BRANCH = "master".freeze
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("APP_ENV", DEFAULT_NAME))
32
+ opts.fetch(:name, ENV.fetch(EnvKeys::ENV_NAME, Defaults::ENV_NAME))
14
33
  end
15
34
 
16
35
  def repo_path
17
- return opts.fetch(:repo_path) if opts.key?(:repo_path)
18
-
19
- in_repository? ? `git rev-parse --show-toplevel`.chomp : nil
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
- return opts.fetch(:branch) if opts.key?(:branch)
24
- return ENV["TARGET_BRANCH"] if ENV.key?("TARGET_BRANCH")
25
-
26
- in_repository? ? `git symbolic-ref --short HEAD`.chomp : DEFAULT_BRANCH
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, :path)
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
- return @in_repository if instance_variable_defined?(:@in_repository)
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(path)
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
- raise "repo_path is nil" if repo_path.nil?
53
- Dir.chdir(repo_path) { `git show #{branch}:#{path}` }
78
+ Dir.chdir(repo_path) { capture("git show #{branch}:#{path}") }
54
79
  else
55
- raise "Unknown source_type: #{source_type}"
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
@@ -1,3 +1,3 @@
1
1
  class Vars < BasicObject
2
- VERSION = "0.0.1".freeze
2
+ VERSION = "0.0.2".freeze
3
3
  end
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.1
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-03 00:00:00.000000000 Z
11
+ date: 2018-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler