varfile 0.0.1 → 0.0.2

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.
data/README.md CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
3
  Varfile is a little executable to write and read variables from a text file.
4
4
  It's job is trivial, but the little automation becomes very handy when dealing
5
- distributed environments. Varfile is suitable to be used as a tool for
6
- configuration of remote servers when used in conjunction with other automation
5
+ distributed environments. Varfile is suitable to be used as a tool for
6
+ configuration of remote servers when used in conjunction with other automation
7
7
  tools like capistrano.
8
8
 
9
9
  ## Installation
@@ -22,20 +22,32 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- ```bash
26
- $ varfile set FOO bar /u/app/shared/config/.env
25
+ ```bash
26
+ $ varfile set FOO bar --file=/u/app/shared/config/.env
27
27
  ```
28
28
 
29
- ```bash
30
- $ varfile get FOO /u/app/shared/config/.env
29
+ ```bash
30
+ $ varfile get FOO --file=/u/app/shared/config/.env
31
31
  bar
32
32
  ```
33
33
 
34
- ```bash
35
- $ varfile list /u/app/shared/config/.env
34
+ ```bash
35
+ $ varfile list --file=/u/app/shared/config/.env
36
36
  FOO=bar
37
37
  ```
38
38
 
39
+ ```bash
40
+ $ varfile rm FOO --file=/u/app/shared/config/.env
41
+ ```
42
+
43
+ ```bash
44
+ $ varfile list --file=/u/app/shared/config/.env
45
+ $
46
+ ```
47
+
48
+ If `--file` option is missing, variables will be written to a file named
49
+ `Varfile`.
50
+
39
51
  ## Contributing
40
52
 
41
53
  1. Fork it
@@ -5,6 +5,7 @@ require 'pathname'
5
5
  module Varfile
6
6
  class Command < Thor
7
7
  attr_reader :file_path
8
+ attr_accessor :output
8
9
 
9
10
  desc "set", "sets a key to file"
10
11
  method_options :file => :string
@@ -24,7 +25,7 @@ module Varfile
24
25
  def get(key)
25
26
  file = file_or_default(options)
26
27
  content = read_file(file)
27
- puts content[key]
28
+ puts_and_return content[key]
28
29
  end
29
30
 
30
31
  desc "list", "lists all keys to file"
@@ -32,11 +33,31 @@ module Varfile
32
33
  def list
33
34
  file = file_or_default(options)
34
35
  content = read_file(file)
35
- puts printable_content(content)
36
+ puts_and_return printable_content(content)
37
+ end
38
+
39
+ desc "rm", "remove a key from file"
40
+ method_options :file => :string
41
+ def rm(key)
42
+ file = file_or_default(options)
43
+ content = read_file(file)
44
+ content.delete key
45
+ save_file(content, file)
46
+ end
47
+
48
+ no_tasks do
49
+ def output
50
+ @output ||= STDOUT
51
+ end
36
52
  end
37
53
 
38
54
  private
39
55
 
56
+ def puts_and_return(text)
57
+ output.puts text
58
+ text
59
+ end
60
+
40
61
  def file_or_default(options)
41
62
  file = options[:file] ? options[:file] : default_file
42
63
  @file_path = Pathname.new(file)
@@ -1,3 +1,3 @@
1
1
  module Varfile
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,6 +1,33 @@
1
1
  require 'spec_helper'
2
2
 
3
+ class TestOutput
4
+ def puts(string)
5
+ @output ||= []
6
+ @output << string
7
+ end
8
+
9
+ def flush
10
+ format @output.slice!(0, @output.count)
11
+ end
12
+
13
+ def inspect
14
+ format @output
15
+ end
16
+
17
+ def format(array)
18
+ array.join "\n"
19
+ end
20
+
21
+ end
22
+
3
23
  describe Varfile::Command do
24
+
25
+ subject {
26
+ a = Varfile::Command.new
27
+ a.output = TestOutput.new
28
+ a
29
+ }
30
+
4
31
  let!(:file_path) { 'MyFile' }
5
32
 
6
33
  after do
@@ -9,33 +36,41 @@ describe Varfile::Command do
9
36
  end
10
37
 
11
38
  it 'should find correct file position' do
12
- a = Varfile::Command.new
39
+ a = subject
13
40
  a.options = { file: file_path }
14
41
  a.list
15
42
  a.file_path.to_s.should == 'MyFile'
16
43
  end
17
44
 
18
45
  describe 'get and set' do
19
- it 'should set key to file' do
20
- Varfile::Command.new.set('foo', 'bar')
21
- Varfile::Command.new.get('foo').should == 'bar'
46
+ it 'should set key to file' do
47
+ subject.set('foo', 'bar')
48
+ test = subject
49
+
50
+ test.get('foo')
51
+ test.output.inspect.should == 'bar'
22
52
  end
23
53
 
24
54
  it 'should overwrite value if key is present' do
25
- Varfile::Command.new.set('foo', 'bar')
26
- Varfile::Command.new.set('foo', 'baz')
27
- Varfile::Command.new.get('foo').should == 'baz'
55
+ subject.set('foo', 'bar')
56
+ subject.set('foo', 'baz')
57
+ test = subject
58
+
59
+ test.get('foo')
60
+ test.output.inspect.should == 'baz'
28
61
  end
29
62
  end
30
63
 
31
64
  describe 'list' do
32
65
  it 'should list content of file' do
33
- Varfile::Command.new.set('sport', 'racing')
34
- Varfile::Command.new.set('language', 'ruby')
35
- Varfile::Command.new.set('country', 'italy')
36
- Varfile::Command.new.set('diet', 'paleo')
66
+ subject.set('sport', 'racing')
67
+ subject.set('language', 'ruby')
68
+ subject.set('country', 'italy')
69
+ subject.set('diet', 'paleo')
37
70
 
38
- Varfile::Command.new.list.should == \
71
+ test = subject
72
+ test.list
73
+ test.output.inspect.should == \
39
74
  %Q{sport=racing
40
75
  language=ruby
41
76
  country=italy
@@ -44,4 +79,18 @@ describe Varfile::Command do
44
79
 
45
80
  end
46
81
  end
82
+
83
+ describe 'rm' do
84
+ it 'should remove variable from file' do
85
+ subject.set('sport', 'racing')
86
+ test = subject
87
+ test.list
88
+ test.output.flush.should == "sport=racing\n"
89
+
90
+ subject.rm('sport')
91
+
92
+ test.list
93
+ test.output.flush.should == ""
94
+ end
95
+ end
47
96
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: varfile
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: