env_setup 0.1.3 → 0.1.8

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: 78f518974541de10a07ab4ed5f9fe126ae435859d30d2de063cbd903cfa3c99b
4
- data.tar.gz: 52f4e06dc2db21cb4bcdc048e61a687ef0bc80a4a6ddf2c9eca14a95142be495
3
+ metadata.gz: 9eac51205869d1c0692b2e0f646c850ab73d501c4fb5991317f90c9a4bda8841
4
+ data.tar.gz: 25367896b58d2a8b2f173dd7b6f1a2b044b29c1329e00b1e6b64b45475f36ef1
5
5
  SHA512:
6
- metadata.gz: ecfb474839b17b793687e44ad35a4b8a74d8977eafa8f4a8c73b2fc7dfe394d19aa21c54a4255046686e0c2972281b50393776defcc69541dc3718575593662d
7
- data.tar.gz: dcb26f5c5894812693f575e9dfbaaf1c4b3317d9cf94f9b5a3fff202e4ff4850913b82341f847ff4058ebef7c04d0acba034e40cab231f703fcd5895812a0a45
6
+ metadata.gz: f8a31297a413089eb778c894904db38a0b8c57821ed14b09ecde3d0cadfb5066e9e3851a73fcb45d9857b91bc8725899d8f0e5ae722d754373ae740756dd31be
7
+ data.tar.gz: 0e92b90ebf6de86b492d7c6c74c15443a702476fd8a8e673d90dca9cc3fa02fbe47d4c02862483e405aa89032bb5a76181b79f83bc56b98144c7256f9c0742e5
data/bin/env_setup CHANGED
@@ -1,11 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- # require 'env_setup/env_builder'
3
+ require 'env_setup/env_builder'
4
4
  require 'json'
5
5
 
6
6
  inputs = Hash[ARGV.flat_map { |s| s.scan(/--?([^=\s]+)(?:=(\S+))?/) }]
7
7
  template_file = inputs.delete('TEMPLATE')
8
- puts "template #{template_file}"
9
8
  template = JSON.parse(File.read(template_file))
10
9
  env_file = inputs.delete('ENV_FILE') || '.env'
11
10
  aws_access_key = inputs.delete('AWS_ACCESS_KEY')
@@ -13,11 +12,9 @@ aws_secret_access_key = inputs.delete('AWS_SECRET_ACCESS_KEY')
13
12
  aws_region = inputs.delete('AWS_REGION')
14
13
  aws_secret_name = inputs.delete('AWS_SECRET_NAME')
15
14
  generate_env_file = inputs.key?('GENERATE_ENV_FILE')
16
- env_name = inputs.delete('ENV_NAME')
17
15
 
18
16
  EnvSetup.configure do |config|
19
17
  config.template = template
20
- config.env_name = env_name
21
18
  config.aws_access_key = aws_access_key
22
19
  config.aws_secret_access_key = aws_secret_access_key
23
20
  config.aws_region = aws_region
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'env_setup/builder/base'
4
+ require 'json'
5
+
6
+ module EnvSetup
7
+ module Builder
8
+ class Json < Base
9
+ def call
10
+ template.to_s
11
+ end
12
+ end
13
+ end
14
+ end
@@ -3,7 +3,7 @@
3
3
  module EnvSetup
4
4
  class Configuration
5
5
  attr_accessor :aws_access_key, :aws_secret_access_key, :aws_region, :aws_secret_name,
6
- :template, :env_name
6
+ :template
7
7
 
8
8
  def aws_ready?
9
9
  aws_secret_name && aws_access_key && aws_secret_access_key
@@ -5,6 +5,7 @@ require 'env_setup/builder/pattern'
5
5
  require 'env_setup/builder/input'
6
6
  require 'env_setup/builder/generator'
7
7
  require 'env_setup/builder/nested_value'
8
+ require 'env_setup/builder/json'
8
9
  require 'aws-sdk-secretsmanager'
9
10
 
10
11
  module EnvSetup
@@ -18,7 +19,7 @@ module EnvSetup
18
19
 
19
20
  def build_json
20
21
  secrets = aws_secrets
21
- configuration.template.merge(secrets).each do |key, var_template|
22
+ secrets.merge(configuration.template).each do |key, var_template|
22
23
  vars[key.to_s] = build_var(var_template)
23
24
  end
24
25
  vars
