benzo 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.8.7
5
+ - ree
6
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in benzo.gemspec
4
+ gemspec
5
+
6
+ gem 'awesome_print'
7
+ gem 'rspec'
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Spike Grobstein
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,117 @@
1
+ # Benzo [![Build Status](https://travis-ci.org/spikegrobstein/benzo.png)](https://travis-ci.org/spikegrobstein/benzo)
2
+
3
+ *Take the edge off when doing (command) lines.*
4
+
5
+ Wrapping [Cocaine](https://github.com/thoughtbot/cocaine), this library will
6
+ greatly simplify building complex and conditional commandline arguments.
7
+
8
+ This is especially useful when creating wrappers for other commandline tools.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'benzo'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install benzo
23
+
24
+ ## Quickstart Example
25
+
26
+ Given that you want to make a call to `pg_dump`, but depending on some conditions
27
+ in your code, you may want to have certain arguments shown or not shown:
28
+
29
+ ```ruby
30
+ # the state of our app:
31
+ @verbose = true
32
+ @database = 'app_production'
33
+ @file = 'app_prod-dump'
34
+
35
+ # build the Cocaine::Commandline with Benzo
36
+ line = Benzo.line('pg_dump', {
37
+ '-v' => @verbose,
38
+ '--schema-only' => @schema_only, # note, @schema_only is nil
39
+ '-f :filename' => @file,
40
+ ':db_name' => @database
41
+ })
42
+
43
+ line.run # execute the command
44
+ ```
45
+
46
+ Benzo takes 2 arguments: `command` and `options_map`. The command is, like in
47
+ `cocaine`, the command you wish to run, and `options_map` is a hash containing
48
+ the data necessary to build the commandline arguments.
49
+
50
+ Any value in the hash that evaluates to `false` (this includes `nil`) will be
51
+ omitted from the command.
52
+
53
+ ## How options_map works
54
+
55
+ `options_map` is a hash that gets processed by `Benzo` to create the
56
+ `Cocaine::CommandLine` object. The hash gets iterated over and any values that
57
+ evaluate to `false` are discarded, then the keys are concatinated together with
58
+ spaces to create the commandline arguments that are passed to `cocaine`. Any
59
+ symbols embedded in those strings will be used to build the variables to be
60
+ interpolated by `cocaine`.
61
+
62
+ Given the following `options_map`:
63
+
64
+ ```ruby
65
+ {
66
+ '-f :file' => "file.dat",
67
+ "-v" => true,
68
+ "-d" => false,
69
+ ":data" => nil
70
+ }
71
+ ```
72
+
73
+ The resulting command's arguments will be built as `-f 'file.dat' -v` omitting the
74
+ `-d` and `:data` values because the values of the hash evaluated to false.
75
+
76
+ When `Benzo` looks at the keys, it tries to find a symbol (a string leading with
77
+ a `:`). It will then use that as the key for whatever value it points to when
78
+ building the commandline. For example, `'-f :file' => 'file.dat'` will set `:file`
79
+ to `'file.dat'` when passing it to `cocaine`.
80
+
81
+ You can also pass a symbol as a key directly to the `options_map`, which will be
82
+ passed directly to `cocaine` when building the commandline. This is useful if you
83
+ want to use the `Logger` or `:expected_outcodes` facilities in `cocaine`. For
84
+ example (modified from one of the `cocaine` examples):
85
+
86
+ ```ruby
87
+ line = Benzo.line('/usr/bin/false', {
88
+ :expected_outcodes => [ 0, 1 ]
89
+ })
90
+
91
+ begin
92
+ line.run
93
+ rescue Cocaine::ExitStatusError => e
94
+ # => You never get here!
95
+ end
96
+ ```
97
+
98
+ ## Contributing
99
+
100
+ 1. Fork it
101
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
102
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
103
+ 4. Push to the branch (`git push origin my-new-feature`)
104
+ 5. Create new Pull Request
105
+
106
+ ## Acknowledgements
107
+
108
+ Benzo is written by Spike Grobstein, and wraps [Cocaine](https://github.com/thoughtbot/cocaine) by
109
+ [Thoughtbot](http://www.thoughtbot.com)
110
+
111
+ ## Author
112
+
113
+ Spike Grobstein
114
+ me@spike.cx
115
+ http://spike.grobste.in
116
+ https://github.com/spikegrobstein
117
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/benzo.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/benzo/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Spike Grobstein"]
6
+ gem.email = ["me@spike.cx"]
7
+ gem.description = %q{Take the edge off when doing (command) lines.}
8
+ gem.summary = %q{A robust mapper for complex commandline calls using cocaine.}
9
+ gem.homepage = "https://github.com/spikegrobstein/benzo"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "benzo"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Benzo::VERSION
17
+
18
+ gem.add_dependency 'cocaine'
19
+ end
@@ -0,0 +1,3 @@
1
+ class Benzo
2
+ VERSION = "1.0.0"
3
+ end
data/lib/benzo.rb ADDED
@@ -0,0 +1,68 @@
1
+ require "benzo/version"
2
+ require 'cocaine'
3
+
4
+ class Benzo
5
+
6
+ attr_accessor :command, :options_map
7
+ attr_accessor :line, :vars
8
+
9
+ def initialize(command, options_map)
10
+ @command = command
11
+ @options_map = options_map
12
+ @line = []
13
+ @vars = {}
14
+
15
+ map!
16
+ end
17
+
18
+ # convenience method for creating a new Benzo object with the given
19
+ # arguments and returning the Cocaine::Commandline
20
+ def self.line(command, options_map)
21
+ b = new(command, options_map)
22
+ line = b.to_cocaine
23
+
24
+ b = nil
25
+ line
26
+ end
27
+
28
+ # convert this object into a Cocaine::CommandLine
29
+ def to_cocaine
30
+ ::Cocaine::CommandLine.new(@command, @line.join(' '), @vars)
31
+ end
32
+
33
+ private
34
+
35
+ # iterate over the options map and build
36
+ # this object's mapping.
37
+ def map!
38
+ @options_map.each do |k,v|
39
+ sym = if k.is_a? Symbol
40
+ k
41
+ else
42
+ @line << k if v
43
+ sym = get_symbol(k)
44
+ end
45
+
46
+ if sym
47
+ @vars[sym] = v
48
+ end
49
+ end
50
+ end
51
+
52
+ # given a string, pull out the symbol
53
+ # this is used to get the key for the variable to pass to cocaine.
54
+ # returns nil if no symbol found
55
+ # raises an ArgumentError if the symbol isn't well-formed (has bad chars)
56
+ def get_symbol(str)
57
+ m = str.match /:(\S+)/
58
+
59
+ return nil unless m
60
+ sym = m[1]
61
+
62
+ raise ArgumentError if sym.match(/[^a-z0-9_]/i)
63
+
64
+ :"#{sym}"
65
+ end
66
+
67
+
68
+ end
@@ -0,0 +1,91 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Benzo do
4
+
5
+ let(:benzo) { Benzo.new 't', {} }
6
+
7
+ context "#map!" do
8
+
9
+ it "should append to @line if v evaluates to true" do
10
+ benzo.options_map = { ':file_name' => 'demo.dat' }
11
+ benzo.send(:map!)
12
+
13
+ benzo.line.count.should == 1
14
+ benzo.line.first.should == ':file_name'
15
+ end
16
+
17
+ it "should not append to @line if v is nil" do
18
+ benzo.options_map = { ':file_name' => nil }
19
+ benzo.send(:map!)
20
+
21
+ benzo.line.count.should == 0
22
+ end
23
+
24
+ it "should not append to @line if v is false" do
25
+ benzo.options_map = { ':file_name' => false }
26
+ benzo.send(:map!)
27
+
28
+ benzo.line.count.should == 0
29
+ end
30
+
31
+ it "should add to @vars if there's a symbol in the string" do
32
+ benzo.options_map = { ':file_name' => 'demo.dat' }
33
+ benzo.send(:map!)
34
+
35
+ benzo.vars.keys.count.should == 1
36
+ benzo.vars.keys.first.should == :file_name
37
+ end
38
+
39
+ it "should not add to @vars if there's no symbol in the string" do
40
+ benzo.options_map = { '-v' => true }
41
+ benzo.send(:map!)
42
+
43
+ benzo.vars.keys.count.should == 0
44
+ end
45
+
46
+ it "should append the value to @vars if the key is a symbol" do
47
+ benzo.options_map = { :logger => 'this_logger' }
48
+ benzo.send(:map!)
49
+
50
+ benzo.vars[:logger].should_not be_nil
51
+ end
52
+
53
+ end
54
+
55
+ context "#get_symbol" do
56
+
57
+ def get_symbol(str)
58
+ benzo.send(:get_symbol, str)
59
+ end
60
+
61
+ it "should find the symbol" do
62
+ get_symbol('-f :file').should == :file
63
+ end
64
+
65
+ it "should return the first symbol it finds" do
66
+ get_symbol('-a :file :gooop').should == :file
67
+ end
68
+
69
+ it "should find symbols with underscores" do
70
+ get_symbol('-d :db_name').should == :db_name
71
+ end
72
+
73
+ it "should find symbols with capital letters" do
74
+ get_symbol(':FileName').should == :FileName
75
+ end
76
+
77
+ it "should raise an error if the symbol has other chars" do
78
+ lambda { get_symbol('-q :what?') }.should raise_error
79
+ end
80
+
81
+ it "should raise an error when encountering symbols with dash" do
82
+ lambda { get_symbol('-f :this-that') }.should raise_error
83
+ end
84
+
85
+ it "should not include dots in the symbol" do
86
+ lambda { get_symbol('-f :file.ext') }.should raise_error
87
+ end
88
+
89
+ end
90
+
91
+ end
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'awesome_print'
5
+
6
+ $: << File.dirname(__FILE__) + '/../lib'
7
+
8
+ require 'benzo'
9
+
10
+ def fixture_path(filename)
11
+ File.join( File.dirname(__FILE__), 'fixtures', filename )
12
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: benzo
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Spike Grobstein
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: cocaine
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
+ description: Take the edge off when doing (command) lines.
31
+ email:
32
+ - me@spike.cx
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - .rspec
39
+ - .travis.yml
40
+ - Gemfile
41
+ - LICENSE
42
+ - README.md
43
+ - Rakefile
44
+ - benzo.gemspec
45
+ - lib/benzo.rb
46
+ - lib/benzo/version.rb
47
+ - spec/benzo_spec.rb
48
+ - spec/spec_helper.rb
49
+ homepage: https://github.com/spikegrobstein/benzo
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.24
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: A robust mapper for complex commandline calls using cocaine.
73
+ test_files:
74
+ - spec/benzo_spec.rb
75
+ - spec/spec_helper.rb
76
+ has_rdoc: