doughboy 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- doughboy (0.0.3)
4
+ doughboy (0.1.0)
5
5
  open4 (~> 1.0.1)
6
6
 
7
7
  GEM
data/README.markdown CHANGED
@@ -1,35 +1,35 @@
1
1
  Doughboy
2
2
  ========
3
3
 
4
- Doughboy is a Ruby library that strives to enable developers to easily make subprocessed shell calls.
4
+ Doughboy is a Ruby library that strives to enable developers to simply make subprocessed shell calls.
5
5
 
6
6
  Usage
7
7
  --------
8
8
 
9
- Doughboy provides a compossible interface to crafting shell commands.
9
+ Doughboy provides a composable interface to crafting shell commands. The following composition methods are available:
10
10
 
11
- command = Doughboy.with_exec("ruby").with_options(["-v"])
12
- command.run!
11
+ Doughboy.new("ruby -v")
12
+ Doughboy.new("python", "setup.py")
13
+ Doughboy.new(:executable => "python", :arguments => "setup.py")
13
14
 
14
- Commands components can be set directly on any object.
15
+ Doing any of these things will return a command object that can be told to run:
15
16
 
16
- command = Doughboy::Command.new(:executable => "easy_install", :arguments => "some-python-library").run!
17
+ command = Doughboy.new("ruby -v")
18
+ command.run #=> returns and Output object
17
19
 
18
- The preferred method is to use the composable chain methods.
20
+ The Output object includes the output methods: pid, stdin, stdout, stderr. For a shortcut, use the .text method. To create and run a new command, use the .with_exec shortcut:
21
+
22
+ output = Doughboy.with_exec("ruby -v") #=> returns an Output object
19
23
 
20
24
  Command Components
21
25
  ------------------
22
26
 
23
- Each command is composed of 3 separate components: executable, options, arguments.
27
+ Each command is composed of 2 components, executable and arguments.
24
28
 
25
29
  ### Executable
26
30
 
27
31
  When this component is set, the full-path is captured. If no path information could be determined, the executable passed will be used.
28
32
 
29
- ### Options
30
-
31
- Options are command line switches that either start w/ "-" or "--".
32
-
33
33
  ### Arguments
34
34
 
35
35
  Arguments are items that are passed to the executable. They might be a list of ip addresses or files.
@@ -37,7 +37,7 @@ Arguments are items that are passed to the executable. They might be a list of i
37
37
  Origin of the Name
38
38
  ------------------
39
39
 
40
- I wish I could say that it was meant to pay homage to the fighting men of the Lost Generation, but it's not. I was watching Boyz N The Hood.
40
+ I wish I could say that it was meant to pay homage to the fighting men of the Lost Generation, but it's not. I was watching Boyz N The Hood. We got a problem here?
41
41
 
42
42
  ![Doughboy](http://2.bp.blogspot.com/_xdN0QQwsP1A/TDhzYsJhkpI/AAAAAAAAJZk/yjqX6ZNF1t8/s400/boyz_n_the_hood_xlg+ICE+Cube+crop.jpg)
43
43
 
data/lib/doughboy.rb CHANGED
@@ -5,8 +5,12 @@ require 'doughboy/output'
5
5
 
6
6
  module Doughboy
7
7
 
8
- def self.with_exec(command)
9
- Command.with_exec(command)
8
+ def self.with_exec(*args)
9
+ Command.with_exec(*args)
10
+ end
11
+
12
+ def self.new(*args)
13
+ Command.new(*args)
10
14
  end
11
15
 
12
16
  end
@@ -1,61 +1,56 @@
1
1
  module Doughboy
2
2
  class Command
3
- attr_accessor :arguments, :executable, :full_arguments, :options
3
+ attr_accessor :arguments, :executable
4
+
5
+ # Command.new
6
+ # Command.new("python setup.py develop")
7
+ # Command.new("python", "setup.py develop")
8
+ # Command.new(:executable => "python", :arguments => "setup.py develop")
4
9
 
5
10
  def initialize(*args)
6
- if args.any?
7
- local_args = args.first
11
+ return self if args.empty?
12
+ parsed_args = args.first.is_a?(Hash) ? args.first : parse_string_args(args)
8
13
 
9
- %w( arguments executable options full_arguments).each do |arg|
10
- self.send("#{arg}=", local_args[arg.intern])
11
- end
12
- end
14
+ self.executable = parsed_args[:executable]
15
+ self.arguments = parsed_args[:arguments]
13
16
  end
14
17
 
15
- def self.with_exec(command)
16
- parsed_command = parse_command(command)
17
-
18
- new( :executable => parsed_command[:executable],
19
- :arguments => parsed_command[:arguments],
20
- :options => parsed_command[:options],
21
- :full_arguments => parsed_command[:full_arguments])
18
+ def self.with_exec(*args)
19
+ command = new(*args)
20
+ command.run
22
21
  end
23
22
 
24
23
  def command
25
- if full_arguments
26
- [executable, full_arguments].join(" ")
27
- else
28
- [executable, options, arguments].join(" ")
29
- end
24
+ [executable, arguments].join(" ")
30
25
  end
31
26
 
32
27
  def executable=(value)
33
28
  return nil if value.nil?
29
+
34
30
  full_path = `which #{value}`.strip
35
31
  @executable = full_path != "" ? full_path : value
36
32
  end
37
33
 
38
- def options=(value)
39
- return nil if value.nil?
40
- @options = value.is_a?(Array) ? value : value.split(" ")
41
- end
42
-
43
- def run!
34
+ def run
44
35
  Output.new( Open4::popen4(command) )
45
36
  end
46
37
 
47
38
  private
48
- def self.parse_command(command)
39
+ def self.parse_executable(command)
49
40
  split_command = command.split(" ")
50
41
  parsed_command = { }
51
- args_and_opts = split_command[1..(split_command.size-1)]
52
42
 
53
- parsed_command[:full_arguments] = args_and_opts
54
43
  parsed_command[:executable] = split_command[0]
55
- parsed_command[:arguments] = args_and_opts.select { |t| !t.include?("-") }.join(" ")
56
- parsed_command[:options] = args_and_opts.select { |t| t.include?("-") }
44
+ parsed_command[:arguments] = split_command[1..(split_command.size-1)]
57
45
  parsed_command
58
46
  end
59
47
 
48
+ def parse_string_args(args)
49
+ if args.size == 1
50
+ self.class.parse_executable(args.first)
51
+ else
52
+ { :executable => args.first, :arguments => args[1]}
53
+ end
54
+ end
60
55
  end
61
56
  end
@@ -1,3 +1,3 @@
1
1
  module Doughboy
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -3,30 +3,51 @@ require 'spec_helper'
3
3
  module Doughboy
4
4
  describe Command do
5
5
  describe ".new" do
6
- context "with a string argument" do
7
- it "sets the executable"
6
+ context "with no arugments" do
7
+ it "does not error" do
8
+ lambda { Command.new }.should_not raise_exception
9
+ end
8
10
  end
9
11
 
10
- context "with a hash argument" do
12
+ context "with a single string argument" do
13
+ it "sets the executable" do
14
+ command = Command.new("python setup.py develop")
15
+ command.executable.should_not be_nil
16
+ end
17
+
11
18
  it "sets the arguments" do
12
- command = Command.new(:arguments => "setup.py develop")
19
+ command = Command.new("python setup.py develop")
20
+ command.arguments.should_not be_nil
21
+ end
22
+ end
23
+
24
+ context "with a double string argument" do
25
+ it "sets the executable" do
26
+ command = Command.new("python", "setup.py develop")
27
+ command.executable.should_not be_nil
28
+ end
29
+
30
+ it "sets the arguments" do
31
+ command = Command.new("python", "setup.py develop")
13
32
  command.arguments.should_not be_nil
14
33
  end
15
34
 
35
+ end
36
+
37
+ context "with a hash argument" do
16
38
  it "sets the executeable" do
17
39
  command = Command.new(:executable => "python")
18
40
  command.executable.should_not be_nil
19
41
  end
20
42
 
21
- it "sets the options" do
22
- options = "-r -i"
23
- command = Command.new(:options => options)
24
- command.options.should_not be_nil
43
+ it "sets the arguments" do
44
+ command = Command.new(:arguments => "setup.py develop")
45
+ command.arguments.should_not be_nil
25
46
  end
26
47
 
27
48
  # Command.with_exec("python setup.py develop).run
28
- # Command.with_exec("python").with_option("-r").with_arg("123456").run
29
- # Command.with_exec("mi").with_options([]).with_args([]).run
49
+ # Command.with_exec("python").with_arg("123456").run
50
+ # Command.with_exec("mi").with_args([]).run
30
51
  # Command.with_exec("python") do |input|
31
52
  # input.puts("exit()")
32
53
  # end
@@ -34,52 +55,11 @@ module Doughboy
34
55
  end
35
56
 
36
57
  describe ".with_exec" do
37
- it "returns a Command" do
38
- Command.with_exec("ruby").should be_kind_of(Command)
39
- end
40
-
41
- context "with a string of executable and argument" do
42
- before(:each) do
43
- @command_string = "ps aux"
44
- end
45
-
46
- it "assigns the executable" do
47
- command = Command.with_exec(@command_string)
48
- command.executable.should == `which ps`.strip
49
- end
50
-
51
- it "assigns the arguments" do
52
- command = Command.with_exec(@command_string)
53
- command.arguments.should == "aux"
54
- end
55
- end
56
-
57
- context "with a string of executable, arguments, and options" do
58
- before(:each) do
59
- @command_string = "mi -r 212984"
60
- end
61
58
 
62
- it "assigns the executable" do
63
- command = Command.with_exec(@command_string)
64
- command.executable.should == `which mi`.strip
65
- end
66
-
67
- it "assigns the arguments" do
68
- command = Command.with_exec(@command_string)
69
- command.arguments.should == "212984"
70
- end
71
-
72
- it "assigns the options" do
73
- command = Command.with_exec(@command_string)
74
- command.options.should == ["-r"]
75
- end
59
+ it "returns a Output" do
60
+ Command.with_exec("ruby").should be_kind_of(Output)
76
61
  end
77
62
 
78
- context "with an executable" do
79
- it "assigns the executable" do
80
- Command.with_exec("ruby").executable.should include("ruby")
81
- end
82
- end
83
63
  end
84
64
 
85
65
  describe "#arguments=" do
@@ -92,15 +72,15 @@ module Doughboy
92
72
  end
93
73
 
94
74
  describe "#command" do
95
- it "joins the executable, options, and arguments" do
96
- full_command = "/bin/ps aux"
75
+ it "joins the executable and arguments" do
76
+ full_command = "/bin/ps aux"
97
77
  command = Command.new(:executable => "ps", :options => "", :arguments => "aux")
98
78
  command.command.should == full_command
99
79
  end
100
80
 
101
81
  it "returns them in the proper order" do
102
82
  python = `which python`.strip
103
- command = Command.with_exec("python mbu_information.py -r 218298")
83
+ command = Command.new("python mbu_information.py -r 218298")
104
84
  command.command.should == "#{python} mbu_information.py -r 218298"
105
85
  end
106
86
  end
@@ -119,19 +99,20 @@ module Doughboy
119
99
  end
120
100
  end
121
101
 
122
- describe "#options=" do
123
- context "with an array of options" do
124
- it "sets the options as an array" do
125
- options = ["-r", "-i"]
126
- command = Command.new(:options => options)
127
- command.options.should == options
128
- end
102
+ describe "#run" do
103
+ it "runs the command" do
104
+ full_command = "/bin/ps aux"
105
+ command = Command.new()
106
+ command.stub!(:command).and_return(full_command)
107
+
108
+ Open4.should_receive(:popen4).with(full_command).
109
+ and_return([1234, StringIO.new, StringIO.new, StringIO.new])
110
+ command.run
129
111
  end
130
112
 
131
- context "with a string of options" do
132
- options = "-r -i"
133
- command = Command.new(:options => options)
134
- command.options.should == ["-r", "-i"]
113
+ it "returns an output object" do
114
+ command = Command.new(:executable => "ps", :arguments => "aux")
115
+ command.run.should be_kind_of(Output)
135
116
  end
136
117
  end
137
118
 
@@ -143,12 +124,12 @@ module Doughboy
143
124
 
144
125
  Open4.should_receive(:popen4).with(full_command).
145
126
  and_return([1234, StringIO.new, StringIO.new, StringIO.new])
146
- command.run!
127
+ command.run
147
128
  end
148
129
 
149
130
  it "returns an output object" do
150
- command = Command.new(:executable => "ps", :options => "", :arguments => "aux")
151
- command.run!.should be_kind_of(Output)
131
+ command = Command.new(:executable => "ps", :arguments => "aux")
132
+ command.run.should be_kind_of(Output)
152
133
  end
153
134
  end
154
135
  end
@@ -2,9 +2,15 @@ require 'spec_helper'
2
2
 
3
3
  describe Doughboy do
4
4
 
5
- describe "with_exec" do
6
- it "returns a command" do
7
- Doughboy.with_exec("ruby -v").should be_kind_of(Doughboy::Command)
5
+ describe ".new" do
6
+ it "returns a Command" do
7
+ Doughboy.new("ruby -v").should be_kind_of(Doughboy::Command)
8
+ end
9
+ end
10
+
11
+ describe ".with_exec" do
12
+ it "returns returns the output from a command" do
13
+ Doughboy.with_exec("ruby -v").should be_kind_of(Doughboy::Output)
8
14
  end
9
15
  end
10
16
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 1
7
8
  - 0
8
- - 4
9
- version: 0.0.4
9
+ version: 0.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Joshua Schairbaum