@@ -33,18 +34,23 @@ module EnvSetup
33
34
  private
34
35
 
35
36
  def var_builder(var_template)
36
- builder = [
37
- { builder: EnvSetup::Builder::Pattern, if: -> { var_template['pattern'] } },
38
- { builder: EnvSetup::Builder::NestedValue, if: -> { var_template['value'] } },
39
- { builder: EnvSetup::Builder::Input, if: -> { var_template['input'] } },
40
- { builder: EnvSetup::Builder::Generator, if: -> { var_template['generator'] } }
41
- ].find { |option| option[:if].call }
37
+ builder = var_builder_class(var_template)
42
38
 
43
39
  raise "Invalid var builder: #{var_template.inspect}" unless builder
44
40
 
45
41
  builder[:builder].new(var_template, vars.merge(inputs))
46
42
  end
47
43
 
44
+ def var_builder_class(var_template)
45
+ [
46
+ { builder: EnvSetup::Builder::Pattern, if: -> { var_template['pattern'] } },
47
+ { builder: EnvSetup::Builder::NestedValue, if: -> { var_template['value'] } },
48
+ { builder: EnvSetup::Builder::Input, if: -> { var_template['input'] } },
49
+ { builder: EnvSetup::Builder::Generator, if: -> { var_template['generator'] } },
50
+ { builder: EnvSetup::Builder::Json, if: -> { var_template.is_a?(Hash) } }
51
+ ].find { |option| option[:if].call }
52
+ end
53
+
48
54
  def configuration
49
55
  EnvSetup.configuration
50
56
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EnvSetup
4
- VERSION = '0.1.3'
4
+ VERSION = '0.1.8'
5
5
  end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+ require 'env_setup/builder/json'
3
+
4
+ RSpec.describe EnvSetup::Builder::Json do
5
+ let(:builder) { described_class.new(value, params) }
6
+ let(:params) { {} }
7
+ let(:value) do
8
+ {
9
+ 'some' => {
10
+ 'custom' => 'data'
11
+ }
12
+ }
13
+ end
14
+
15
+ it 'generates json string format' do
16
+ expect(builder.call).to eq "{\"some\"=>{\"custom\"=>\"data\"}}"
17
+ end
18
+ end
@@ -4,7 +4,6 @@ require 'env_setup/env_builder'
4
4
 
5
5
  RSpec.describe EnvSetup::EnvBuilder do
6
6
  describe '.build_json' do
7
- let(:env_name) { 'PR-12345' }
8
7
  let(:secret) { SecureRandom.hex }
9
8
  let(:salt) { SecureRandom.hex(2) }
10
9
  let(:template) do
@@ -22,30 +21,41 @@ RSpec.describe EnvSetup::EnvBuilder do
22
21
  'APP_PASSWORD' => {
23
22
  'generator' => 'salt'
24
23
  },
24
+ 'HOST' => { 'pattern' => 'wss://{{APP_NAME}}-community2-cable.{{DOMAIN}}/cable' },
25
25
  'INTEGRATION_APP_HOST' => {
26
26
  'input' => 'INTEGRATION_APP_HOST'
27
27
  },
28
28
  'INTEGRATION_APP_URL' => {
29
29
  'pattern' => '{{INTEGRATION_APP_HOST}}/api/integrate'
30
+ },
31
+ 'WELCOME_MSG' => {
32
+ 'pattern' => 'Welcome {{USERNAME}}'
33
+ },
34
+ 'DOMAINS' => {
35
+ 'APP' => ['abc.com', 'def.com'],
36
+ 'ADMIN' => ['abc-admin.com', 'def-admin.com']
30
37
  }
31
38
  }
32
39
  end
33
40
 
34
41
  context 'with aws secrets manager' do
35
42
  let(:inputs) do
36
- { 'INTEGRATION_APP_HOST' => 'https://integrate.foobar2.com' }
43
+ { 'INTEGRATION_APP_HOST' => 'https://integrate.foobar2.com', 'ENV_NAME' => 'PR-12345' }
37
44
  end
38
45
  let(:expected_json) do
39
46
  {
40
47
  'LANG' => 'en_US.UTF-8',
48
+ 'USERNAME' => 'foo',
49
+ 'HOST' => 'wss://pr-123-community2-cable.foobar.com/cable',
41
50
  'DOMAIN' => 'foobar.com',
42
51
  'APP_NAME' => 'pr-123',
43
- 'HOST' => 'wss://pr-123-community2-cable.foobar.com/cable',
44
52
  'APP_HOST' => 'https://pr-123.foobar.com',
45
53
  'APP_SECRET' => secret,
46
54
  'APP_PASSWORD' => salt,
47
55
  'INTEGRATION_APP_HOST' => inputs['INTEGRATION_APP_HOST'],
48
- 'INTEGRATION_APP_URL' => "#{inputs['INTEGRATION_APP_HOST']}/api/integrate"
56
+ 'INTEGRATION_APP_URL' => "#{inputs['INTEGRATION_APP_HOST']}/api/integrate",
57
+ 'WELCOME_MSG' => 'Welcome foo',
58
+ 'DOMAINS' => '{"APP"=>["abc.com", "def.com"], "ADMIN"=>["abc-admin.com", "def-admin.com"]}'
49
59
  }
50
60
  end
51
61
 
@@ -55,7 +65,6 @@ RSpec.describe EnvSetup::EnvBuilder do
55
65
 
