stella 0.5.3 → 0.5.4
Sign up to get free protection for your applications and to get access to all the features.
- data/{README.txt → README.textile} +63 -40
- data/Rakefile +7 -5
- data/bin/stella +1 -1
- data/bin/stella.bat +12 -0
- data/lib/pcaplet.rb +180 -0
- data/lib/stella/adapter/ab.rb +57 -33
- data/lib/stella/adapter/base.rb +11 -1
- data/lib/stella/adapter/httperf.rb +13 -10
- data/lib/stella/adapter/pcap_watcher.rb +221 -0
- data/lib/stella/adapter/proxy_watcher.rb +76 -0
- data/lib/stella/adapter/siege.rb +28 -11
- data/lib/stella/cli/agents.rb +2 -2
- data/lib/stella/cli/base.rb +37 -1
- data/lib/stella/cli/localtest.rb +1 -2
- data/lib/stella/cli/sysinfo.rb +17 -0
- data/lib/stella/cli/watch.rb +278 -0
- data/lib/stella/cli.rb +23 -11
- data/lib/stella/command/base.rb +1 -10
- data/lib/stella/command/localtest.rb +43 -23
- data/lib/stella/data/domain.rb +75 -0
- data/lib/stella/data/http.rb +124 -0
- data/lib/stella/logger.rb +16 -5
- data/lib/stella/storable.rb +4 -2
- data/lib/stella/support.rb +71 -0
- data/lib/stella/sysinfo.rb +247 -0
- data/lib/stella/test/base.rb +5 -1
- data/lib/stella/test/definition.rb +1 -1
- data/lib/stella/test/run/summary.rb +14 -4
- data/lib/stella/text/resource.rb +0 -1
- data/lib/stella.rb +28 -10
- data/lib/utils/domainutil.rb +47 -0
- data/lib/utils/fileutil.rb +22 -3
- data/lib/utils/httputil.rb +184 -128
- data/lib/utils/mathutil.rb +20 -7
- data/lib/win32/Console/ANSI.rb +305 -0
- data/lib/win32/Console.rb +970 -0
- data/spec/show-agents_spec.rb +0 -0
- data/support/kvm.h +91 -0
- data/support/ruby-pcap-takuma-notes.txt +19 -0
- data/support/ruby-pcap-takuma-patch.txt +30 -0
- data/support/text/en.yaml +26 -3
- data/vendor/frylock/README.textile +72 -0
- data/vendor/frylock/bin/example +170 -0
- data/vendor/frylock/frylock.gemspec +18 -0
- data/vendor/frylock/lib/frylock/exceptions.rb +24 -0
- data/vendor/frylock/lib/frylock.rb +232 -0
- data/vendor/frylock/test/command_test.rb +33 -0
- data/vendor/hitimes-0.4.0/HISTORY +28 -0
- data/vendor/hitimes-0.4.0/LICENSE.txt +19 -0
- data/vendor/hitimes-0.4.0/README +80 -0
- data/vendor/hitimes-0.4.0/Rakefile +63 -0
- data/vendor/hitimes-0.4.0/examples/benchmarks.rb +86 -0
- data/vendor/hitimes-0.4.0/examples/stats.rb +29 -0
- data/vendor/hitimes-0.4.0/ext/extconf.rb +15 -0
- data/vendor/hitimes-0.4.0/ext/hitimes_ext.c +21 -0
- data/vendor/hitimes-0.4.0/ext/hitimes_instant_clock_gettime.c +20 -0
- data/vendor/hitimes-0.4.0/ext/hitimes_instant_osx.c +16 -0
- data/vendor/hitimes-0.4.0/ext/hitimes_instant_windows.c +27 -0
- data/vendor/hitimes-0.4.0/ext/hitimes_interval.c +340 -0
- data/vendor/hitimes-0.4.0/ext/hitimes_interval.h +73 -0
- data/vendor/hitimes-0.4.0/ext/hitimes_stats.c +242 -0
- data/vendor/hitimes-0.4.0/ext/hitimes_stats.h +30 -0
- data/vendor/hitimes-0.4.0/ext/rbconfig-mingw.rb +178 -0
- data/vendor/hitimes-0.4.0/ext/rbconfig.rb +178 -0
- data/vendor/hitimes-0.4.0/gemspec.rb +54 -0
- data/vendor/hitimes-0.4.0/lib/hitimes/mutexed_stats.rb +23 -0
- data/vendor/hitimes-0.4.0/lib/hitimes/paths.rb +54 -0
- data/vendor/hitimes-0.4.0/lib/hitimes/stats.rb +29 -0
- data/vendor/hitimes-0.4.0/lib/hitimes/timer.rb +223 -0
- data/vendor/hitimes-0.4.0/lib/hitimes/version.rb +42 -0
- data/vendor/hitimes-0.4.0/lib/hitimes.rb +24 -0
- data/vendor/hitimes-0.4.0/spec/interval_spec.rb +115 -0
- data/vendor/hitimes-0.4.0/spec/mutex_stats_spec.rb +34 -0
- data/vendor/hitimes-0.4.0/spec/paths_spec.rb +14 -0
- data/vendor/hitimes-0.4.0/spec/spec_helper.rb +6 -0
- data/vendor/hitimes-0.4.0/spec/stats_spec.rb +72 -0
- data/vendor/hitimes-0.4.0/spec/timer_spec.rb +105 -0
- data/vendor/hitimes-0.4.0/spec/version_spec.rb +27 -0
- data/vendor/hitimes-0.4.0/tasks/announce.rake +39 -0
- data/vendor/hitimes-0.4.0/tasks/config.rb +107 -0
- data/vendor/hitimes-0.4.0/tasks/distribution.rake +53 -0
- data/vendor/hitimes-0.4.0/tasks/documentation.rake +33 -0
- data/vendor/hitimes-0.4.0/tasks/extension.rake +64 -0
- data/vendor/hitimes-0.4.0/tasks/rspec.rake +31 -0
- data/vendor/hitimes-0.4.0/tasks/rubyforge.rake +52 -0
- data/vendor/hitimes-0.4.0/tasks/utils.rb +80 -0
- data/vendor/useragent/lib/user_agent.rb +1 -1
- metadata +87 -8
File without changes
|
data/support/kvm.h
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
|
3
|
+
*
|
4
|
+
* @APPLE_LICENSE_HEADER_START@
|
5
|
+
*
|
6
|
+
* This file contains Original Code and/or Modifications of Original Code
|
7
|
+
* as defined in and that are subject to the Apple Public Source License
|
8
|
+
* Version 2.0 (the 'License'). You may not use this file except in
|
9
|
+
* compliance with the License. Please obtain a copy of the License at
|
10
|
+
* http://www.opensource.apple.com/apsl/ and read it before using this
|
11
|
+
* file.
|
12
|
+
*
|
13
|
+
* The Original Code and all software distributed under the License are
|
14
|
+
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
|
15
|
+
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
|
16
|
+
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
|
18
|
+
* Please see the License for the specific language governing rights and
|
19
|
+
* limitations under the License.
|
20
|
+
*
|
21
|
+
* @APPLE_LICENSE_HEADER_END@
|
22
|
+
*/
|
23
|
+
/*-
|
24
|
+
* Copyright (c) 1989, 1993
|
25
|
+
* The Regents of the University of California. All rights reserved.
|
26
|
+
*
|
27
|
+
* Redistribution and use in source and binary forms, with or without
|
28
|
+
* modification, are permitted provided that the following conditions
|
29
|
+
* are met:
|
30
|
+
* 1. Redistributions of source code must retain the above copyright
|
31
|
+
* notice, this list of conditions and the following disclaimer.
|
32
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
33
|
+
* notice, this list of conditions and the following disclaimer in the
|
34
|
+
* documentation and/or other materials provided with the distribution.
|
35
|
+
* 3. All advertising materials mentioning features or use of this software
|
36
|
+
* must display the following acknowledgement:
|
37
|
+
* This product includes software developed by the University of
|
38
|
+
* California, Berkeley and its contributors.
|
39
|
+
* 4. Neither the name of the University nor the names of its contributors
|
40
|
+
* may be used to endorse or promote products derived from this software
|
41
|
+
* without specific prior written permission.
|
42
|
+
*
|
43
|
+
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
44
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
45
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
46
|
+
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
47
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
48
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
49
|
+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
50
|
+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
51
|
+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
52
|
+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
53
|
+
* SUCH DAMAGE.
|
54
|
+
*
|
55
|
+
* @(#)kvm.h 8.1 (Berkeley) 6/2/93
|
56
|
+
*/
|
57
|
+
|
58
|
+
#ifndef _KVM_H_
|
59
|
+
#define _KVM_H_
|
60
|
+
|
61
|
+
/* Default version symbol. */
|
62
|
+
#define VRS_SYM "_version"
|
63
|
+
#define VRS_KEY "VERSION"
|
64
|
+
|
65
|
+
#include <nlist.h>
|
66
|
+
#include <sys/cdefs.h>
|
67
|
+
|
68
|
+
__BEGIN_DECLS
|
69
|
+
|
70
|
+
typedef struct __kvm kvm_t;
|
71
|
+
|
72
|
+
struct kinfo_proc;
|
73
|
+
int kvm_close(kvm_t *);
|
74
|
+
char **kvm_getargv(kvm_t *, const struct kinfo_proc *, int);
|
75
|
+
char **kvm_getenvv(kvm_t *, const struct kinfo_proc *, int);
|
76
|
+
char *kvm_geterr(kvm_t *);
|
77
|
+
int kvm_getloadavg(kvm_t *, double [], int);
|
78
|
+
char *kvm_getfiles(kvm_t *, int, int, int *);
|
79
|
+
struct kinfo_proc *
|
80
|
+
kvm_getprocs(kvm_t *, int, int, int *);
|
81
|
+
int kvm_nlist(kvm_t *, struct nlist *);
|
82
|
+
kvm_t *kvm_open
|
83
|
+
(const char *, const char *, const char *, int, const char *);
|
84
|
+
kvm_t *kvm_openfiles
|
85
|
+
(const char *, const char *, const char *, int, char *);
|
86
|
+
int kvm_read(kvm_t *, unsigned long, void *, unsigned int);
|
87
|
+
int kvm_write(kvm_t *, unsigned long, const void *, unsigned int);
|
88
|
+
|
89
|
+
__END_DECLS
|
90
|
+
|
91
|
+
#endif /* !_KVM_H_ */
|
@@ -0,0 +1,19 @@
|
|
1
|
+
Ruby-Pcap Patch (2008-02-10)
|
2
|
+
-------------------------------------------------------
|
3
|
+
|
4
|
+
"pcap.bundle: warning: do not use Fixnums as Symbols"
|
5
|
+
|
6
|
+
Ruby-Pcap is a libpcap binding for Ruby. On Ruby 1.8.6,
|
7
|
+
the latest release (0.6) produces the warning above on
|
8
|
+
OS X. ruby-pcap-takuma-patch.txt is a patch from here:
|
9
|
+
http://d.hatena.ne.jp/takuma104/20080210/1202638583
|
10
|
+
|
11
|
+
You can get Ruby-Pcap from here:
|
12
|
+
http://www.goto.info.waseda.ac.jp/~fukusima/ruby/pcap-e.html
|
13
|
+
|
14
|
+
$ tar -zxf ruby-pcap-0.6.tar.gz
|
15
|
+
$ cd pcap
|
16
|
+
$ patch -p1 < ruby-pcap-takuma-patch.txt
|
17
|
+
$ make
|
18
|
+
$ make install
|
19
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
diff -ur pcap-0.6/Pcap.c pcap-working/Pcap.c
|
2
|
+
--- pcap-0.6/Pcap.c 2008-02-10 19:10:30.000000000 +0900
|
3
|
+
+++ pcap-working/Pcap.c 2008-02-10 18:58:49.000000000 +0900
|
4
|
+
@@ -782,9 +782,9 @@
|
5
|
+
/* define class PcapStat */
|
6
|
+
cPcapStat = rb_funcall(rb_cStruct, rb_intern("new"), 4,
|
7
|
+
Qnil,
|
8
|
+
- INT2NUM(rb_intern("recv")),
|
9
|
+
- INT2NUM(rb_intern("drop")),
|
10
|
+
- INT2NUM(rb_intern("ifdrop")));
|
11
|
+
+ ID2SYM(rb_intern("recv")),
|
12
|
+
+ ID2SYM(rb_intern("drop")),
|
13
|
+
+ ID2SYM(rb_intern("ifdrop")));
|
14
|
+
rb_define_const(mPcap, "Stat", cPcapStat);
|
15
|
+
|
16
|
+
/* define exception classes */
|
17
|
+
diff -ur pcap-0.6/packet.c pcap-working/packet.c
|
18
|
+
--- pcap-0.6/packet.c 2008-02-10 19:10:30.000000000 +0900
|
19
|
+
+++ pcap-working/packet.c 2008-02-10 18:55:13.000000000 +0900
|
20
|
+
@@ -17,8 +17,8 @@
|
21
|
+
|
22
|
+
VALUE cPacket;
|
23
|
+
static VALUE mMarshal;
|
24
|
+
-int id_load;
|
25
|
+
-int id_dump;
|
26
|
+
+ID id_load;
|
27
|
+
+ID id_dump;
|
28
|
+
|
29
|
+
/* called from GC */
|
30
|
+
static void
|
data/support/text/en.yaml
CHANGED
@@ -30,16 +30,28 @@
|
|
30
30
|
Examples:
|
31
31
|
|
32
32
|
# Run Apache Bench with 50 users, 20 requests each. Repeat 5 times.
|
33
|
-
$ stella -
|
33
|
+
$ stella -x 5 -w 0.5 ab -n 1000 -c 50 http://stellaaahhhh.com/
|
34
34
|
|
35
35
|
# Run Siege with 100 users, 10 requests each with a Linux FireFox 3 user agent
|
36
|
-
$ stella
|
37
|
-
|
36
|
+
$ stella --rampup=25,200 --agent=firefox-3-linux siege -c 100 -r 10 http://stellaaahhhh.com/
|
37
|
+
|
38
|
+
# Display available user agent strings
|
39
|
+
$ stella agents -l
|
40
|
+
|
41
|
+
# Launch a proxy to monitor HTTP traffic. Record requests to a flatfile.
|
42
|
+
$ stella -vv watch -W -p 3114 -r uri.txt
|
43
|
+
|
44
|
+
# Monitor network traffic with packet sniffer (requires unix and root access)
|
45
|
+
$ stella watch -P dns
|
46
|
+
$ stella watch -P -p 9000 http
|
47
|
+
|
48
|
+
|
38
49
|
User-Agent Examples (--agent):
|
39
50
|
ff-3-linux, ie-7-win, opera-10, chrome-0.2-osx
|
40
51
|
|
41
52
|
:option_help_message: A short note about the test (i.e. 'new cache enabled')
|
42
53
|
:option_help_format: "Output file format. One of: csv [default], yaml, tsv, json"
|
54
|
+
:option_help_force: "Force the command to ignore non-fatal errors. CAREFUL!"
|
43
55
|
:option_help_version: Display the version, then exit
|
44
56
|
:option_help_sleep: Number of seconds to sleep between runs
|
45
57
|
:option_help_agent: Specify a User-Agent. Can be a short string or complete agent string. Defaults to 'random'.
|
@@ -50,5 +62,16 @@
|
|
50
62
|
:error_class_must_override: "You must override the method: %s"
|
51
63
|
:error_class_unknown_argument: "Ignoring unknown argument: %s"
|
52
64
|
:error_adapter_command_not_ready: "Incomplete options or arguments for %s"
|
65
|
+
:error_adapter_runtime: "%s had a problem: %s"
|
66
|
+
:error_watch_proxy: "The proxy has a problem: %s"
|
67
|
+
|
53
68
|
:error_invalid_argument: "%s is not a valid argument"
|
54
69
|
:error_unknown_value: "I don't know what to do with %s"
|
70
|
+
:error_missing_dependency: "%s is not available: %s"
|
71
|
+
:error_generic: "Oops, there was an unidentified problem (consider reporting it as a bug)"
|
72
|
+
|
73
|
+
:error_sysinfo_notunix: "Sorry, Unix only"
|
74
|
+
:error_sysinfo_notwindows: "Sorry, Windows only"
|
75
|
+
:error_sysinfo_notroot: "You need to be root to do that"
|
76
|
+
|
77
|
+
:error_watch_norubypcap: "Ruby-Pcap not installed (http://www.goto.info.waseda.ac.jp/~fukusima/ruby/pcap-e.html)"
|
@@ -0,0 +1,72 @@
|
|
1
|
+
h1. Frylock - Easy Command line apps
|
2
|
+
|
3
|
+
Inspired by "github-gem":http://github.com/defunkt/github-gem
|
4
|
+
|
5
|
+
*Install:*
|
6
|
+
|
7
|
+
gem install bmizerany-sinatra
|
8
|
+
|
9
|
+
*Example:*
|
10
|
+
|
11
|
+
Let's say we want to run our app like this:
|
12
|
+
|
13
|
+
<pre><code>ruby script.rb hello</pre></code>
|
14
|
+
|
15
|
+
Then we write this:
|
16
|
+
|
17
|
+
<pre><code>require 'rubygems'
|
18
|
+
require 'bmizerany-frylock'
|
19
|
+
|
20
|
+
command :hello do
|
21
|
+
puts "Hello!"
|
22
|
+
end</pre></code>
|
23
|
+
|
24
|
+
If we want and optional name:
|
25
|
+
|
26
|
+
<pre><code>ruby script.rb hello Blake</pre></code>
|
27
|
+
|
28
|
+
Add a command with proc that has an arity of 1 and it will be matched instead of the first command
|
29
|
+
|
30
|
+
<pre><code>require 'rubygems'
|
31
|
+
require 'bmizerany-frylock'
|
32
|
+
|
33
|
+
command :hello do
|
34
|
+
puts "Hello!"
|
35
|
+
end
|
36
|
+
|
37
|
+
command :hello do |name|
|
38
|
+
puts "Hello #{name}!"
|
39
|
+
end</pre></code>
|
40
|
+
|
41
|
+
|
42
|
+
For further examples see: bin/example
|
43
|
+
|
44
|
+
|
45
|
+
*Contribute:*
|
46
|
+
|
47
|
+
I'm very open to patches! Please fork and send a pull request.
|
48
|
+
|
49
|
+
*LICENSE*
|
50
|
+
|
51
|
+
Copyright (c) 2008 Blake Mizerany
|
52
|
+
|
53
|
+
Permission is hereby granted, free of charge, to any person
|
54
|
+
obtaining a copy of this software and associated documentation
|
55
|
+
files (the "Software"), to deal in the Software without
|
56
|
+
restriction, including without limitation the rights to use,
|
57
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
58
|
+
copies of the Software, and to permit persons to whom the
|
59
|
+
Software is furnished to do so, subject to the following
|
60
|
+
conditions:
|
61
|
+
|
62
|
+
The above copyright notice and this permission notice shall be
|
63
|
+
included in all copies or substantial portions of the Software.
|
64
|
+
|
65
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
66
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
67
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
68
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
69
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
70
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
71
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
72
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,170 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
FRYLOCK_HOME = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
4
|
+
$: << File.join(FRYLOCK_HOME, 'lib')
|
5
|
+
|
6
|
+
require 'rubygems'
|
7
|
+
require 'frylock'
|
8
|
+
|
9
|
+
default :commands
|
10
|
+
|
11
|
+
|
12
|
+
before do
|
13
|
+
# You can execute a block before the requests command is executed. You
|
14
|
+
# can use it to define instance variables here and use them in the commands
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
command :commands do
|
19
|
+
# Example: ruby bin/example welcome
|
20
|
+
|
21
|
+
puts "Meatwad: Science is a mystery to man, isn't it Frylock?"
|
22
|
+
print "Frylock: At least we have some commands: "
|
23
|
+
|
24
|
+
# The commands method returns a hash of Frylock::Command objects
|
25
|
+
puts commands.keys.inject([]) { |list, command| list << command.to_s }.sort.join(', ')
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
option :f, :found, "A boolean value. Did you find the car?"
|
30
|
+
command :findcar do |options|
|
31
|
+
# +options+ is a hash containing the options defined above
|
32
|
+
# Example: ruby bin/example -f findcar
|
33
|
+
|
34
|
+
puts "Frylock: So, did they ever find your car?"
|
35
|
+
|
36
|
+
# The keys to the hash are the long string from the option definition.
|
37
|
+
# If only the short string is provided, those will be used instead (i.e. :f).
|
38
|
+
puts (!options[:found]) ? "Carl: No" :
|
39
|
+
"Carl: Oh, they found part of it, hangin' from a trestle near the turnpike."
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
global_usage "USAGE: #{File.basename($0)} [global options] command [command options]"
|
45
|
+
global_option :s, :seconds, "Display values in seconds"
|
46
|
+
global_option :v, :verbose, "Verbosity level (i.e. -vvv is greater than -v)" do |v|
|
47
|
+
# Use instance variables to maintain values between option blocks.
|
48
|
+
# This will increment for every -v found (i.e. -vvv)
|
49
|
+
@val ||= 0
|
50
|
+
@val += 1
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
usage "ruby bin/example [--seconds] [-vv] time"
|
55
|
+
command :date do |options, argv, global_options|
|
56
|
+
# +argv+ contains the unnamed arguments
|
57
|
+
# +global_options+ contains hash of the options defined with global_options
|
58
|
+
|
59
|
+
require 'time'
|
60
|
+
now = Time.now
|
61
|
+
puts "More verbosely, the date is now: " if (global_options[:verbose] || 0) >= 2
|
62
|
+
puts (global_options[:seconds]) ? now.to_i : now.to_s
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
option :c, :check, "Check response codes for each URI"
|
67
|
+
option :d, :delim, String, "Output delimiter"
|
68
|
+
option :t, :timeout, Float, "Timeout value for HTTP request" do |v|
|
69
|
+
# You can provide an block to process the option value.
|
70
|
+
# This block must return the final value.
|
71
|
+
v = 10 if (v > 10)
|
72
|
+
v
|
73
|
+
end
|
74
|
+
|
75
|
+
usage 'echo "http://github.com/" | ruby bin/example process -c -d " " -t 15 http://solutious.com/'
|
76
|
+
command :processuris do |options, argv, global_options, stdin, cmd|
|
77
|
+
# +stdin+ is either an IO object or a custom object defined with a stdin block (see below)
|
78
|
+
# +cmd+ is the string used to evoke this command. Useful with alias_command (see below).
|
79
|
+
|
80
|
+
require 'net/http'
|
81
|
+
require 'uri'
|
82
|
+
require 'timeout'
|
83
|
+
|
84
|
+
uris = [stdin, argv].flatten # Combine the argv and stdin arrays
|
85
|
+
delim = options[:delim] || ','
|
86
|
+
timeout = options[:timeout] || 5
|
87
|
+
code = :notchecked # The default code when :check is false
|
88
|
+
|
89
|
+
if uris.empty?
|
90
|
+
puts "Frylock: You didn't provide any URIs. "
|
91
|
+
puts "Master Shake: Ya, see #{$0} #{cmd} -h"
|
92
|
+
exit 0
|
93
|
+
end
|
94
|
+
|
95
|
+
uris.each_with_index do |uri, index|
|
96
|
+
code = response_code(uri, timeout) if (options[:check])
|
97
|
+
puts [index+1, uri, code].join(delim)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
alias_command :process, :processuris
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
stdin do |stdin, output|
|
106
|
+
# Pre-process STDIN for all commands. This example returns an array of lines.
|
107
|
+
# The command processuris uses this array.
|
108
|
+
|
109
|
+
# We only want piped data. If this is not included
|
110
|
+
# execution will wait for input from the user.
|
111
|
+
unless stdin.tty?
|
112
|
+
|
113
|
+
while !stdin.eof? do
|
114
|
+
line = stdin.readline
|
115
|
+
line.chomp!
|
116
|
+
(output ||= []) << line
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
output
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
# response_code
|
126
|
+
#
|
127
|
+
# return the HTTP response code for the given URI
|
128
|
+
# +uri+ A valid HTTP URI
|
129
|
+
# +duration+ The timeout threshold (in seconds) for the request.
|
130
|
+
def response_code(uri_str, duration=5)
|
131
|
+
response = :unavailable
|
132
|
+
begin
|
133
|
+
uri = (uri_str.kind_of? URI::HTTP) ? uri_str : URI.parse(uri_str)
|
134
|
+
timeout(duration) do
|
135
|
+
response = Net::HTTP.get_response(uri).code
|
136
|
+
end
|
137
|
+
rescue Exception => ex
|
138
|
+
end
|
139
|
+
response
|
140
|
+
end
|
141
|
+
|
142
|
+
|
143
|
+
at_exit do
|
144
|
+
# This is an example of how to call Frylock in your script.
|
145
|
+
begin
|
146
|
+
Frylock.run!(ARGV, STDIN)
|
147
|
+
|
148
|
+
rescue Frylock::UnknownCommand => ex
|
149
|
+
STDERR.puts "Frylock: I don't know what the #{ex.name} command is. #{$/}"
|
150
|
+
STDERR.puts "Master Shake: I'll tell you what it is, friends... it's shut up and let me eat it."
|
151
|
+
|
152
|
+
rescue Frylock::NoCommandsDefined => ex
|
153
|
+
STDERR.puts "Frylock: Carl, I don't want it. And I'd appreciate it if you'd define at least one command. #{$/}"
|
154
|
+
STDERR.puts "Carl: Fryman, don't be that way! This sorta thing happens every day! People just don't... you know, talk about it this loud."
|
155
|
+
|
156
|
+
rescue Frylock::InvalidArgument => ex
|
157
|
+
STDERR.puts "Frylock: Shake, how many arguments have you not provided a value for this year? #{$/}"
|
158
|
+
STDERR.puts "Master Shake: A *lot* more than *you* have! (#{@args.join(', ')})"
|
159
|
+
|
160
|
+
rescue Frylock::MissingArgument => ex
|
161
|
+
STDERR.puts "Frylock: I don't know what #{ex.args.join(', ')} is. #{$/}"
|
162
|
+
STDERR.puts "Master Shake: I'll tell you what it is, friends... it's shut up and let me eat it."
|
163
|
+
|
164
|
+
rescue => ex
|
165
|
+
STDERR.puts "Master Shake: Okay, but when we go in, watch your step. "
|
166
|
+
STDERR.puts "Frylock: Why?"
|
167
|
+
STDERR.puts "Meatwad: [explosion] #{ex.message}"
|
168
|
+
STDERR.puts ex.backtrace
|
169
|
+
end
|
170
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = %q{frylock}
|
3
|
+
s.version = "0.1.0"
|
4
|
+
|
5
|
+
s.specification_version = 1 if s.respond_to? :specification_version=
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Blake Mizerany"]
|
9
|
+
s.date = %q{2008-08-17}
|
10
|
+
s.description = %q{Command line apps made easy}
|
11
|
+
s.email = %q{blake.mizerany@gmail.com}
|
12
|
+
s.files = Dir["lib/*.rb"]
|
13
|
+
s.has_rdoc = false
|
14
|
+
s.homepage = %q{http://github.com/bmizerany/frylock}
|
15
|
+
s.require_paths = ["lib"]
|
16
|
+
s.rubygems_version = %q{1.1.1}
|
17
|
+
s.summary = %q{Command line apps made easy}
|
18
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Frylock
|
2
|
+
|
3
|
+
class UnknownCommand < RuntimeError
|
4
|
+
attr_reader :name
|
5
|
+
def initialize(name)
|
6
|
+
@name = name || :unknown
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class NoCommandsDefined < RuntimeError
|
11
|
+
end
|
12
|
+
|
13
|
+
class InvalidArgument < RuntimeError
|
14
|
+
attr_accessor :args
|
15
|
+
def initialize(args)
|
16
|
+
# We grab just the name of the argument
|
17
|
+
@args = args || []
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class MissingArgument < InvalidArgument
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|