envred 0.0.1 → 0.1.0

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
2
  SHA1:
3
- metadata.gz: f00ca1e95e16ed62ffb274022b449eb90889b99c
4
- data.tar.gz: 2003ad3e0932adc11a839867f39f2ec2f9139527
3
+ metadata.gz: 93212e392b8cf3ccff6398d91037ca897c1e63e9
4
+ data.tar.gz: 556de3856293cdd8ad8893093f78cbfca296c5ff
5
5
  SHA512:
6
- metadata.gz: b18f757f9c4beacf403a2c30564df0ff10e35ad5e0a7e3da181b15c2877c48f4870525965a1629b39192ccfe0bcd077195fef4f7e428bcbf3f2f3ca7439acf3d
7
- data.tar.gz: 7309507e424ad2190145566d4318339e54bda3eb9d5781041951355d7e223f612b21485262eabc3a855ecbefdf80a65c908f471b2316f9a3430718207362e6f3
6
+ metadata.gz: 37450b7ac0190fc8b55ee69ff7ba5cbf2c7e5ced5c56a480957fcef2c21ed9fa03f5039fead20752b30326d51c28236dc6c99d16192b1650e46a97970c41c052
7
+ data.tar.gz: 6a76c3fcc701975bbda8227f3a85396203c987f6ed75e20f829193de905c5079834c238456a67fb39dcc49eca4b1ac1c41667d915712f97e5f01fec0ce8e8b59
data/README.md CHANGED
@@ -14,14 +14,27 @@ Install via rubygems:
14
14
 
15
15
  ## Usage
16
16
 
17
- Run application with environment loaded from Redis server:
17
+ Set some variables first:
18
18
 
19
- $ envred localhost:6379/0/~myapp myapp --do something
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
23
23
  the data as hash.
24
24
 
25
+ Now you can run any application with environment loaded from Redis server:
26
+
27
+ $ envred localhost:6379/0/~myapp myapp --do something
28
+
29
+ To list all your variables use:
30
+
31
+ $ envred localhost:6379/0/~myapp --list
32
+
33
+ You can remove specific keys or purge all config:
34
+
35
+ $ envred localhost:6379/0/~myapp --unset FOO BAR
36
+ $ envred localhost:6379/0/~myapp --purge
37
+
25
38
  ## Hacking
26
39
 
27
40
  If you wanna hack on the repo for some reason, first clone it, then run:
data/bin/envred CHANGED
@@ -3,18 +3,68 @@
3
3
  require 'rubygems'
4
4
  require 'envred'
5
5
 
6
+ $app_name = 'envred'
7
+
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
13
+ end
14
+
15
+ def safe
16
+ yield
17
+ rescue => err
18
+ $stderr.write("ERROR: #{err.to_s}\n")
19
+ exit(146)
20
+ end
21
+
6
22
  case ARGV[0]
7
23
  when "-h", "--help"
8
- puts "usage: envred [-h|--help] [CENTRAL] [COMMAND]"
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]"
9
30
  exit
10
31
  when "-v", "--version"
11
- puts "envred v#{Envred::VERSION}"
32
+ puts "#{$app_name} v#{Envred::VERSION}"
12
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!"
40
+ 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)
46
+ puts "Ok!"
47
+ 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}"
54
+ end
55
+ end
56
+ when "-p", "--purge"
57
+ ARGV.shift
58
+ validate_argc(ARGV.size < 1)
59
+ safe do
60
+ Envred::Purgator.new(ARGV.shift).purge
61
+ puts "Ok!"
62
+ 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
69
+ end
13
70
  end
14
-
15
- if ARGV.size < 2
16
- $stderr.write("ERROR: Invalid parameters. Run with `-h' flag for help.\n")
17
- exit(1)
18
- end
19
-
20
- Envred.wrap(ARGV.shift, ARGV.join(" "))
@@ -2,28 +2,48 @@ require "envred/version"
2
2
  require "redis"
3
3
 
4
4
  module Envred
5
- class Wrapper
5
+ class Central
6
6
  def initialize(central)
7
7
  @server, @app = central.split("/~")
8
8
  @redis = Redis.new(url: "redis://#{@server}")
9
-
10
- load and yield if block_given?
11
9
  end
10
+ end
12
11
 
12
+ class Loader < Central
13
13
  def load
