envs 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.env +1 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +21 -0
- data/Rakefile +8 -0
- data/bin/envs +5 -0
- data/lib/envs.rb +5 -0
- data/lib/envs/cli.rb +19 -0
- data/lib/envs/env_file.rb +36 -0
- data/lib/envs/runner.rb +52 -0
- data/lib/envs/version.rb +3 -0
- data/ranger.gemspec +24 -0
- data/spec/ranger_spec.rb +16 -0
- data/spec/spec_helper.rb +63 -0
- metadata +127 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Josh Hull
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# Envs
|
2
|
+
|
3
|
+
Sync up values from your `.env` and `.env.default` files.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install it using ...
|
8
|
+
|
9
|
+
$ gem install envs
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
Try running `envs` in your project root. This will traverse your entire project and find any `.env` files or `.env.default` files and make sure they have the same values in them.
|
14
|
+
|
15
|
+
## Contributing
|
16
|
+
|
17
|
+
1. Fork it
|
18
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
19
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
20
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
21
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bin/envs
ADDED
data/lib/envs.rb
ADDED
data/lib/envs/cli.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'trollop'
|
2
|
+
|
3
|
+
module Envs
|
4
|
+
class CLI
|
5
|
+
def self.run
|
6
|
+
new.run
|
7
|
+
end
|
8
|
+
|
9
|
+
def run
|
10
|
+
global_opts = Trollop::options do
|
11
|
+
banner "Manage environment variables on a per-project basis"
|
12
|
+
opt :no_color, "Don't be colorized", :short => "-c"
|
13
|
+
opt :root, "Project root directory", :short => "-r", :default => Dir.pwd
|
14
|
+
end
|
15
|
+
runner = Runner.new(global_opts)
|
16
|
+
runner.run
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Envs
|
2
|
+
class EnvFile < Hash
|
3
|
+
attr_reader :path
|
4
|
+
|
5
|
+
def initialize(path)
|
6
|
+
@path = path
|
7
|
+
parse if exist?
|
8
|
+
end
|
9
|
+
|
10
|
+
def parse
|
11
|
+
new_hash = {}
|
12
|
+
File.open(@path).each_line do |line|
|
13
|
+
case line
|
14
|
+
when /^\s*$/, /^#/ # ignore
|
15
|
+
else
|
16
|
+
line.chomp
|
17
|
+
key, value = line.split('=', 2)
|
18
|
+
new_hash[key] = value.strip
|
19
|
+
end
|
20
|
+
end
|
21
|
+
update(new_hash)
|
22
|
+
end
|
23
|
+
|
24
|
+
def []=(key, value)
|
25
|
+
super(key, value)
|
26
|
+
File.open(@path, "a") do |f|
|
27
|
+
f << "#{key}=#{value}\n"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
def exist?
|
33
|
+
File.exist?(@path)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/envs/runner.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rainbow'
|
2
|
+
|
3
|
+
module Envs
|
4
|
+
class Runner
|
5
|
+
def initialize(opts)
|
6
|
+
@root = opts[:root]
|
7
|
+
Sickill::Rainbow.enabled = !opts[:no_color]
|
8
|
+
@count = 0
|
9
|
+
@synced_dirs = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def run
|
13
|
+
puts "Envs! #{VERSION}"
|
14
|
+
|
15
|
+
gitignore = File.join(@root, ".gitignore")
|
16
|
+
if File.exist?(gitignore)
|
17
|
+
warn "You should probably add .env to your gitignore file fool!".foreground(:red) unless File.read(gitignore).split(/\n/).include?('.env')
|
18
|
+
end
|
19
|
+
|
20
|
+
Dir[File.join(@root, "**/.env")].each do |dir|
|
21
|
+
sync File.dirname(dir)
|
22
|
+
end
|
23
|
+
Dir[File.join(@root, "**/.env.default")].each do |dir|
|
24
|
+
sync File.dirname(dir)
|
25
|
+
end
|
26
|
+
|
27
|
+
puts "Synced #{@count.to_s.foreground(:green)} values in #{@synced_dirs.size.to_s.foreground(:green)} dirs. Your welcome."
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
def sync(dir)
|
32
|
+
return if @synced_dirs.include?(dir)
|
33
|
+
|
34
|
+
env = EnvFile.new(File.join(dir, ".env"))
|
35
|
+
defaults = EnvFile.new(File.join(dir, ".env.default"))
|
36
|
+
files = [env, defaults]
|
37
|
+
|
38
|
+
values = defaults.merge(env)
|
39
|
+
2.times do
|
40
|
+
keys = files.first.keys - files.last.keys
|
41
|
+
keys.each do |key|
|
42
|
+
value = values[key]
|
43
|
+
puts "Adding #{key.foreground(:green)}=#{value.foreground(:green)} to #{files.last.path.foreground(:yellow)}"
|
44
|
+
files.last[key] = value
|
45
|
+
@count += 1
|
46
|
+
end
|
47
|
+
files.reverse!
|
48
|
+
end
|
49
|
+
@synced_dirs << dir
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/envs/version.rb
ADDED
data/ranger.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'envs/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "envs"
|
8
|
+
gem.version = Envs::VERSION
|
9
|
+
gem.authors = ["Josh Hull"]
|
10
|
+
gem.email = ["joshbuddy@gmail.com"]
|
11
|
+
gem.description = %q{Sync your .env and .env.default files}
|
12
|
+
gem.summary = %q{Sync your .env and .env.default files.}
|
13
|
+
gem.homepage = "https://github.com/joshbuddy/envs"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency "trollop"
|
21
|
+
gem.add_dependency "highline"
|
22
|
+
gem.add_dependency "rainbow"
|
23
|
+
gem.add_development_dependency "minitest"
|
24
|
+
end
|
data/spec/ranger_spec.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
describe "Envs!" do
|
2
|
+
|
3
|
+
before { clear_project }
|
4
|
+
|
5
|
+
it "should sync to default" do
|
6
|
+
env "test" => "value"
|
7
|
+
run_envs
|
8
|
+
assert_synced "test" => "value"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should sync to env" do
|
12
|
+
default "test" => "value"
|
13
|
+
run_envs
|
14
|
+
assert_synced "test" => "value"
|
15
|
+
end
|
16
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
$: << File.expand_path("../../lib", __FILE__)
|
2
|
+
|
3
|
+
require 'envs'
|
4
|
+
require 'minitest/autorun'
|
5
|
+
|
6
|
+
class MiniTest::Spec
|
7
|
+
|
8
|
+
def clear_project
|
9
|
+
FileUtils.rm_rf("/tmp/envs-test")
|
10
|
+
FileUtils.mkdir_p("/tmp/envs-test")
|
11
|
+
end
|
12
|
+
|
13
|
+
def run_envs(*args)
|
14
|
+
args.concat %w(-r /tmp/envs-test)
|
15
|
+
ARGV.replace(args)
|
16
|
+
pid = fork { Envs::CLI.run }
|
17
|
+
_, status = Process.waitpid2(pid)
|
18
|
+
status
|
19
|
+
end
|
20
|
+
|
21
|
+
def assert_synced(vals = {})
|
22
|
+
assert_equal vals, env_result
|
23
|
+
assert_equal vals, default_result
|
24
|
+
end
|
25
|
+
|
26
|
+
def parse_env_file(f)
|
27
|
+
return nil unless File.exist?(f)
|
28
|
+
env = {}
|
29
|
+
File.read(f).split(/\n/).each do |line|
|
30
|
+
line.strip!
|
31
|
+
unless line.empty?
|
32
|
+
key, value = line.split("=", 2)
|
33
|
+
env[key] = value
|
34
|
+
end
|
35
|
+
end
|
36
|
+
env
|
37
|
+
end
|
38
|
+
|
39
|
+
def env_result
|
40
|
+
parse_env_file("/tmp/envs-test/.env")
|
41
|
+
end
|
42
|
+
|
43
|
+
def default_result
|
44
|
+
parse_env_file("/tmp/envs-test/.env.default")
|
45
|
+
end
|
46
|
+
|
47
|
+
def env(vals)
|
48
|
+
add_vals("/tmp/envs-test/.env", vals)
|
49
|
+
end
|
50
|
+
|
51
|
+
def default(vals)
|
52
|
+
add_vals("/tmp/envs-test/.env.default", vals)
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
def add_vals(file, vals)
|
57
|
+
File.open(file, "a") do |f|
|
58
|
+
vals.each do |k, v|
|
59
|
+
f << "#{k}=#{v}\n"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: envs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Josh Hull
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: trollop
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: highline
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rainbow
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: minitest
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Sync your .env and .env.default files
|
79
|
+
email:
|
80
|
+
- joshbuddy@gmail.com
|
81
|
+
executables:
|
82
|
+
- envs
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files: []
|
85
|
+
files:
|
86
|
+
- .env
|
87
|
+
- .gitignore
|
88
|
+
- Gemfile
|
89
|
+
- LICENSE.txt
|
90
|
+
- README.md
|
91
|
+
- Rakefile
|
92
|
+
- bin/envs
|
93
|
+
- lib/envs.rb
|
94
|
+
- lib/envs/cli.rb
|
95
|
+
- lib/envs/env_file.rb
|
96
|
+
- lib/envs/runner.rb
|
97
|
+
- lib/envs/version.rb
|
98
|
+
- ranger.gemspec
|
99
|
+
- spec/ranger_spec.rb
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
homepage: https://github.com/joshbuddy/envs
|
102
|
+
licenses: []
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
109
|
+
requirements:
|
110
|
+
- - ! '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ! '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 1.8.24
|
122
|
+
signing_key:
|
123
|
+
specification_version: 3
|
124
|
+
summary: Sync your .env and .env.default files.
|
125
|
+
test_files:
|
126
|
+
- spec/ranger_spec.rb
|
127
|
+
- spec/spec_helper.rb
|