kommando 0.0.3 → 0.0.4

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: 21c7dae7504adc6c8d020d36a15040145cb539b2
4
- data.tar.gz: 6e13903d3610a520f87759feefbf810b9f60e8ed
3
+ metadata.gz: 90cd16476bfc79190a5707fa07bd7c6d235f5a04
4
+ data.tar.gz: 947a4d5c54f2837e2f969bb1ca7ef1de0487a06b
5
5
  SHA512:
6
- metadata.gz: 20709e235f978d408c0a8b4b0e596a2c3d5b8ebc1dcfe9b3c902737f324450d12423633e4801e12a30b8e29d596bb764a2f6810e0ec80023eb7555b3bb329e70
7
- data.tar.gz: 48cb28a22b828a1dd39561b88f9f5bf344453385cbbf4edc304b1e0dba2bbe64279f71a7bcbc19ee8d87d91287474e6f89103e95d1b0065706fa411c8677734b
6
+ metadata.gz: f1bf07fac67f79fa57574c243cfee2e1da16168d4c9a4ce8b7a8ba65f55f09bf491355af063c64a7a1fbce5dc456b05cce877bbdcc3d4223c2cf6002411bac31
7
+ data.tar.gz: 069f925a8b7b8c13a2f317a6c9b6ea557b1a21c5aad2b82b3d03d2fe86d0023c33280c78827756a02f8acd102aab2bc6b657346ab93a1d616fa027103fc69168
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.0.4
4
+ Writes standard out to file and timeouts.
5
+
6
+ - FEAT: Support for command run timeouts with `Kommando.new "ping -c 1 google.com", timeout: 3.5`
7
+ - FEAT: Write output to file with `Kommando.new "ping -c 1 google.com", output: "path/to/file.txt"`
8
+ - EXAMPLES: `stdout_to_file` and `timeout` added.
9
+
3
10
  ## 0.0.3
4
11
  Outputs to standard out while executing.
5
12
 
@@ -14,7 +21,6 @@ Actually useful initial release.
14
21
  - EXAMPLES: `ping` and `exit` added.
15
22
 
16
23
  ## 0.0.1
17
-
18
24
  Initial release.
19
25
 
20
26
  - FEAT: Runs command without arguments.
data/bin/e2e CHANGED
@@ -1,17 +1,24 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "./lib/kommando"
4
+ require 'tempfile'
4
5
 
5
6
  find_examples = Kommando.new "find examples -type f -name *"
6
7
  find_examples.run
7
8
 
9
+ e2e_output = Tempfile.new
10
+
8
11
  for example in find_examples.out.split("\r\n") do
9
12
  print "Running #{example} ".ljust(74, ".")
10
- example = Kommando.new "ruby #{example}"
13
+ example = Kommando.new "ruby #{example}", {
14
+ output: e2e_output.path
15
+ }
11
16
  example.run
12
17
 
13
- raise "Example #{example} did not exit cleanly" unless example.code == 0
14
-
18
+ unless example.code == 0
19
+ puts "\n#{File.read(e2e_output.path)}"
20
+ raise "Example #{example} did not exit cleanly"
21
+ end
15
22
  puts " done"
16
23
  end
17
24
 