14
- vars = @redis.hgetall(@app) and vars.each do |key, val|
15
- if ENV[key] === nil
16
- ENV[key] ||= val
17
- elsif val == ""
14
+ @redis.hgetall(@app)
15
+ end
16
+
17
+ def each(*args, &block)
18
+ load.each(*args, &block)
19
+ end
20
+
21
+ def apply
22
+ each do |key, val|
23
+ if val == ''
18
24
  ENV.delete(key)
25
+ else
26
+ ENV[key] = val if ENV[key] === nil
19
27
  end
20
28
  end
29
+
30
+ yield if block_given?
31
+ end
32
+ end
33
+
34
+ class Setter < Central
35
+ def set(*values)
36
+ @redis.hmset(@app, *values)
37
+ end
38
+
39
+ def unset(*keys)
40
+ @redis.hdel(@app, *keys)
21
41
  end
22
42
  end
23
43
 
24
- def self.wrap(central, command)
25
- Wrapper.new(central) do
26
- system command
44
+ class Purgator < Central
45
+ def purge
46
+ @redis.del(@app)
27
47
  end
28
48
  end
29
49
  end
@@ -1,3 +1,3 @@
1
1
  module Envred
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,6 +1,6 @@
1
1
  require File.expand_path("../spec_helper.rb", __FILE__)
2
2
 
3
- describe Envred do
3
+ describe Envred::Loader do
4
4
  let(:server) do
5
5
  ENV['REDIS_URL'].split('redis://').last.split('/').first
6
6
  end
@@ -13,22 +13,47 @@ describe Envred do
13
13
  Redis.new(url: "redis://#{server}/0")
14
14
  end
15
15
 
16
- describe Envred::Wrapper do
17
- let(:setenv) do
18
- { foo: 1, bar: 2 }
16
+ let(:setenv) do
17
+ { foo: 1, bar: 2 }
18
+ end
19
+
20
+ before do
21
+ redis.mapped_hmset("test", setenv)
22
+ end
23
+
24
+ describe "#load" do
25
+ 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
+
41
+ all.should have(2).items
42
+ all['foo'].should == '1'
43
+ all['bar'].should == '2'
19
44
  end
45
+ end
20
46
 
47
+ describe "#apply" do
21
48
  before do
22
49
  ENV.delete('foo')
23
50
  ENV.delete('bar')
24
-
25
- redis.mapped_hmset("test", setenv)
26
51
  end
27
52
 
28
53
  it "should load proper configuration and set env" do
29
54
  env = nil
30
55
 
31
- Envred::Wrapper.new(central) do
56
+ Envred::Loader.new(central).apply do
32
57
  env = ENV
33
58
  end
34
59
 
@@ -39,7 +64,7 @@ describe Envred do
39
64
  it "should property pass env to commands" do
40
65
  res = nil
41
66
 
42
- Envred::Wrapper.new(central) do
67
+ Envred::Loader.new(central).apply do
43
68
  res = `echo $foo $bar`.strip
44
69
  end
45
70
 
@@ -59,7 +84,7 @@ describe Envred do
59
84
  it "should not change existing keys" do
60
85
  res = nil
61
86
 
62
- Envred::Wrapper.new(central) do
87
+ Envred::Loader.new(central).apply do
63
88
  res = `echo $foo $bar`.strip
64
89
  end
65
90
 
@@ -69,7 +94,7 @@ describe Envred do
69
94
  it "should remove key if value is empty" do
70
95
  res = nil
71
96
 
72
- Envred::Wrapper.new(central) do
97
+ Envred::Loader.new(central).apply do
73
98
  res = `echo $foo $bar $baz`.strip
74
99
  end
75
100
 
@@ -0,0 +1,34 @@
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
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.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kris Kovalik
@@ -84,7 +84,8 @@ files:
84
84
  - envred.gemspec
85
85
  - lib/envred.rb
86
86
  - lib/envred/version.rb
87
- - spec/envred_spec.rb
87
+ - spec/loader_spec.rb
88
+ - spec/setter_spec.rb
88
89
  - spec/spec_helper.rb
89
90
  - wercker.yml
90
91
  homepage: https://github.com/kkvlk/envred
@@ -112,5 +113,6 @@ signing_key:
112
113
  specification_version: 4
113
114
  summary: Redis backend env setup proxy.
114
115
  test_files:
115
- - spec/envred_spec.rb
116
+ - spec/loader_spec.rb
117
+ - spec/setter_spec.rb
116
118
  - spec/spec_helper.rb