56
66
  allow(EnvSetup).to receive(:configuration).and_return(
57
67
  EnvSetup::Configuration.new.tap do |config|
58
- config.env_name = env_name
59
68
  config.template = template
60
69
  config.aws_access_key = 'aws_access_key'
61
70
  config.aws_secret_access_key = 'aws_secret_access_key'
@@ -66,7 +75,7 @@ RSpec.describe EnvSetup::EnvBuilder do
66
75
  allow_any_instance_of(EnvSetup::EnvBuilder).to receive(:aws_secrets).and_return(
67
76
  {
68
77
  'LANG' => 'en_US.UTF-8',
69
- 'HOST' => {"pattern" => "wss://{{APP_NAME}}-community2-cable.{{DOMAIN}}/cable"},
78
+ 'USERNAME' => 'foo'
70
79
  }
71
80
  )
72
81
  end
@@ -78,7 +87,7 @@ RSpec.describe EnvSetup::EnvBuilder do
78
87
 
79
88
  context 'without aws secrets manager' do
80
89
  let(:inputs) do
81
- { 'INTEGRATION_APP_HOST' => 'https://integrate.foobar2.com' }
90
+ { 'INTEGRATION_APP_HOST' => 'https://integrate.foobar2.com', 'USERNAME' => 'foo' }
82
91
  end
83
92
  let(:expected_json) do
84
93
  {
@@ -87,8 +96,11 @@ RSpec.describe EnvSetup::EnvBuilder do
87
96
  'APP_HOST' => 'https://pr-123.foobar.com',
88
97
  'APP_SECRET' => secret,
89
98
  'APP_PASSWORD' => salt,
99
+ 'HOST' => 'wss://pr-123-community2-cable.foobar.com/cable',
90
100
  'INTEGRATION_APP_HOST' => inputs['INTEGRATION_APP_HOST'],
91
- 'INTEGRATION_APP_URL' => "#{inputs['INTEGRATION_APP_HOST']}/api/integrate"
101
+ 'INTEGRATION_APP_URL' => "#{inputs['INTEGRATION_APP_HOST']}/api/integrate",
102
+ 'WELCOME_MSG' => 'Welcome foo',
103
+ 'DOMAINS' => '{"APP"=>["abc.com", "def.com"], "ADMIN"=>["abc-admin.com", "def-admin.com"]}'
92
104
  }
93
105
  end
94
106
 
@@ -98,7 +110,6 @@ RSpec.describe EnvSetup::EnvBuilder do
98
110
 
99
111
  allow(EnvSetup).to receive(:configuration).and_return(
100
112
  EnvSetup::Configuration.new.tap do |config|
101
- config.env_name = env_name
102
113
  config.template = template
103
114
  end
104
115
  )
@@ -108,74 +119,10 @@ RSpec.describe EnvSetup::EnvBuilder do
108
119
  expect(described_class.new(inputs).build_json).to eq expected_json
109
120
  end
110
121
  end
111
-
112
- context 'test' do
113
- before do
114
- allow_any_instance_of(EnvSetup::Builder::Generator).to receive(:secret).and_return("secret")
115
- allow_any_instance_of(EnvSetup::Builder::Generator).to receive(:salt).and_return("salt")
116
-
117
- allow(EnvSetup).to receive(:configuration).and_return(
118
- EnvSetup::Configuration.new.tap do |config|
119
- config.env_name = 'pr-123'
120
- config.template = template
121
- end
122
- )
123
- end
124
-
125
- let(:template) do
126
- {
127
- "ASSET_HOST": {
128
- "pattern": "https://{{ENV_NAME}}.{{DOMAIN}}"
129
- },
130
- "CANONICAL_URL": {
131
- "pattern": "https://{{ENV_NAME}}.{{DOMAIN}}"
132
- },
133
- "DISCOURSE_AUTH_DOMAIN": {
134
- "pattern": "https://{{ENV_NAME}}.goeatrightnow.com"
135
- },
136
- "DOMAIN_NAME": {
137
- "input": "DOMAIN"
138
- },
139
- "MESSENGER_URI": {
140
- "input": "MESSENGER_URI"
141
- },
142
- "MSCOMMUNITY_URL": {
143
- "input": "MSCOMMUNITY_URL"
144
- },
145
- "PROGRAM_BBS_APP_HOST": {
146
- "pattern": "{{ENV_NAME}}.breathebysharecare.com"
147
- },
148
- "PROGRAM_CTQ_APP_HOST": {
149
- "pattern": "{{ENV_NAME}}.cravingtoquit.com"
150
- },
151
- "PROGRAM_ERN_APP_HOST": {
152
- "pattern": "{{ENV_NAME}}.goeatrightnow.com"
153
- },
154
- "PROGRAM_UA_APP_HOST": {
155
- "pattern": "{{ENV_NAME}}.unwindinganxiety.com"
156
- },
157
- "PROGRAM_UFS_APP_HOST": {
158
- "pattern": "{{ENV_NAME}}.unwindingfromsharecare.com"
159
- }
160
- }
161
- end
162
-
163
- let(:inputs) do
164
- {
165
- 'DOMAIN' => 'mindsciences.net',
166
- 'MESSENGER_URI' => 'messenger.mindsciences.net',
167
- 'MSCOMMUNITY_URL' => 'mscom.mindsciences.net'
168
- }
169
- end
170
-
171
- it 'generate json' do
172
- described_class.new(inputs).build_json
173
- end
174
- end
175
122
  end
176
123
 
177
124
  describe '.build_var' do
178
- let(:inputs) { { 'MY_VAR' => 'Hey!', 'FOO' => 'bar' } }
125
+ let(:inputs) { { 'MY_VAR' => 'Hey!', 'FOO' => 'bar', 'ENV_NAME' => 'env-test' } }
179
126
  let(:env_name) { 'env-test' }
180
127
  let(:template) { {} }
181
128
  let(:builder) { described_class.new(inputs) }
@@ -183,7 +130,6 @@ RSpec.describe EnvSetup::EnvBuilder do
183
130
  before do
184
131
  EnvSetup.configure do |config|
185
132
  config.template = template
186
- config.env_name = env_name
187
133
  end
188
134
  end
189
135
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: env_setup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonatas Daniel Hermann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-25 00:00:00.000000000 Z
11
+ date: 2021-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -96,6 +96,7 @@ files:
96
96
  - lib/env_setup/builder/base.rb
97
97
  - lib/env_setup/builder/generator.rb
98
98
  - lib/env_setup/builder/input.rb
99
+ - lib/env_setup/builder/json.rb
99
100
  - lib/env_setup/builder/nested_value.rb
100
101
  - lib/env_setup/builder/pattern.rb
101
102
  - lib/env_setup/configuration.rb
@@ -103,6 +104,7 @@ files:
103
104
  - lib/env_setup/version.rb
104
105
  - spec/env_setup/builder/generator_spec.rb
105
106
  - spec/env_setup/builder/input_spec.rb
107
+ - spec/env_setup/builder/json_spec.rb
106
108
  - spec/env_setup/builder/pattern_spec.rb
107
109
  - spec/env_setup/env_builder_spec.rb
108
110
  - spec/env_setup_spec.rb
@@ -134,5 +136,6 @@ test_files:
134
136
  - spec/env_setup_spec.rb
135
137
  - spec/env_setup/env_builder_spec.rb
136
138
  - spec/env_setup/builder/generator_spec.rb
139
+ - spec/env_setup/builder/json_spec.rb
137
140
  - spec/env_setup/builder/pattern_spec.rb
138
141
  - spec/env_setup/builder/input_spec.rb