data/bin/stress ADDED
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "./lib/kommando"
4
+
5
+ def stress iterations
6
+ iterations.times do |i|
7
+ k = Kommando.new "rake", {
8
+ timeout: 10
9
+ }
10
+ k.run
11
+ unless k.code == 0
12
+ print "!FAILED! waiting for 3 seconds"
13
+ sleep 3 # wait for sync
14
+ puts k.out
15
+ exit 1
16
+ end
17
+ print "."
18
+ end
19
+ end
20
+
21
+ number_of_threads = if ARGV[0]
22
+ ARGV[0].to_i
23
+ else
24
+ 3
25
+ end
26
+
27
+ number_of_iterations = if ARGV[1]
28
+ ARGV[1].to_i
29
+ else
30
+ 100
31
+ end
32
+
33
+ puts "starting #{number_of_threads} threads for #{number_of_iterations} iterations"
34
+
35
+ threads = []
36
+ number_of_threads.times do |i|
37
+ threads << Thread.new do
38
+ stress number_of_iterations
39
+ end
40
+ end
41
+
42
+ threads.each do |thread|
43
+ thread.join
44
+ end
@@ -0,0 +1,20 @@
1
+ require "./lib/kommando"
2
+ require "tempfile"
3
+
4
+ outfile = Tempfile.new
5
+
6
+ k = Kommando.new "ping -c 3 127.0.0.1", {
7
+ output: outfile.path
8
+ }
9
+
10
+ Thread.abort_on_exception=true
11
+ t = Thread.new do
12
+ Kommando.new "tail -f #{outfile.path}", {
13
+ output: true
14
+ }
15
+ end
16
+
17
+ k.run
18
+ t.kill
19
+
20
+ puts File.read outfile.path
@@ -0,0 +1,15 @@
1
+ require "./lib/kommando"
2
+
3
+ k = Kommando.new "sleep 2", {
4
+ timeout: 1
5
+ }
6
+ k2 = Kommando.new "sleep 2", {
7
+ timeout: 1
8
+ }
9
+
10
+ k.run
11
+ k2.run
12
+
13
+ if k.code == 1 && k2.code == 1
14
+ puts "timed out"
15
+ end
data/lib/kommando.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "pty"
2
+ require "timeout"
2
3
 
3
4
  require_relative "kommando/error"
4
5
  require_relative "kommando/version"
@@ -10,32 +11,72 @@ class Kommando
10
11
  @cmd = cmd
11
12
  @stdout = Buffer.new
12
13
 
13
- @output = opts[:output] == true
14
+ @output_stdout = opts[:output] == true
15
+ @output_file = if opts[:output].class == String
16
+ opts[:output]
17
+ end
18
+
19
+ @timeout = if opts[:timeout].class == Float
20
+ opts[:timeout]
21
+ elsif opts[:timeout].class == Fixnum
22
+ opts[:timeout].to_f
23
+ end
24
+
25
+ @executed = false
14
26
  end
15
27
 
16
28
  def run
29
+ return false if @executed
30
+ @executed = true
31
+
17
32
  command, *args = @cmd.split " "
18
33
  begin
19
34
  PTY.spawn(command, *args) do |stdout, stdin, pid|
20
- Thread.abort_on_exception = true
35
+ stdout_file = File.open @output_file, 'w' if @output_file
21
36
 
37
+ Thread.abort_on_exception = true
22
38
  thread_stdout = Thread.new do
23
39
  while true do
24
40
  break if stdout.eof?
25
41
 
26
42
  c = stdout.getc
27
43
  @stdout.append c if c
28
- print c if @output
44
+ print c if @output_stdout
45
+ stdout_file.write c if @output_file
29
46
  end
30
47
  end
31
- thread_stdout.join
48
+
49
+ thread_did_timeout = nil
50
+ if @timeout
51
+ begin
52
+ Timeout.timeout(@timeout) do
53
+ thread_stdout.join
54
+ end
55
+ rescue Timeout::Error
56
+ Process.kill('KILL', pid)
57
+ thread_did_timeout = true
58
+ end
59
+ else
60
+ thread_stdout.join
61
+ end
62
+
63
+ stdout_file.close if @output_file
32
64
 
33
65
  # http://stackoverflow.com/a/7263243
34
66
  Process.wait(pid)
35
67
 
36
- @code = $?.exitstatus
68
+ @code = if thread_did_timeout
69
+ 1
70
+ else
71
+ $?.exitstatus
72
+ end
73
+
74
+ end
75
+ rescue RuntimeError => ex
76
+ if ex.message == "can't get Master/Slave device"
77
+ #suppress, weird stuff.
37
78
  end
38
- rescue => ex
79
+ rescue Errno::ENOENT => ex
39
80
  raise Kommando::Error, "Command '#{command}' not found"
40
81
  end
41
82
 
@@ -1,3 +1,3 @@
1
1
  class Kommando
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kommando
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matti Paksula
@@ -101,9 +101,12 @@ files:
101
101
  - bin/e2e
102
102
  - bin/reinstall
103
103
  - bin/setup
104
+ - bin/stress
104
105
  - examples/exit.rb
105
106
  - examples/live_output.rb
106
107
  - examples/ping.rb
108
+ - examples/stdout_to_file.rb
109
+ - examples/timeout.rb
107
110
  - examples/uptime.rb
108
111
  - kommando.gemspec
109
112
  - lib/kommando.rb