envred 0.1.0 → 0.2.0

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
  SHA1:
3
- metadata.gz: 93212e392b8cf3ccff6398d91037ca897c1e63e9
4
- data.tar.gz: 556de3856293cdd8ad8893093f78cbfca296c5ff
3
+ metadata.gz: 97af96bcff96f52e3fc3e7187baf47ce0ceb7ed8
4
+ data.tar.gz: 2c060c0b6fddb7de2edf675af9a733f9f2d1372b
5
5
  SHA512:
6
- metadata.gz: 37450b7ac0190fc8b55ee69ff7ba5cbf2c7e5ced5c56a480957fcef2c21ed9fa03f5039fead20752b30326d51c28236dc6c99d16192b1650e46a97970c41c052
7
- data.tar.gz: 6a76c3fcc701975bbda8227f3a85396203c987f6ed75e20f829193de905c5079834c238456a67fb39dcc49eca4b1ac1c41667d915712f97e5f01fec0ce8e8b59
6
+ metadata.gz: 69ae0ee5f98509150b515b84a590d099d1aa1b0a46af7321e37228e4025a34586fbfb3500baff4255048bb21dcf38f589665d4e74af113956030a93ff616eb2d
7
+ data.tar.gz: a36fa4a1fb622b70adb4a659a1cd6f210f7c0ef17f0d22df6324fe39ec2847ea39176b3e56f4e6680c4fe97e2a8779200f8f870500aeeaa4928e8abd57ce194b
data/README.md CHANGED
@@ -16,7 +16,7 @@ Install via rubygems:
16
16
 
17
17
  Set some variables first:
18
18
 
19
- $ envred localhost:6379/0/~myapp --set FOO=1 BAR=2 BAZ=3
19
+ $ envred localhost:6379/0/~myapp set FOO 1 BAR 2 BAZ 3
20
20
 
21
21
  The `localhost:6379` is host anf port of Redis server, `0` is the number of
22
22
  the database to load stuff from, finally, `myapp` is the key which keeps
@@ -24,16 +24,16 @@ the data as hash.
24
24
 
25
25
  Now you can run any application with environment loaded from Redis server:
26
26
 
27
- $ envred localhost:6379/0/~myapp myapp --do something
27
+ $ envred localhost:6379/0/~myapp wrap myapp --do something
28
28
 
29
29
  To list all your variables use:
30
30
 
31
- $ envred localhost:6379/0/~myapp --list
31
+ $ envred localhost:6379/0/~myapp list
32
32
 
33
33
  You can remove specific keys or purge all config:
34
34
 
35
- $ envred localhost:6379/0/~myapp --unset FOO BAR
36
- $ envred localhost:6379/0/~myapp --purge
35
+ $ envred localhost:6379/0/~myapp unset FOO BAR
36
+ $ envred localhost:6379/0/~myapp purge
37
37
 
38
38
  ## Hacking
39
39
 
data/bin/envred CHANGED
@@ -2,69 +2,105 @@
2
2
 
3
3
  require 'rubygems'
4
4
  require 'envred'
5
+ require 'gli'
5
6
 
6
- $app_name = 'envred'
7
+ include GLI::App
7
8
 
8
- def validate_argc(condition)
9
- if condition
10
- $stderr.write("ERROR: Invalid parameters. Run with `-h' flag for help.\n")
11
- exit(1)
12
- end
9
+ program_desc "Environment configuration backed with Redis."
10
+
11
+ desc "The URL of central env server."
12
+ flag [:c, :central], :default_value => ENV['ENVRED_CENTRAL']
13
+
14
+ pre do |globals, command, options, args|
15
+ $envred = Envred.new(globals[:central])
13
16
  end
14
17
 
15
- def safe
16
- yield
17
- rescue => err
18
- $stderr.write("ERROR: #{err.to_s}\n")
19
- exit(146)
18
+ desc "Wrap command with loaded env variables."
19
+ long_desc "Executes given command previously setting all loaded env variables."
20
+ command :wrap do |c|
21
+ c.action do |globals, options, args|
22
+ if args.empty?
23
+ help_now!('you must specify command to run')
24
+ end
25
+
26
+ $envred.apply do
27
+ exit(system(args.join(" ")))
28
+ end
29
+ end
20
30
  end
21
31
 
22
- case ARGV[0]
23
- when "-h", "--help"
24
- puts "usage: #{$app_name} CENTRAL COMMAND"
25
- puts " #{$app_name} [-s|--set] CENTRAL KEY VALUE [KEY VALUE...]"
26
- puts " #{$app_name} [-u|--unset] CENTRAL KEY [KEY...]"
27
- puts " #{$app_name} [-l|--list] CENTRAL"
28
- puts " #{$app_name} [-p|--purge] CENTRAL"
29
- puts " #{$app_name} [-h|--help]"
30
- exit
31
- when "-v", "--version"
32
- puts "#{$app_name} v#{Envred::VERSION}"
33
- exit
34
- when "-s", "--set"
35
- ARGV.shift
36
- validate_argc(ARGV.size < 3)
37
- safe do
38
- Envred::Setter.new(ARGV.shift).set(*ARGV)
39
- puts "Ok!"
32
+ desc "List variables."
33
+ long_desc "Display list of stored variables."
34
+ command :list do |c|
35
+ c.action do |globals, options, args|
36
+ print "Loading all variables..."
37
+
38
+ all = $envred.load
39
+
40
+ print "\r" + (" " * 40) + "\r"
41
+
42
+ if all.empty?
43
+ puts "No variables to show!"
44
+ else
45
+ all.each do |key, val|
46
+ puts "#{key}=#{val.inspect}"
47
+ end
48
+ end
40
49
  end
41
- when "-u", "--unset"
42
- ARGV.shift
43
- validate_argc(ARGV.size < 2)
44
- safe do
45
- Envred::Setter.new(ARGV.shift).unset(*ARGV)
50
+ end
51
+
52
+ desc "Set env variables."
53
+ long_desc "Assigns values to one or more env variale."
54
+ command :set do |c|
55
+ c.action do |globals, options, args|
56
+ if args.size % 2 != 0
57
+ help_now!("mismatched number of key-value pair arguments")
58
+ elsif args.size < 2
59
+ help_now!("you must specify at leat one key-value pair")
60
+ end
61
+
62
+ print "Saving variables... "
63
+
64
+ $envred.set(*args)
65
+
46
66
  puts "Ok!"
47
67
  end
48
- when "-l", "--list"
49
- ARGV.shift
50
- validate_argc(ARGV.size < 1)
51
- safe do
52
- Envred::Loader.new(ARGV.shift).each do |key, val|
53
- puts "#{key}=#{val.inspect}"
68
+ end
69
+
70
+ desc "Remove variables."
71
+ long_desc "Removes one or more env variable."
72
+ command :unset do |c|
73
+ c.action do |globals, options, args|
74
+ if args.empty?
75
+ help_now!("you must specify at leat one key to remove")
54
76
  end
77
+
78
+ print "Removing variables... "
79
+
80
+ $envred.unset(*args)
81
+
82
+ puts "Ok!"
55
83
  end
56
- when "-p", "--purge"
57
- ARGV.shift
58
- validate_argc(ARGV.size < 1)
59
- safe do
60
- Envred::Purgator.new(ARGV.shift).purge
84
+ end
85
+
86
+ desc "Delete all variables."
87
+ long_desc "Removes all stored variables from the project."
88
+ command :purge do |c|
89
+ c.action do |globals, options, args|
90
+ print "Purging variables list... "
91
+
92
+ $envred.purge
93
+
61
94
  puts "Ok!"
62
95
  end
63
- else
64
- validate_argc(ARGV.size < 2)
65
- safe do
66
- Envred::Loader.new(ARGV.shift).apply do
67
- system ARGV.join(" ")
68
- end
96
+ end
97
+
98
+ desc "Display current version."
99
+ long_desc "Displays current version number."
100
+ command :version do |c|
101
+ c.action do |globals, options, args|
102
+ puts "envred v#{Envred::VERSION}"
69
103
  end
70
104
  end
105
+
106
+ exit run(ARGV)
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rspec", "~> 2.5"
23
23
  spec.add_development_dependency "rake"
24
24
  spec.add_dependency "redis", "~> 3.0"
25
+ spec.add_dependency "gli", "~> 2.9"
25
26
  end
@@ -1,49 +1,37 @@
1
1
  require "envred/version"
2
2
  require "redis"
3
3
 
4
- module Envred
5
- class Central
6
- def initialize(central)
7
- @server, @app = central.split("/~")
8
- @redis = Redis.new(url: "redis://#{@server}")
9
- end
4
+ class Envred
5
+ def initialize(central)
6
+ @server, @app = central.split("/~")
7
+ @redis = Redis.new(url: "redis://#{@server}")
10
8
  end
11
9
 
12
- class Loader < Central
13
- def load
14
- @redis.hgetall(@app)
15
- end
16
-
17
- def each(*args, &block)
18
- load.each(*args, &block)
19
- end
10
+ def load
11
+ @redis.hgetall(@app) or []
12
+ end
20
13
 
21
- def apply
22
- each do |key, val|
23
- if val == ''
24
- ENV.delete(key)
25
- else
26
- ENV[key] = val if ENV[key] === nil
27
- end
14
+ def apply
15
+ load.each do |key, val|
16
+ if val == ''
17
+ ENV.delete(key)
18
+ else
19
+ ENV[key] = val if ENV[key] === nil
28
20
  end
29
-
30
- yield if block_given?
31
21
  end
22
+
23
+ yield if block_given?
32
24
  end
33
25
 
34
- class Setter < Central
35
- def set(*values)
36
- @redis.hmset(@app, *values)
37
- end
26
+ def set(*values)
27
+ @redis.hmset(@app, *values)
28
+ end
38
29
 
39
- def unset(*keys)
40
- @redis.hdel(@app, *keys)
41
- end
30
+ def unset(*keys)
31
+ @redis.hdel(@app, keys)
42
32
  end
43
33
 
44
- class Purgator < Central
45
- def purge
46
- @redis.del(@app)
47
- end
34
+ def purge
35
+ @redis.del(@app)
48
36
  end
49
37
  end
@@ -1,3 +1,3 @@
1
- module Envred
2
- VERSION = "0.1.0"
1
+ class Envred
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,6 +1,6 @@
1
1
  require File.expand_path("../spec_helper.rb", __FILE__)
2
2
 
3
- describe Envred::Loader do
3
+ describe Envred do
4
4
  let(:server) do
5
5
  ENV['REDIS_URL'].split('redis://').last.split('/').first
6
6
  end
@@ -18,26 +18,13 @@ describe Envred::Loader do
18
18
  end
19
19
 
20
20
  before do
21
+ redis.flushdb()
21
22
  redis.mapped_hmset("test", setenv)
22
23
  end
23
24
 
24
25
  describe "#load" do
25
26
  it "loads list of set variables" do
26
- all = Envred::Loader.new(central).load
27
- all.should have(2).items
28
- all['foo'].should == '1'
29
- all['bar'].should == '2'
30
- end
31
- end
32
-
33
- describe "#load" do
34
- it "loads list of set variables and produces an iterator for it" do
35
- all = {}
36
-
37
- Envred::Loader.new(central).each do |key, val|
38
- all[key] = val
39
- end
40
-
27
+ all = Envred.new(central).load
41
28
  all.should have(2).items
42
29
  all['foo'].should == '1'
43
30
  all['bar'].should == '2'
