dotgpg-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 19a1436ce7a20848dfed96f810d4dcb7e1e3b1e5
4
+ data.tar.gz: 541f0b28f7bb869d83aeb6ff05865bc2c8e9a82f
5
+ SHA512:
6
+ metadata.gz: b3ca145f0f8963d25a916a9cab1742808ad37ff77319c6a6661de3844b9f854eff124c0e47af6f4aae5e0b488cea8da0a097f343416eae1444d3c5ffed99d75b
7
+ data.tar.gz: ec9b2e0a40fcac1b68cdea05278cbe8b0cdaafe676abb5dc8d411c0ca7000b630b523d895d287b2e14d355e557319f9a7e2e4684d69af2c45f5752a63fced1ca
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dotgpg-rails.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # Dotgpg::Rails
2
+
3
+ Shim to load environment variables directly into Rails from dotgpg encrypted files.
4
+
5
+
6
+ ## Installation
7
+
8
+ ### Rails
9
+
10
+ Add this line to the top of your application's Gemfile
11
+
12
+ ```ruby
13
+ gem 'dotgpg-rails', :groups => [:development, :test]
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install dotgpg-rails
23
+
24
+ You should also refer to the dotenv Notes on load order section.
25
+
26
+
27
+ ## Usage
28
+
29
+ This code is almost 100% copied from dotgpg/rails, refer to the [dotenv readme](https://github.com/bkeepers/dotenv/blob/master/README.md), for more info since almost all of that document applies to this gem as well.
30
+
31
+ #### Note on load order
32
+
33
+ dotgpg is initialized in your Rails app during the `before_configuration` callback, which is fired when the `Application` constant is defined in `config/application.rb` with `class Application < Rails::Application`. If you need it to be initialized sooner, you can manually call `Dotgpg::Railtie.load`.
34
+
35
+ ```ruby
36
+ # config/application.rb
37
+ Bundler.require(*Rails.groups)
38
+
39
+ Dotgpg::Railtie.load
40
+
41
+ HOSTNAME = ENV['HOSTNAME']
42
+ ```
43
+
44
+ If you use gems that require environment variables to be set before they are loaded, then list `dotenv-rails` in the `Gemfile` before those other gems and require `dotgpg/rails-now`.
45
+
46
+ ```ruby
47
+ gem 'dotgpg-rails', :require => 'dotgpg/rails-now'
48
+ gem 'gem-that-requires-env-variables'
49
+ ```
50
+
51
+
52
+ ## Development
53
+
54
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
55
+
56
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it ( https://github.com/[my-github-username]/dotgpg-rails/fork )
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rspec/core/rake_task"
4
+
5
+ desc "Run all specs"
6
+ RSpec::Core::RakeTask.new(:spec) do |t|
7
+ t.rspec_opts = %w(--color)
8
+ t.verbose = false
9
+ end
10
+
11
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dotgpg/rails"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dotgpg/rails/version'
5
+ require "English"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "dotgpg-rails"
9
+ spec.version = Dotgpg::Rails::VERSION
10
+ spec.authors = ["Jonathan Schatz"]
11
+ spec.email = ["jon@divisionbyzero.com"]
12
+ spec.description = spec.summary = "Autoload environment variables from dotgpg-encrypted files into Rails."
13
+ spec.homepage = "https://github.com/vouch/dotgpg-rails"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.9"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "spring"
25
+ spec.add_development_dependency "railties", "~>4.0"
26
+
27
+ spec.add_dependency "dotgpg"
28
+ spec.add_dependency "dotgpg-environment", "~> 0.1.1"
29
+ spec.add_dependency "dotenv", "~> 2.0.1"
30
+ end
@@ -0,0 +1 @@
1
+ require 'dotgpg/rails'
@@ -0,0 +1,10 @@
1
+ # If you use gems that require environment variables to be set before they are
2
+ # loaded, then list `dotgpg-rails` in the `Gemfile` before those other gems and
3
+ # require `dotgpg/rails-now`.
4
+ #
5
+ # gem "dotgpg-rails", :require => "dotgpg/rails-now"
6
+ # gem "gem-that-requires-env-variables"
7
+ #
8
+
9
+ require "dotgpg/rails"
10
+ Dotgpg::Railtie.load
@@ -0,0 +1,125 @@
1
+ require "dotgpg/rails/version"
2
+ require "dotgpg"
3
+ require "dotgpg/environment"
4
+
5
+ class Dotgpg
6
+
7
+ module Rails
8
+ class << self
9
+ attr_accessor :instrumenter
10
+ end
11
+
12
+ module_function
13
+
14
+ def load(*filenames)
15
+ with(*filenames) do |f|
16
+ ignoring_nonexistent_files do
17
+ env = Environment.new(f)
18
+ instrument("dotgpg.load", :env => env) { env.apply }
19
+ end
20
+ end
21
+ end
22
+
23
+ # same as `load`, but raises Errno::ENOENT if any files don't exist
24
+ def load!(*filenames)
25
+ with(*filenames) do |f|
26
+ env = Environment.new(f)
27
+ instrument("dotgpg.load", :env => env) { env.apply }
28
+ end
29
+ end
30
+
31
+ # same as `load`, but will override existing values in `ENV`
32
+ def overload(*filenames)
33
+ with(*filenames) do |f|
34
+ ignoring_nonexistent_files do
35
+ env = Environment.new(f)
36
+ instrument("dotgpg.overload", :env => env) { env.apply! }
37
+ end
38
+ end
39
+ end
40
+
41
+
42
+ # Internal: Helper to expand list of filenames.
43
+ #
44
+ # Returns a hash of all the loaded environment variables.
45
+ def with(*filenames, &block)
46
+ filenames << ".env" if filenames.empty?
47
+
48
+ filenames.reduce({}) do |hash, filename|
49
+ hash.merge! block.call(File.expand_path(filename)) || {}
50
+ end
51
+ end
52
+
53
+ def instrument(name, payload = {}, &block)
54
+ if instrumenter
55
+ instrumenter.instrument(name, payload, &block)
56
+ else
57
+ block.call
58
+ end
59
+ end
60
+
61
+ def ignoring_nonexistent_files
62
+ yield
63
+ rescue Errno::ENOENT
64
+ end
65
+
66
+ end
67
+
68
+ class Railtie < ::Rails::Railtie
69
+ config.before_configuration { load }
70
+
71
+ rake_tasks do
72
+ Rake.load_rakefile File.expand_path('../rails/tasks/dotgpg_rails.rake', __FILE__).to_s
73
+ end
74
+
75
+ # Public: Load dotgpg
76
+ #
77
+ # This will get called during the `before_configuration` callback, but you
78
+ # can manually call `Dotgpg::Railtie.load` if you needed it sooner.
79
+ def load
80
+ file = root.join 'config/dotgpg', "#{::Rails.env}.gpg"
81
+ if File.exists? file
82
+ Dotgpg::Rails.load file
83
+ else
84
+ # if our file doesn't exist don't bother trying to load. if we're not
85
+ # being called from our rake task print an error message to STDERR
86
+ unless File.basename($PROGRAM_NAME) == 'rake' && ARGV[0] == 'dotgpg:rails:init'
87
+ $stderr.puts "Couldn't initialize dotgpg file #{file}: " <<
88
+ "(do you need to run 'rake dotgpg:rails:init' ?)"
89
+ end
90
+ # gracefully fail and return an empty string - maybe we want to make this
91
+ # fatal in the future?
92
+ ''
93
+ end
94
+ end
95
+
96
+ # Internal: `Rails.root` is nil in Rails 4.1 before the application is
97
+ # initialized, so this falls back to the `RAILS_ROOT` environment variable,
98
+ # or the current working directory.
99
+ def root
100
+ ::Rails.root || Pathname.new(ENV["RAILS_ROOT"] || Dir.pwd)
101
+ end
102
+
103
+ # Rails uses `#method_missing` to delegate all class methods to the
104
+ # instance, which means `Kernel#load` gets called here. We don't want that.
105
+ def self.load
106
+ instance.load
107
+ end
108
+
109
+ end
110
+ end
111
+
112
+ Dotgpg::Rails.instrumenter = ActiveSupport::Notifications
113
+
114
+ # Watch all loaded env files with Spring
115
+ begin
116
+ require "spring/watcher"
117
+ ActiveSupport::Notifications.subscribe(/^dotgpg/) do |*args|
118
+ event = ActiveSupport::Notifications::Event.new(*args)
119
+ Spring.watch event.payload[:env].filename if Rails.application
120
+ end
121
+ rescue LoadError
122
+ # Spring is not available
123
+ end
124
+
125
+
@@ -0,0 +1,57 @@
1
+ namespace :dotgpg do
2
+ namespace :rails do
3
+ desc "initialize dotgpg_rails"
4
+ task :init do
5
+ require 'fileutils'
6
+ require 'dotgpg'
7
+
8
+ # create config/dotgpg if it doesn't already exist
9
+ dir = Rails.root.join 'config/dotgpg'
10
+ unless File.directory?(dir)
11
+ $stdout.puts "Creating #{dir}"
12
+ FileUtils.mkdir_p dir
13
+ end
14
+
15
+ # setup dotgpg directory if necessary
16
+ dotgpg_dir = Dotgpg::Dir.new(dir)
17
+ unless dotgpg_dir.dotgpg?
18
+ begin
19
+ $stdout.puts "Initializing dotgpg directory #{dir}"
20
+ Dir.chdir dir
21
+ Dotgpg.interactive = true
22
+ Dotgpg::Cli.start ['init']
23
+ ensure
24
+ # make sure thor didn't overwrite STDIN/STDOUT/STDERR
25
+ $stderr = STDERR
26
+ $stdin = STDIN
27
+ $stdout = STDOUT
28
+
29
+ # and change back to our RAILS_ROOT
30
+ Dir.chdir Rails.root
31
+ end
32
+ end
33
+
34
+ # get the list of rails environments that we require dotgpg-rails in and
35
+ # initialize a dotgpg file for each one. this depends on having the gem
36
+ # listed either in :default or in groups that match 1:1 environments we
37
+ # can find in config/environments. it's somewhat error-prone, i suppose we
38
+ # could also just create one for every environment?
39
+ definition = Bundler.definition
40
+ envs = Dir['./config/environments/*'].map{|f| File.basename f, '.*'}
41
+ if definition.specs_for([:default])['dotgpg-rails'].empty?
42
+ # we're not in the default group, check our gemfile for groups that match
43
+ # our environment names
44
+ envs.reject!{|e| definition.specs_for([e.to_sym])['dotgpg-rails'].empty? }
45
+ end
46
+
47
+ # for each of the environments that require dotgpg-rails initialize a blank
48
+ # dotgpg file in config/dotgpg/environment.gpg
49
+ envs.each do |env|
50
+ path = File.join dir, "#{env}.gpg"
51
+ next if File.exists? path
52
+ $stdout.puts "Initializing empty dotgpg file #{path}"
53
+ dotgpg_dir.encrypt path, "# placeholder dotgpg file for #{env} environment.\n"
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,5 @@
1
+ class Dotgpg
2
+ module Rails
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dotgpg-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Schatz
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: spring
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: railties
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: dotgpg
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: dotgpg-environment
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.1.1
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.1.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: dotenv
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 2.0.1
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 2.0.1
125
+ description: Autoload environment variables from dotgpg-encrypted files into Rails.
126
+ email:
127
+ - jon@divisionbyzero.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".travis.yml"
135
+ - Gemfile
136
+ - README.md
137
+ - Rakefile
138
+ - bin/console
139
+ - bin/setup
140
+ - dotgpg-rails.gemspec
141
+ - lib/dotgpg-rails.rb
142
+ - lib/dotgpg/rails-now.rb
143
+ - lib/dotgpg/rails.rb
144
+ - lib/dotgpg/rails/tasks/dotgpg_rails.rake
145
+ - lib/dotgpg/rails/version.rb
146
+ homepage: https://github.com/vouch/dotgpg-rails
147
+ licenses:
148
+ - MIT
149
+ metadata: {}
150
+ post_install_message:
151
+ rdoc_options: []
152
+ require_paths:
153
+ - lib
154
+ required_ruby_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ requirements: []
165
+ rubyforge_project:
166
+ rubygems_version: 2.4.6
167
+ signing_key:
168
+ specification_version: 4
169
+ summary: Autoload environment variables from dotgpg-encrypted files into Rails.
170
+ test_files: []