curl-spawn 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +97 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/curl-spawn.gemspec +24 -0
- data/example.rb +109 -0
- data/exe/curl-spawn-generate +3 -0
- data/exe/curl-spawn-irb +30 -0
- data/lib/curl/spawn/args.rb +15 -0
- data/lib/curl/spawn/args_builder.rb +131 -0
- data/lib/curl/spawn/example.rb +153 -0
- data/lib/curl/spawn/version.rb +5 -0
- data/lib/curl/spawn.rb +39 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7676051058f1b4763f5a22e603b71561f1ccb6b3
|
4
|
+
data.tar.gz: 8f1a5765a4d41861aaa1660801e1e0c55c19f1af
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a50aed177c4f47f3a0fb3e93ed3c170f5a9d29c9f5f89747a995cb742bfe4aae4dc904a49af982651a367b65d9e93ddd0a4dd76e27c72c43d49b0f9ad4c60eeb
|
7
|
+
data.tar.gz: 0fa807255c489c528d8d479fdbf4288aa7648d36ef6ba31bc82fb63577c5dffa6d1f2c431b7304a7ed2be65ec05371a748f4a82b77bd81628f96cc8e2befce68
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 jbreeden
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
# curl-spawn
|
2
|
+
|
3
|
+
Simple DSL for invoking curl commands. Includes a script generator - `curl-spawn-generate` -
|
4
|
+
and an interactive shell - `curl-spawn-irb`.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'curl-spawn'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install curl-spawn
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
### Ruby API
|
25
|
+
|
26
|
+
`curl-spawn-irb` will show a short API example at startup.
|
27
|
+
At the time of this writing, it looks like this:
|
28
|
+
|
29
|
+
```Sh
|
30
|
+
[jared:~/projects/curl-spawn] curl-spawn-irb
|
31
|
+
--------------------
|
32
|
+
Welcome to curl-spawn-irb!
|
33
|
+
|
34
|
+
This is an interactive ruby shell with curl-spawn loaded.
|
35
|
+
Here, you can spawn curl processes with a convenient ruby DSL.
|
36
|
+
|
37
|
+
Example:
|
38
|
+
|
39
|
+
# - The block is executed in the context of a Curl::Spawn::ArgsBuilder, which
|
40
|
+
# provides a DSL for describing curl commands.
|
41
|
+
#
|
42
|
+
# - Any parameters to `Curl.spawn` are merged with the arguments derived
|
43
|
+
# from the DSL block, then passed through to `Kernel.spawn` when invoking curl.
|
44
|
+
# (Arguments derived from the block are preferred in case of conflicts.)
|
45
|
+
#
|
46
|
+
# + You can use this to set positional arguments, redirect stdin/stdout/stderr,
|
47
|
+
# and a few other things. (Note: the env hash argument to `Kernel.spawn` is
|
48
|
+
# not supported through this api.)
|
49
|
+
|
50
|
+
pid = Curl.spawn('--progress-bar', out: $stdout, err: $stderr) {
|
51
|
+
https # Use https. Same as `scheme 'https'`. (default: http)
|
52
|
+
ssl_no_verify # Don't verify ssl certs. (Primarily for development)
|
53
|
+
|
54
|
+
user $opt[:user] if $opt[:user] # Set the username to use in basic auth
|
55
|
+
password 'abcd1234' # Set the password to use for basic auth
|
56
|
+
|
57
|
+
verb :post # Set the http method/verb (default: 'GET')
|
58
|
+
host 'localhost' # Specify host (default: 'localhost')
|
59
|
+
port 8000 # On port 8000 (default: 80)
|
60
|
+
path '/' # The path segment of the url (default: '/')
|
61
|
+
|
62
|
+
query :param => Curl.url_encode('value') # Set query params (alias: queries)
|
63
|
+
header :Accept => 'application/json' # Set headers (alias: headers)
|
64
|
+
|
65
|
+
content $stdin # IO object or string to use as the request content.
|
66
|
+
# Note that this option overwrite the `:in` option
|
67
|
+
# from the positional arguments to this method.
|
68
|
+
|
69
|
+
# For debugging, you can print out the generated Curl::Spawn::Args object
|
70
|
+
$stderr.puts build!.inspect
|
71
|
+
}
|
72
|
+
--------------------
|
73
|
+
[1] pry(main)>
|
74
|
+
```
|
75
|
+
|
76
|
+
### Shell Scripts
|
77
|
+
|
78
|
+
The main reason for writing this gem was to ease the task of writing small
|
79
|
+
shell scripts whose main function is using curl to hit a REST endpoint.
|
80
|
+
So the `curl-spawn-generate` command is provided, which generates just
|
81
|
+
such a script.
|
82
|
+
|
83
|
+
`curl-spawn-generate > `[example.rb](./example.rb)
|
84
|
+
|
85
|
+
## Development
|
86
|
+
|
87
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
88
|
+
|
89
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
90
|
+
|
91
|
+
## Contributing
|
92
|
+
|
93
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jbreeden/curl-spawn.
|
94
|
+
|
95
|
+
## License
|
96
|
+
|
97
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "curl/spawn"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
require "pry"
|
11
|
+
Pry.start
|
12
|
+
|
13
|
+
# require "irb"
|
14
|
+
# IRB.start
|
data/bin/setup
ADDED
data/curl-spawn.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'curl/spawn/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "curl-spawn"
|
8
|
+
spec.version = Curl::Spawn::VERSION
|
9
|
+
spec.authors = ["jbreeden"]
|
10
|
+
spec.email = ["jared.breeden@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Utilities for spawning curl processes}
|
13
|
+
spec.homepage = "http://github.com/jbreeden/curl-spawn"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "pry"
|
24
|
+
end
|
data/example.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'curl/spawn'
|
4
|
+
|
5
|
+
# User Manual
|
6
|
+
# -----------
|
7
|
+
|
8
|
+
def help(status = 0)
|
9
|
+
puts <<-EOS
|
10
|
+
Usage:
|
11
|
+
#{File.basename(__FILE__)} [[--]help] [OPTION...] -- [CURL_ARG...]
|
12
|
+
|
13
|
+
OPTION:
|
14
|
+
-u,--user=USER[:PASSWORD]
|
15
|
+
Username & optional password to authenticate with.
|
16
|
+
|
17
|
+
-h,--host=HOST
|
18
|
+
The host to send the request to (default: localhost).
|
19
|
+
|
20
|
+
-p,--port=PORT
|
21
|
+
The port to send the request to (default: 80).
|
22
|
+
|
23
|
+
-c,--content=FILENAME
|
24
|
+
The file to send as the request body. If FILENAME is a hyphen (-), then
|
25
|
+
stdin is used.
|
26
|
+
|
27
|
+
CURL_ARG:
|
28
|
+
Any additional arguments after a -- are passed straight through to curl.
|
29
|
+
EOS
|
30
|
+
exit status
|
31
|
+
end
|
32
|
+
|
33
|
+
# Argument Parsing
|
34
|
+
# ----------------
|
35
|
+
|
36
|
+
$opt = {
|
37
|
+
user: nil,
|
38
|
+
password: nil,
|
39
|
+
host: 'localhost',
|
40
|
+
port: 80,
|
41
|
+
content: nil
|
42
|
+
}
|
43
|
+
|
44
|
+
in_curl_args = false
|
45
|
+
curl_args = []
|
46
|
+
ARGV.dup.each do |arg|
|
47
|
+
if !in_curl_args
|
48
|
+
case arg
|
49
|
+
when '--help', 'help'
|
50
|
+
help(0)
|
51
|
+
when /^(?:-u|--user)=?(.*)$/
|
52
|
+
$opt[:user], $opt[:password] = $1.match(/([^:]*):?(.*)/).captures
|
53
|
+
ARGV.delete(arg)
|
54
|
+
when /^(?:-h|--host)=?(.*)$/
|
55
|
+
$opt[:host] = $1
|
56
|
+
ARGV.delete(arg)
|
57
|
+
when /^(?:-p|--port)=?(.*)$/
|
58
|
+
$opt[:port] = $1
|
59
|
+
ARGV.delete(arg)
|
60
|
+
when /^(?:-c|--content)=?(.*)$/
|
61
|
+
prev = $opt[:content]
|
62
|
+
prev.close if prev && prev != $stdin
|
63
|
+
if $1 == '-'
|
64
|
+
$opt[:content] = $stdin
|
65
|
+
else
|
66
|
+
$opt[:content] = File.open($1, 'r')
|
67
|
+
end
|
68
|
+
ARGV.delete(arg)
|
69
|
+
when '--'
|
70
|
+
in_curl_args = true
|
71
|
+
ARGV.delete(arg)
|
72
|
+
end
|
73
|
+
else
|
74
|
+
curl_args.push(arg)
|
75
|
+
ARGV.delete(arg)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
if !ARGV.empty?
|
80
|
+
$stderr.puts "Unexpected arguments: #{ARGV.inspect}"
|
81
|
+
help(1)
|
82
|
+
end
|
83
|
+
|
84
|
+
# Curl Invocation
|
85
|
+
# ---------------
|
86
|
+
|
87
|
+
pid = Curl.spawn(*curl_args, out: $stdout, err: $stderr) {
|
88
|
+
https
|
89
|
+
ssl_no_verify
|
90
|
+
|
91
|
+
user $opt[:user] unless $opt[:user].nil? || $opt[:user].empty?
|
92
|
+
password $opt[:password] unless $opt[:password].nil? || $opt[:password].empty?
|
93
|
+
|
94
|
+
verb 'POST'
|
95
|
+
host $opt[:host]
|
96
|
+
port $opt[:port]
|
97
|
+
path '/'
|
98
|
+
|
99
|
+
header 'Content-Type' => 'application/json'
|
100
|
+
header 'Accept' => 'application/json'
|
101
|
+
|
102
|
+
content $opt[:content] if $opt[:content]
|
103
|
+
|
104
|
+
# # Debug:
|
105
|
+
# $stderr.puts build!.inspect
|
106
|
+
}
|
107
|
+
|
108
|
+
Process.wait(pid)
|
109
|
+
exit $?.exitstatus
|
data/exe/curl-spawn-irb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'curl/spawn'
|
4
|
+
require 'curl/spawn/example'
|
5
|
+
|
6
|
+
welcome = <<-eos
|
7
|
+
--------------------
|
8
|
+
Welcome to curl-spawn-irb!
|
9
|
+
|
10
|
+
This is an interactive ruby shell with curl-spawn loaded.
|
11
|
+
Here, you can spawn curl processes with a convenient ruby DSL.
|
12
|
+
|
13
|
+
Example: ```
|
14
|
+
|
15
|
+
#{Curl::Spawn::EXAMPLE.each_line.map { |l| " #{l}"}.join("")}
|
16
|
+
|
17
|
+
```
|
18
|
+
--------------------
|
19
|
+
eos
|
20
|
+
|
21
|
+
begin
|
22
|
+
require 'pry'
|
23
|
+
puts welcome
|
24
|
+
self.pry
|
25
|
+
rescue Exception => ex
|
26
|
+
$stderr.puts "Tip: `gem install pry` for a nicer shell."
|
27
|
+
puts welcome
|
28
|
+
require 'irb'
|
29
|
+
IRB.start
|
30
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require 'erb'
|
2
|
+
require 'curl/spawn/args'
|
3
|
+
|
4
|
+
module Curl
|
5
|
+
module Spawn
|
6
|
+
|
7
|
+
class ArgsBuilder
|
8
|
+
def initialize
|
9
|
+
@user = nil
|
10
|
+
@password = nil
|
11
|
+
@scheme = 'http'
|
12
|
+
@ssl_no_verify = false
|
13
|
+
@host = 'localhost'
|
14
|
+
@port = 80
|
15
|
+
@path = '/'
|
16
|
+
@verb = 'GET'
|
17
|
+
@queries = {}
|
18
|
+
@headers = {}
|
19
|
+
@data = nil
|
20
|
+
end
|
21
|
+
|
22
|
+
def user(user)
|
23
|
+
@user = user
|
24
|
+
end
|
25
|
+
alias user= user
|
26
|
+
alias username user
|
27
|
+
alias username= user
|
28
|
+
|
29
|
+
def password(p)
|
30
|
+
@password = p
|
31
|
+
end
|
32
|
+
alias password= password
|
33
|
+
|
34
|
+
def scheme(h)
|
35
|
+
@scheme = h
|
36
|
+
end
|
37
|
+
alias scheme= scheme
|
38
|
+
|
39
|
+
def http
|
40
|
+
@scheme = 'http'
|
41
|
+
end
|
42
|
+
|
43
|
+
def https
|
44
|
+
@scheme = 'https'
|
45
|
+
end
|
46
|
+
|
47
|
+
def ssl_no_verify(val = true)
|
48
|
+
@ssl_no_verify = val
|
49
|
+
end
|
50
|
+
alias ssl_no_verify= ssl_no_verify
|
51
|
+
|
52
|
+
def host(h)
|
53
|
+
@host = h
|
54
|
+
end
|
55
|
+
alias host= host
|
56
|
+
|
57
|
+
def port(p)
|
58
|
+
@port = p
|
59
|
+
end
|
60
|
+
alias port= port
|
61
|
+
|
62
|
+
def path(p)
|
63
|
+
p = "/#{p}" unless p.start_with?('/')
|
64
|
+
@path = p
|
65
|
+
end
|
66
|
+
alias path= path
|
67
|
+
|
68
|
+
def verb(v)
|
69
|
+
@verb = v
|
70
|
+
end
|
71
|
+
alias verb= verb
|
72
|
+
|
73
|
+
def query(q)
|
74
|
+
@queries.merge!(q)
|
75
|
+
end
|
76
|
+
alias queries query
|
77
|
+
|
78
|
+
def header(h)
|
79
|
+
@headers.merge!(h)
|
80
|
+
end
|
81
|
+
alias headers header
|
82
|
+
|
83
|
+
def data(d)
|
84
|
+
@data =d
|
85
|
+
end
|
86
|
+
alias data= data
|
87
|
+
alias content data
|
88
|
+
alias content= data
|
89
|
+
|
90
|
+
def build!
|
91
|
+
args = ::Curl::Spawn::Args.new
|
92
|
+
|
93
|
+
args.argv.push('curl')
|
94
|
+
args.argv.push('-k') if @ssl_no_verify
|
95
|
+
args.argv.push('--user') unless @user.nil?
|
96
|
+
args.argv.push("#{@user}#{":#{@password}" unless @password.nil?}")
|
97
|
+
|
98
|
+
query_str = ''
|
99
|
+
@queries.each do |k, v|
|
100
|
+
if query_str == ''
|
101
|
+
query_str << '?'
|
102
|
+
else
|
103
|
+
query_str << '&'
|
104
|
+
end
|
105
|
+
query_str << "#{ERB::Util.url_encode(k)}=#{ERB::Util.url_encode(v)}"
|
106
|
+
end
|
107
|
+
|
108
|
+
args.argv.push('-X')
|
109
|
+
args.argv.push(@verb.upcase)
|
110
|
+
|
111
|
+
args.argv.push("#{@scheme}://#{@host}:#{@port}#{@path}#{query_str}")
|
112
|
+
|
113
|
+
@headers.each do |k, v|
|
114
|
+
args.argv.push("-H")
|
115
|
+
args.argv.push("'#{k}: #{v}'")
|
116
|
+
end
|
117
|
+
|
118
|
+
if @data
|
119
|
+
args.argv.push('--data-binary')
|
120
|
+
args.argv.push('@-')
|
121
|
+
args.opt[:in] = @data
|
122
|
+
end
|
123
|
+
|
124
|
+
args.argv = args.argv.reject { |arg| arg.nil? || arg.empty? }.map { |arg| arg.to_s }
|
125
|
+
|
126
|
+
args
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
module Curl
|
2
|
+
module Spawn
|
3
|
+
|
4
|
+
EXAMPLE = <<-eos
|
5
|
+
# - The block is executed in the context of a Curl::Spawn::ArgsBuilder, which
|
6
|
+
# provides a DSL for describing curl commands.
|
7
|
+
#
|
8
|
+
# - Any parameters to `Curl.spawn` are merged with the arguments derived
|
9
|
+
# from the DSL block, then passed through to `Kernel.spawn` when invoking curl.
|
10
|
+
# (Arguments derived from the block are preferred in case of conflicts.)
|
11
|
+
#
|
12
|
+
# + You can use this to set positional arguments, redirect stdin/stdout/stderr,
|
13
|
+
# and a few other things. (Note: the env hash argument to `Kernel.spawn` is
|
14
|
+
# not supported through this api.)
|
15
|
+
|
16
|
+
pid = Curl.spawn('--progress-bar', out: $stdout, err: $stderr) {
|
17
|
+
https # Use https. Same as `scheme 'https'`. (default: http)
|
18
|
+
ssl_no_verify # Don't verify ssl certs. (Primarily for development)
|
19
|
+
|
20
|
+
user $opt[:user] if $opt[:user] # Set the username to use for authentication
|
21
|
+
password 'abcd1234' # Set the password to use for authentication
|
22
|
+
|
23
|
+
verb :post # Set the http method/verb (default: 'GET')
|
24
|
+
host 'localhost' # Specify host (default: 'localhost')
|
25
|
+
port 8000 # On port 8000 (default: 80)
|
26
|
+
path '/' # The path segment of the url (default: '/')
|
27
|
+
|
28
|
+
query :param => Curl.url_encode('value') # Set query params (alias: queries)
|
29
|
+
header :Accept => 'application/json' # Set headers (alias: headers)
|
30
|
+
|
31
|
+
content $stdin # IO object or string to use as the request content.
|
32
|
+
# Note that this option overwrite the `:in` option
|
33
|
+
# from the positional arguments to this method.
|
34
|
+
|
35
|
+
# For debugging, you can print out the generated Curl::Spawn::Args object
|
36
|
+
$stderr.puts build!.inspect
|
37
|
+
}
|
38
|
+
eos
|
39
|
+
|
40
|
+
EXAMPLE_SCRIPT = <<-eos
|
41
|
+
#! /usr/bin/env ruby
|
42
|
+
|
43
|
+
require 'curl/spawn'
|
44
|
+
|
45
|
+
# User Manual
|
46
|
+
# -----------
|
47
|
+
|
48
|
+
def help(status = 0)
|
49
|
+
puts <<-EOS
|
50
|
+
Usage:
|
51
|
+
\#{File.basename(__FILE__)} [[--]help] [OPTION...] -- [CURL_ARG...]
|
52
|
+
|
53
|
+
OPTION:
|
54
|
+
-u,--user=USER[:PASSWORD]
|
55
|
+
Username & optional password to authenticate with.
|
56
|
+
|
57
|
+
-h,--host=HOST
|
58
|
+
The host to send the request to (default: localhost).
|
59
|
+
|
60
|
+
-p,--port=PORT
|
61
|
+
The port to send the request to (default: 80).
|
62
|
+
|
63
|
+
-c,--content=FILENAME
|
64
|
+
The file to send as the request body. If FILENAME is a hyphen (-), then
|
65
|
+
stdin is used.
|
66
|
+
|
67
|
+
CURL_ARG:
|
68
|
+
Any additional arguments after a -- are passed straight through to curl.
|
69
|
+
EOS
|
70
|
+
exit status
|
71
|
+
end
|
72
|
+
|
73
|
+
# Argument Parsing
|
74
|
+
# ----------------
|
75
|
+
|
76
|
+
$opt = {
|
77
|
+
user: nil,
|
78
|
+
password: nil,
|
79
|
+
host: 'localhost',
|
80
|
+
port: 80,
|
81
|
+
content: nil
|
82
|
+
}
|
83
|
+
|
84
|
+
in_curl_args = false
|
85
|
+
curl_args = []
|
86
|
+
ARGV.dup.each do |arg|
|
87
|
+
if !in_curl_args
|
88
|
+
case arg
|
89
|
+
when '--help', 'help'
|
90
|
+
help(0)
|
91
|
+
when /^(?:-u|--user)=?(.*)$/
|
92
|
+
$opt[:user], $opt[:password] = $1.match(/([^:]*):?(.*)/).captures
|
93
|
+
ARGV.delete(arg)
|
94
|
+
when /^(?:-h|--host)=?(.*)$/
|
95
|
+
$opt[:host] = $1
|
96
|
+
ARGV.delete(arg)
|
97
|
+
when /^(?:-p|--port)=?(.*)$/
|
98
|
+
$opt[:port] = $1
|
99
|
+
ARGV.delete(arg)
|
100
|
+
when /^(?:-c|--content)=?(.*)$/
|
101
|
+
prev = $opt[:content]
|
102
|
+
prev.close if prev && prev != $stdin
|
103
|
+
if $1 == '-'
|
104
|
+
$opt[:content] = $stdin
|
105
|
+
else
|
106
|
+
$opt[:content] = File.open($1, 'r')
|
107
|
+
end
|
108
|
+
ARGV.delete(arg)
|
109
|
+
when '--'
|
110
|
+
in_curl_args = true
|
111
|
+
ARGV.delete(arg)
|
112
|
+
end
|
113
|
+
else
|
114
|
+
curl_args.push(arg)
|
115
|
+
ARGV.delete(arg)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
if !ARGV.empty?
|
120
|
+
$stderr.puts "Unexpected arguments: \#{ARGV.inspect}"
|
121
|
+
help(1)
|
122
|
+
end
|
123
|
+
|
124
|
+
# Curl Invocation
|
125
|
+
# ---------------
|
126
|
+
|
127
|
+
pid = Curl.spawn(*curl_args, out: $stdout, err: $stderr) {
|
128
|
+
https
|
129
|
+
ssl_no_verify
|
130
|
+
|
131
|
+
user $opt[:user] unless $opt[:user].nil? || $opt[:user].empty?
|
132
|
+
password $opt[:password] unless $opt[:password].nil? || $opt[:password].empty?
|
133
|
+
|
134
|
+
verb 'POST'
|
135
|
+
host $opt[:host]
|
136
|
+
port $opt[:port]
|
137
|
+
path '/'
|
138
|
+
|
139
|
+
header 'Content-Type' => 'application/json'
|
140
|
+
header 'Accept' => 'application/json'
|
141
|
+
|
142
|
+
content $opt[:content] if $opt[:content]
|
143
|
+
|
144
|
+
# # Debug:
|
145
|
+
# $stderr.puts build!.inspect
|
146
|
+
}
|
147
|
+
|
148
|
+
Process.wait(pid)
|
149
|
+
exit $?.exitstatus
|
150
|
+
eos
|
151
|
+
|
152
|
+
end
|
153
|
+
end
|
data/lib/curl/spawn.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require "curl/spawn/version"
|
2
|
+
require "curl/spawn/args_builder"
|
3
|
+
require 'erb'
|
4
|
+
|
5
|
+
module Curl
|
6
|
+
module Spawn
|
7
|
+
def self.build_args(&block)
|
8
|
+
builder = ArgsBuilder.new
|
9
|
+
builder.instance_eval(&block) if block
|
10
|
+
builder.build!
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.spawn(*supplied_argv, &block)
|
15
|
+
built_args = Curl::Spawn.build_args(&block)
|
16
|
+
|
17
|
+
normalized_argv = []
|
18
|
+
|
19
|
+
supplied_opt = supplied_argv.last.kind_of?(Hash) ? supplied_argv.pop : {}
|
20
|
+
|
21
|
+
# Merge the argv contents
|
22
|
+
built_args.argv.each do |arg|
|
23
|
+
normalized_argv.push(arg.to_s)
|
24
|
+
end
|
25
|
+
|
26
|
+
supplied_argv.each do |arg|
|
27
|
+
normalized_argv.push(arg.to_s)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Merge the spawn options hash argument
|
31
|
+
normalized_argv.push(supplied_opt.merge(built_args.opt))
|
32
|
+
|
33
|
+
Kernel.spawn(*normalized_argv)
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.url_encode(str)
|
37
|
+
ERB::Util.url_encode(str)
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: curl-spawn
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- jbreeden
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- jared.breeden@gmail.com
|
58
|
+
executables:
|
59
|
+
- curl-spawn-generate
|
60
|
+
- curl-spawn-irb
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- .gitignore
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- curl-spawn.gemspec
|
72
|
+
- example.rb
|
73
|
+
- exe/curl-spawn-generate
|
74
|
+
- exe/curl-spawn-irb
|
75
|
+
- lib/curl/spawn.rb
|
76
|
+
- lib/curl/spawn/args.rb
|
77
|
+
- lib/curl/spawn/args_builder.rb
|
78
|
+
- lib/curl/spawn/example.rb
|
79
|
+
- lib/curl/spawn/version.rb
|
80
|
+
homepage: http://github.com/jbreeden/curl-spawn
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.0.14
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: Utilities for spawning curl processes
|
104
|
+
test_files: []
|
105
|
+
has_rdoc:
|