@@ -53,7 +40,7 @@ describe Envred::Loader do
53
40
  it "should load proper configuration and set env" do
54
41
  env = nil
55
42
 
56
- Envred::Loader.new(central).apply do
43
+ Envred.new(central).apply do
57
44
  env = ENV
58
45
  end
59
46
 
@@ -64,7 +51,7 @@ describe Envred::Loader do
64
51
  it "should property pass env to commands" do
65
52
  res = nil
66
53
 
67
- Envred::Loader.new(central).apply do
54
+ Envred.new(central).apply do
68
55
  res = `echo $foo $bar`.strip
69
56
  end
70
57
 
@@ -84,7 +71,7 @@ describe Envred::Loader do
84
71
  it "should not change existing keys" do
85
72
  res = nil
86
73
 
87
- Envred::Loader.new(central).apply do
74
+ Envred.new(central).apply do
88
75
  res = `echo $foo $bar`.strip
89
76
  end
90
77
 
@@ -94,7 +81,7 @@ describe Envred::Loader do
94
81
  it "should remove key if value is empty" do
95
82
  res = nil
96
83
 
97
- Envred::Loader.new(central).apply do
84
+ Envred.new(central).apply do
98
85
  res = `echo $foo $bar $baz`.strip
99
86
  end
100
87
 
@@ -103,4 +90,19 @@ describe Envred::Loader do
103
90
  end
104
91
  end
105
92
  end
93
+
94
+ describe "#set" do
95
+ it "should save given variable if non empty" do
96
+ Envred.new(central).set("foo", 1)
97
+ redis.hget("test", "foo").should == "1"
98
+ end
99
+ end
100
+
101
+ describe "#unset" do
102
+ it "should save given variable if non empty" do
103
+ Envred.new(central).set("foo", 1)
104
+ Envred.new(central).unset("foo")
105
+ redis.hget("test", "foo").should == nil
106
+ end
107
+ end
106
108
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: envred
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kris Kovalik
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: gli
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.9'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.9'
69
83
  description: This is a tool/wrapper for the apps that set env configuration from given
70
84
  redis machine.
71
85
  email:
@@ -84,8 +98,7 @@ files:
84
98
  - envred.gemspec
85
99
  - lib/envred.rb
86
100
  - lib/envred/version.rb
87
- - spec/loader_spec.rb
88
- - spec/setter_spec.rb
101
+ - spec/envred_spec.rb
89
102
  - spec/spec_helper.rb
90
103
  - wercker.yml
91
104
  homepage: https://github.com/kkvlk/envred
@@ -113,6 +126,5 @@ signing_key:
113
126
  specification_version: 4
114
127
  summary: Redis backend env setup proxy.
115
128
  test_files:
116
- - spec/loader_spec.rb
117
- - spec/setter_spec.rb
129
+ - spec/envred_spec.rb
118
130
  - spec/spec_helper.rb
@@ -1,34 +0,0 @@
1
- require File.expand_path("../spec_helper.rb", __FILE__)
2
-
3
- describe Envred::Setter do
4
- let(:server) do
5
- ENV['REDIS_URL'].split('redis://').last.split('/').first
6
- end
7
-
8
- let(:central) do
9
- "#{server}/0/~test"
10
- end
11
-
12
- let(:redis) do
13
- Redis.new(url: "redis://#{server}/0")
14
- end
15
-
16
- before do
17
- redis.flushdb()
18
- end
19
-
20
- describe "#set" do
21
- it "should save given variable if non empty" do
22
- Envred::Setter.new(central).set("foo", 1)
23
- redis.hget("test", "foo").should == "1"
24
- end
25
- end
26
-
27
- describe "#unset" do
28
- it "should save given variable if non empty" do
29
- Envred::Setter.new(central).set("foo", 1)
30
- Envred::Setter.new(central).unset("foo")
31
- redis.hget("test", "foo").should == nil
32
- end
33
- end
34
- end