vars 0.0.4 → 0.0.5

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: 7d4a8ae7877bcc6826a494fa4b95ea5af03c7599bd8f6d58485d19936d8d0ded
4
- data.tar.gz: d1846e487a43c1462c53239ed2e5c5fe330ffd1b42da756dd6df314a3b6f2b34
3
+ metadata.gz: f089dad07e951ae88cd1d8e9c69bf0aa25d2c3f344ddeed6a78879609d8bb69f
4
+ data.tar.gz: 57558dd28e1d9a36bf716dccab2afc708e7f6b61318492a2dd401458ff462580
5
5
  SHA512:
6
- metadata.gz: '028832d7dbe69ff29befe22a517df5e6c97d7e2242c7e2b80e95f0279948972ca96bff83521b6627052df28f6a8ce165fba96e7f63b69f85add58c8ced4da0a7'
7
- data.tar.gz: 7cf14a3ae4efb5eadcad03fce70b521c9e5ea0581a3c6822ce567384081fe7636a27aa93edb799f26fff73b46c3cd99ceaa62ed3dbf2225775b2c5b02f08391a
6
+ metadata.gz: d0055ac7e2c385f51c6ee41d3151c91c65f5db769b143198d513c55f74caecd781ef4abb60782dc78a061ca581eeff5a7d56b8fe8a619a11cbb8b22c99e5c84b
7
+ data.tar.gz: beaa4187761f12c02c2bbb54f5daf0159efd041746618d98518d56c5ba1c8052ff9c4a330dff06106700e2725a6c4f0799cf845ccaed57648f6952c78ad6b020
@@ -3,3 +3,6 @@ language: ruby
3
3
  rvm:
4
4
  - 2.5.1
5
5
  before_install: gem install bundler -v 1.16.2
6
+ script:
7
+ - bundle exec rake test
8
+ - bundle exec rubocop
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Vars
2
2
 
3
+ [![Build Status](https://travis-ci.org/i2bskn/vars.svg?branch=master)](https://travis-ci.org/i2bskn/vars)
4
+
3
5
  `vars` is provide configuration each environments.
4
6
 
5
7
  ## Installation
@@ -23,7 +25,10 @@ vars = Vars.new(path: "path/to/environment.yml", name: "production")
23
25
  vars.rails_env # => "production"
24
26
  vars.app_root # => "/var/www/app/current"
25
27
 
26
- vars.resolve_template("path/to/template.erb", "path/to/dest_file") # Create file from template.
28
+ # Create config/database.yml from template.
29
+ vars.resolve_template("config/deploy/templates/database.yml.erb", "config/database.yml")
30
+
31
+ # Create config files from templates. (config/deploy/templates/**/*)
27
32
  vars.resolve_templates("config/deploy/templates", "config")
28
33
  ```
29
34
 
@@ -35,9 +40,16 @@ default:
35
40
  app_root: /var/www/app/current
36
41
  db_host: localhost
37
42
 
43
+ development:
44
+ db_host: app-development-db-01.xxx.ap-northeast-1.rds.amazonaws.com
45
+
46
+ staging:
47
+ rails_env: staging
48
+ db_host: app-staging-db-01.xxx.ap-northeast-1.rds.amazonaws.com
49
+
38
50
  production:
39
51
  rails_env: production
40
- db_host: app-db-01.cluster.ap-northeast-1.rds.amazonaws.com
52
+ db_host: app-db-01.xxx.ap-northeast-1.rds.amazonaws.com
41
53
  ```
42
54
 
43
55
  ## Development
@@ -5,17 +5,21 @@ class Vars
5
5
  module Defaults
6
6
  BRANCH = "master".freeze
7
7
  ENV_NAME = "development".freeze
8
+ REMOTE_NAME = "origin".freeze
8
9
  SOURCE_PATH = "config/vars.yml".freeze
9
10
  SOURCE_TYPE = :path
10
11
  end
11
12
 
12
13
  module EnvKeys
13
- BRANCH = "TARGET_BRANCH".freeze
14
- ENV_NAME = "APP_ENV".freeze
14
+ BRANCH = "TARGET_BRANCH".freeze
15
+ ENV_NAME = "APP_ENV".freeze
16
+ REMOTE_NAME = "REMOTE_NAME".freeze
15
17
  end
16
18
 
17
19
  def initialize(opts = {})
18
20
  @opts = opts.transform_keys(&:to_sym)
21
+ # Use only when source_type is git.
22
+ @need_fetch = true
19
23
  end
20
24
 
21
25
  def hash(reload = false)
@@ -25,6 +29,8 @@ class Vars
25
29
 
26
30
  def load_source
27
31
  src = YAML.safe_load(ERB.new(raw_source, nil, "-").result(Class.new.__binding__), [], [], true)
32
+ return {} if src.nil?
33
+
28
34
  src.fetch("default", {}).merge(src.fetch(name.to_s))
29
35
  end
30
36
 
@@ -33,35 +39,40 @@ class Vars
33
39
  end
34
40
 
35
41
  def repo_path
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
42
+ return opts.fetch(:repo_path) if opts.key?(:repo_path)
43
+ return nil unless in_repository?
44
+
45
+ opts[:repo_path] = capture("git rev-parse --show-toplevel")
44
46
  end
45
47
 
46
48
  def branch
47
- case
48
- when opts.key?(:branch)
49
- opts.fetch(:branch)
50
- when ENV.key?(EnvKeys::BRANCH)
49
+ return opts.fetch(:branch) if opts.key?(:branch)
50
+
51
+ if ENV.key?(EnvKeys::BRANCH)
51
52
  opts[:branch] = ENV.fetch(EnvKeys::BRANCH)
52
- when in_repository?
53
+ elsif in_repository?
53
54
  opts[:branch] = capture("git symbolic-ref --short HEAD")
54
55
  else
55
56
  Defaults::BRANCH
56
57
  end
57
58
  end
58
59
 
60
+ def remote_name
61
+ ENV.fetch(EnvKeys::REMOTE_NAME, Defaults::REMOTE_NAME)
62
+ end
63
+
59
64
  def source_type
60
65
  opts.fetch(:source_type, Defaults::SOURCE_TYPE)
61
66
  end
62
67
 
63
68
  def in_repository?
64
69
  opts[:in_repository] = success?("git rev-parse --git-dir") unless opts.key?(:in_repository)
70
+ # Update remote repositories.
71
+ if opts[:in_repository] && @need_fetch
72
+ execute("git fetch #{remote_name}")
73
+ @need_fetch = false
74
+ end
75
+
65
76
  opts.fetch(:in_repository)
66
77
  end
67
78
 
@@ -75,7 +86,7 @@ class Vars
75
86
  when :path
76
87
  File.read(path)
77
88
  when :git
78
- Dir.chdir(repo_path) { capture("git show #{branch}:#{path}") }
89
+ Dir.chdir(repo_path) { capture("git show #{remote_name}/#{branch}:#{path}") }
79
90
  else
80
91
  raise "unknown source_type: #{source_type}"
81
92
  end
@@ -1,3 +1,3 @@
1
1
  class Vars
2
- VERSION = "0.0.4".freeze
2
+ VERSION = "0.0.5".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.4
4
+ version: 0.0.5
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-20 00:00:00.000000000 Z
11
+ date: 2018-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler