varfile 0.0.1
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/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +45 -0
- data/Rakefile +1 -0
- data/bin/varfile +7 -0
- data/lib/varfile.rb +85 -0
- data/lib/varfile/version.rb +3 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/varfile/command_spec.rb +47 -0
- data/varfile.gemspec +21 -0
- metadata +77 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Fabrizio Regini
|
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,45 @@
|
|
1
|
+
# Varfile
|
2
|
+
|
3
|
+
Varfile is a little executable to write and read variables from a text file.
|
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
|
7
|
+
tools like capistrano.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'varfile'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install varfile
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```bash
|
26
|
+
$ varfile set FOO bar /u/app/shared/config/.env
|
27
|
+
```
|
28
|
+
|
29
|
+
```bash
|
30
|
+
$ varfile get FOO /u/app/shared/config/.env
|
31
|
+
bar
|
32
|
+
```
|
33
|
+
|
34
|
+
```bash
|
35
|
+
$ varfile list /u/app/shared/config/.env
|
36
|
+
FOO=bar
|
37
|
+
```
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
1. Fork it
|
42
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
43
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
44
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
45
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/varfile
ADDED
data/lib/varfile.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require "varfile/version"
|
2
|
+
require 'thor'
|
3
|
+
require 'pathname'
|
4
|
+
|
5
|
+
module Varfile
|
6
|
+
class Command < Thor
|
7
|
+
attr_reader :file_path
|
8
|
+
|
9
|
+
desc "set", "sets a key to file"
|
10
|
+
method_options :file => :string
|
11
|
+
def set(key, value)
|
12
|
+
file = file_or_default(options)
|
13
|
+
|
14
|
+
key = normalize_key(key)
|
15
|
+
value = normalize_value(value)
|
16
|
+
|
17
|
+
content = read_file(file)
|
18
|
+
content[key] = value
|
19
|
+
save_file(content, file)
|
20
|
+
end
|
21
|
+
|
22
|
+
desc "get", "get the value of key from file"
|
23
|
+
method_options :file => :string
|
24
|
+
def get(key)
|
25
|
+
file = file_or_default(options)
|
26
|
+
content = read_file(file)
|
27
|
+
puts content[key]
|
28
|
+
end
|
29
|
+
|
30
|
+
desc "list", "lists all keys to file"
|
31
|
+
method_options :file => :string
|
32
|
+
def list
|
33
|
+
file = file_or_default(options)
|
34
|
+
content = read_file(file)
|
35
|
+
puts printable_content(content)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def file_or_default(options)
|
41
|
+
file = options[:file] ? options[:file] : default_file
|
42
|
+
@file_path = Pathname.new(file)
|
43
|
+
end
|
44
|
+
|
45
|
+
def save_file(content, file)
|
46
|
+
file = File.open(file, 'w')
|
47
|
+
file.write printable_content(content)
|
48
|
+
file.close
|
49
|
+
end
|
50
|
+
|
51
|
+
def printable_content(content)
|
52
|
+
string = ""
|
53
|
+
content.each do |key, value|
|
54
|
+
string << "#{key}=#{value}\n"
|
55
|
+
end
|
56
|
+
string
|
57
|
+
end
|
58
|
+
|
59
|
+
def read_file(file)
|
60
|
+
content = {}
|
61
|
+
if file.exist?
|
62
|
+
File.new(file, 'r').readlines.each do |line|
|
63
|
+
next if line.strip == ''
|
64
|
+
|
65
|
+
key, value = line.split '='
|
66
|
+
content[key] = value.strip
|
67
|
+
end
|
68
|
+
end
|
69
|
+
content
|
70
|
+
end
|
71
|
+
|
72
|
+
def default_file
|
73
|
+
Pathname.new('Varfile')
|
74
|
+
end
|
75
|
+
|
76
|
+
def normalize_key(key)
|
77
|
+
key.strip.gsub('=', '').gsub("\n", '')
|
78
|
+
end
|
79
|
+
|
80
|
+
def normalize_value(value)
|
81
|
+
value.strip.gsub('=', '').gsub("\n", '')
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
|
8
|
+
require 'varfile'
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
12
|
+
config.run_all_when_everything_filtered = true
|
13
|
+
config.filter_run :focus
|
14
|
+
|
15
|
+
# Run specs in random order to surface order dependencies. If you find an
|
16
|
+
# order dependency and want to debug it, you can fix the order by providing
|
17
|
+
# the seed, which is printed after each run.
|
18
|
+
# --seed 1234
|
19
|
+
config.order = 'random'
|
20
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Varfile::Command do
|
4
|
+
let!(:file_path) { 'MyFile' }
|
5
|
+
|
6
|
+
after do
|
7
|
+
`rm -f #{file_path}`
|
8
|
+
`rm -f Varfile`
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should find correct file position' do
|
12
|
+
a = Varfile::Command.new
|
13
|
+
a.options = { file: file_path }
|
14
|
+
a.list
|
15
|
+
a.file_path.to_s.should == 'MyFile'
|
16
|
+
end
|
17
|
+
|
18
|
+
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'
|
22
|
+
end
|
23
|
+
|
24
|
+
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'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe 'list' do
|
32
|
+
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')
|
37
|
+
|
38
|
+
Varfile::Command.new.list.should == \
|
39
|
+
%Q{sport=racing
|
40
|
+
language=ruby
|
41
|
+
country=italy
|
42
|
+
diet=paleo
|
43
|
+
}.gsub(' ', '')
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/varfile.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'varfile/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "varfile"
|
8
|
+
gem.version = Varfile::VERSION
|
9
|
+
gem.authors = ["Fabrizio Regini"]
|
10
|
+
gem.email = ["freegenie@gmail.com"]
|
11
|
+
gem.description = %q{writes and reads key=value from file}
|
12
|
+
gem.summary = %q{writes and reads key=value from file}
|
13
|
+
gem.homepage = ""
|
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
|
+
gem.add_dependency "thor", "~> 0.16.0"
|
20
|
+
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: varfile
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Fabrizio Regini
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: thor
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.16.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.16.0
|
30
|
+
description: writes and reads key=value from file
|
31
|
+
email:
|
32
|
+
- freegenie@gmail.com
|
33
|
+
executables:
|
34
|
+
- varfile
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- .gitignore
|
39
|
+
- .rspec
|
40
|
+
- Gemfile
|
41
|
+
- LICENSE.txt
|
42
|
+
- README.md
|
43
|
+
- Rakefile
|
44
|
+
- bin/varfile
|
45
|
+
- lib/varfile.rb
|
46
|
+
- lib/varfile/version.rb
|
47
|
+
- spec/spec_helper.rb
|
48
|
+
- spec/varfile/command_spec.rb
|
49
|
+
- varfile.gemspec
|
50
|
+
homepage: ''
|
51
|
+
licenses: []
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.8.23
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: writes and reads key=value from file
|
74
|
+
test_files:
|
75
|
+
- spec/spec_helper.rb
|
76
|
+
- spec/varfile/command_spec.rb
|
77
|
+
has_rdoc:
|