rcloadenv 0.1.1 → 0.2.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 31510d7452b0e8c4709b51f140cd4c5986129e75
4
- data.tar.gz: a6a203aef79daa98a74fdd48ade7559249e5fe71
2
+ SHA256:
3
+ metadata.gz: 1cc4bb306ab969ea6a63f15e5fc9075df593306293f766d660f23a2ec9087dd0
4
+ data.tar.gz: feb5de2a2e1a2de5c08a0ce42c54d88496cf8a4d478a37ecbe980dbfa4bd7f98
5
5
  SHA512:
6
- metadata.gz: e7469d40251b4d9cd965d503544c9a56d81dbebdccbb17b54b11cf67957ee917ff2f4eb11476740a20b3d025e3246c5f426b9cbced3ee73ad8d3c8c5368da735
7
- data.tar.gz: e4949ed052f0dbe44877f3048f23e4fab92a96889c48bfdb5339ae97145dbd896b3f5aa881e18aaf5775ed8c5af3f8b2aeb1ffdac5f7cbfa89ae6954ee26b13f
6
+ metadata.gz: 9fdc21e25f7b66625cad77dbf9ad9872e1150728c07775e3d47b45ee2e57d9be83ade8f1fbe2d2c7d385e10ee4b1706e3fd398d9cc8cef32687a4f369f709ae0
7
+ data.tar.gz: a6aa807120a6ea3d0b449d7a6b9e2ae910324cabf5c1875076ec4fd484e867890d8f5f443a97cfb732dfa2cbbbbe44250a839a12cdb45455385a6a0dbef06e72
data/README.md CHANGED
@@ -14,11 +14,27 @@ Install the gem using
14
14
  Alternately, include "rcloadenv" in your application's Gemfile.
15
15
 
16
16
  You may then invoke the "rcloadenv" binary. You must pass the name of the
17
- runtime config resource, and then the command to execute. For example, if
18
- the gem is present in the bundle for your Rails app, you can execute:
17
+ runtime config resource, and then optionally a command to execute, delimited
18
+ by two dashes `--`.
19
+
20
+ If a command is provided, rcloadenv loads the configuration into environment
21
+ variables and runs the command. For example, if the gem is present in the
22
+ bundle for your Rails app, you can execute:
19
23
 
20
24
  bundle exec rcloadenv my-config -- bin/rails s
21
25
 
26
+ to run Rails directly with the loaded environment.
27
+
28
+ If a command is _not_ provided_, rcloadenv loads the configuration and prints
29
+ it to stdout in dotenv-compatible format. For example, you can execute:
30
+
31
+ bundle exec rcloadenv my-config > .env
32
+
33
+ to create a `.env` file suitable for loading with the
34
+ [dotenv](https://github.com/bkeepers/dotenv/) gem.
35
+
36
+ ### Accessing runtime config data
37
+
22
38
  If `rcloadenv` is run on Google Cloud Platform hosting (such as Google Compute
23
39
  Engine, Container Engine, or App Engine), then it infers the project name and
24
40
  credentials from the hosting environment.
@@ -34,7 +50,7 @@ Run `rcloadenv --help` for more information on flags you can set.
34
50
  When not running on GCP, credentials are obtained from
35
51
  [Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials),
36
52
  so you can set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable or
37
- configure `gcloud auth`.
53
+ configure `gcloud auth application-default login`.
38
54
 
39
55
  ## Example: Loading the Rails SECRET_KEY_BASE in Google App Engine
40
56
 
@@ -35,7 +35,7 @@ module RCLoadEnv
35
35
  @debug = false
36
36
 
37
37
  @parser = OptionParser.new do |opts|
38
- opts.banner = "Usage: rcloadenv [options] <config-name> -- <command>"
38
+ opts.banner = "Usage: rcloadenv [options] <config-name> [-- <command>]"
39
39
  opts.on "-p name", "--project=name", "--projectId=name",
40
40
  "Project to read runtime config from" do |p|
41
41
  @project = p
@@ -66,7 +66,7 @@ module RCLoadEnv
66
66
  end
67
67
 
68
68
  separator_index = args.index "--"
69
- @command_list = separator_index ? args[(separator_index+1)..-1] : []
69
+ @command_list = separator_index ? args[(separator_index+1)..-1] : nil
70
70
 
71
71
  args = args[0..separator_index] if separator_index
72
72
  begin
@@ -82,8 +82,8 @@ module RCLoadEnv
82
82
  usage_error "Extra arguments found: #{remaining_args.inspect}"
83
83
  end
84
84
 
85
- if @command_list.empty?
86
- usage_error "You must provide a command delimited by `--`."
85
+ if @command_list && @command_list.empty?
86
+ usage_error "Command cannot be empty. To output dotenv format, omit `--`."
87
87
  end
88
88
  end
89
89
 
@@ -95,8 +95,12 @@ module RCLoadEnv
95
95
  loader = RCLoadEnv::Loader.new @config_name,
96
96
  exclude: @exclude, include: @include, override: @override,
97
97
  project: @project, debug: @debug
98
- loader.modify_env ENV
99
- exec(*@command_list)
98
+ if @command_list
99
+ loader.modify_env ENV
100
+ exec(*@command_list)
101
+ else
102
+ loader.write_dotenv
103
+ end
100
104
  end
101
105
 
102
106
  ## @private
@@ -13,6 +13,7 @@
13
13
  # limitations under the License.
14
14
  ;
15
15
 
16
+ require "shellwords"
16
17
  require "rcloadenv/api_client"
17
18
  require "google/cloud/env"
18
19
 
@@ -72,7 +73,7 @@ module RCLoadEnv
72
73
  debug "Skipping config variable #{k}"
73
74
  else
74
75
  debug "Found config variable #{k}"
75
- key = k.split("/").last.upcase.gsub("-", "_")
76
+ key = make_env_key k
76
77
  if !env.include?(key)
77
78
  debug "Setting envvar: #{key}"
78
79
  env[key] = v
@@ -88,7 +89,26 @@ module RCLoadEnv
88
89
  end
89
90
 
90
91
  ##
91
- # Returns the has of variables retrieved from the runtime config.
92
+ # Modify the given environment with the configuration. The given hash
93
+ # is modified in place and returned. If no hash is provided, a new one is
94
+ # created and returned.
95
+ #
96
+ # @param [Hash<String,String>] env The environment to modify.
97
+ # @return the modified environment.
98
+ #
99
+ def write_dotenv io=STDOUT
100
+ raw_variables.each do |k, v|
101
+ key = make_env_key k
102
+ if key =~ /^[\w\.]+$/
103
+ io.puts "#{key}=\"#{escape_for_dotenv v}\""
104
+ else
105
+ error "Bad key: #{key}"
106
+ end
107
+ end
108
+ end
109
+
110
+ ##
111
+ # Returns the hash of variables retrieved from the runtime config.
92
112
  # Variable names have the parent (project and config name) stripped.
93
113
  # Values are all converted to strings.
94
114
  #
@@ -149,10 +169,25 @@ module RCLoadEnv
149
169
  s
150
170
  end
151
171
 
172
+ ## @private
173
+ def make_env_key key
174
+ key.split("/").last.upcase.gsub("-", "_")
175
+ end
176
+
177
+ ## @private
178
+ def escape_for_dotenv value
179
+ value.gsub("\\", "\\\\\\\\").gsub("\n", "\\\\n").gsub("$", "\\\\$")
180
+ end
181
+
152
182
  ## @private
153
183
  def debug str
154
184
  STDERR.puts "RCLOADENV DEBUG: #{str}" if @debug
155
185
  end
186
+
187
+ ## @private
188
+ def error str
189
+ STDERR.puts "RCLOADENV ERROR: #{str}"
190
+ end
156
191
  end
157
192
 
158
193
  end
@@ -16,6 +16,6 @@
16
16
  module RCLoadEnv
17
17
 
18
18
  ## The current version of this gem, as a string.
19
- VERSION = '0.1.1'.freeze
19
+ VERSION = '0.2.0.pre.1'.freeze
20
20
 
21
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rcloadenv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0.pre.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Azuma
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-11 00:00:00.000000000 Z
11
+ date: 2017-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-api-client
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0.9'
125
+ - !ruby/object:Gem::Dependency
126
+ name: dotenv
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '2.2'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '2.2'
125
139
  description: rcloadenv is a tool for loading configuration from the Google Runtime
126
140
  Config API into environment variables. The rcloadenv ruby gem is a ruby implementation
127
141
  of the tool that may be installed as a ruby gem or included in a ruby Gemfile.
@@ -161,12 +175,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
161
175
  version: 2.0.0
162
176
  required_rubygems_version: !ruby/object:Gem::Requirement
163
177
  requirements:
164
- - - ">="
178
+ - - ">"
165
179
  - !ruby/object:Gem::Version
166
- version: '0'
180
+ version: 1.3.1
167
181
  requirements: []
168
182
  rubyforge_project:
169
- rubygems_version: 2.6.13
183
+ rubygems_version: 2.6.14
170
184
  signing_key:
171
185
  specification_version: 4
172
186
  summary: Load Google Runtime Config data into environment variables