faraday-cli 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/TODO +1 -0
- data/VERSION +1 -1
- data/bin/faraday-cli +13 -97
- data/lib/faraday/cli/middleware/verbose_request.rb +1 -1
- data/lib/faraday/cli/middleware/verbose_response.rb +12 -0
- data/lib/faraday/cli/middleware.rb +1 -0
- data/lib/faraday/cli/option/parser.rb +106 -0
- data/lib/faraday/cli/option/validator.rb +19 -0
- data/lib/faraday/cli/option.rb +4 -0
- data/lib/faraday/cli.rb +2 -1
- metadata +5 -2
- data/lib/faraday/cli/response_formatter.rb +0 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f4d394529ed718ce41e89f26fb9263c3ebaaf07
|
4
|
+
data.tar.gz: 26d37f005429916ac2991e7c80f4bc182d291414
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6db00733558102de7f8b984db34d53ad9e369ef1cc352d14f02da469d6e426fa8d6b8c4b51ff995c6e1c521bb802fa0c47a122061e670c84efe6232ff6a028a7
|
7
|
+
data.tar.gz: 380aab8b2a7cdd5d7d328e22876348551976acd80501a3964dfaaeb12007e57c9af0da70ab25d0b475b8808e265e674bc44fb3225a378aa23a11b612224c404a
|
data/TODO
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.7.0
|
data/bin/faraday-cli
CHANGED
@@ -3,100 +3,10 @@ if ENV['FARADAY_CLI_DEVELOPER_ENV'] == 'true'
|
|
3
3
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
4
4
|
end
|
5
5
|
|
6
|
-
require 'optparse'
|
7
6
|
require 'faraday/cli'
|
8
7
|
|
9
|
-
CLI_OPTIONS =
|
10
|
-
CLI_OPTIONS
|
11
|
-
OptionParser.new do |o|
|
12
|
-
|
13
|
-
o.banner.concat(' <url>')
|
14
|
-
|
15
|
-
o.on('-V', '--version', 'Show version number and quit') { $stdout.puts(Faraday::CLI::VERSION); exit }
|
16
|
-
|
17
|
-
CLI_OPTIONS[:http_method]= 'get'
|
18
|
-
o.on('-X', '--request COMMAND', 'Specify http request command to use') do |http_method|
|
19
|
-
CLI_OPTIONS[:http_method]= http_method.to_s.strip.downcase
|
20
|
-
end
|
21
|
-
|
22
|
-
CLI_OPTIONS[:http_headers]= []
|
23
|
-
o.on('-H', '--header HEADER:VALUE', 'Pass custom header LINE to server (H)') do |header|
|
24
|
-
CLI_OPTIONS[:http_headers].push(header.split(':'))
|
25
|
-
end
|
26
|
-
|
27
|
-
CLI_OPTIONS[:params]= []
|
28
|
-
o.on('-q', '--query key=value', 'Pass Query key values to use in the request') do |raw_query_pair|
|
29
|
-
CLI_OPTIONS[:params].push(raw_query_pair.split('='))
|
30
|
-
end
|
31
|
-
|
32
|
-
o.on('-d', '--data PAYLOAD_STRING', 'HTTP POST data (H)') { |payload| CLI_OPTIONS[:body]= payload }
|
33
|
-
|
34
|
-
o.on('--upload_file KEY=FILE_PATH[:CONTENT_TYPE]', 'Pass File upload io in the request pointing to the given file') do |payload_file_path|
|
35
|
-
|
36
|
-
CLI_OPTIONS[:flags] << :multipart
|
37
|
-
|
38
|
-
parts = payload_file_path.split('=')
|
39
|
-
raw_file_path = parts.pop
|
40
|
-
key = parts.first
|
41
|
-
|
42
|
-
file_path, content_type = raw_file_path.split(':')
|
43
|
-
content_type ||= 'application/octet-stream'
|
44
|
-
|
45
|
-
file_stream_io = Faraday::UploadIO.new(File.realpath(file_path), content_type)
|
46
|
-
|
47
|
-
CLI_OPTIONS[:body] = {key => file_stream_io}
|
48
|
-
|
49
|
-
end
|
50
|
-
|
51
|
-
o.on('-A', '--user-agent STRING', 'Send User-Agent STRING to server (H)') do |user_agent|
|
52
|
-
CLI_OPTIONS[:http_headers] << ['User-Agent', user_agent]
|
53
|
-
end
|
54
|
-
|
55
|
-
o.on('-o', '--output FILE_PATH', 'Write to FILE instead of stdout') do |out_file_path|
|
56
|
-
$stdout.reopen(out_file_path, 'a+')
|
57
|
-
$stderr.reopen(out_file_path, 'a+')
|
58
|
-
end
|
59
|
-
|
60
|
-
# o.on('-x', '--proxy HOST:PORT', 'HOST[:PORT] Use proxy on given port') do |host_port_str|
|
61
|
-
# host, port = host_port_str.split(':')
|
62
|
-
# port = '80' if port.nil?
|
63
|
-
#
|
64
|
-
# CLI_OPTIONS[:proxy]= {host: host, port: port}
|
65
|
-
# end
|
66
|
-
|
67
|
-
o.on('-v', '--verbose', 'Make the operation more talkative') do
|
68
|
-
CLI_OPTIONS[:flags] << :verbose
|
69
|
-
end
|
70
|
-
|
71
|
-
o.on('-s', '--silent', "Silent mode (don't output anything)") do
|
72
|
-
CLI_OPTIONS[:flags] << :silent
|
73
|
-
$stdout.reopen('/dev/null', 'a+')
|
74
|
-
$stderr.reopen('/dev/null', 'a+')
|
75
|
-
|
76
|
-
end
|
77
|
-
|
78
|
-
CLI_OPTIONS[:config_file_paths]= []
|
79
|
-
o.on('-K', '--config FILE_PATH', 'File path to the .faraday.rb if you want use other than default') do |file_path|
|
80
|
-
CLI_OPTIONS[:config_file_paths] << File.absolute_path(file_path)
|
81
|
-
end
|
82
|
-
|
83
|
-
o.on('-M', '--middlewares', 'Show current middleware stack') do
|
84
|
-
CLI_OPTIONS[:flags] << :show_middlewares
|
85
|
-
end
|
86
|
-
|
87
|
-
o.on('-W', '--without_middlewares', 'Make request without consuming middleware file(s)') do
|
88
|
-
CLI_OPTIONS[:flags] << :without_middlewares
|
89
|
-
end
|
90
|
-
|
91
|
-
# Z
|
92
|
-
|
93
|
-
o.parse!
|
94
|
-
|
95
|
-
end
|
96
|
-
|
97
|
-
ALLOWED_HTTP_METHODS = %w(get head post put patch delete options)
|
98
|
-
raise('invalid http method given') unless ALLOWED_HTTP_METHODS.include?(CLI_OPTIONS[:http_method])
|
99
|
-
is_verbose = !CLI_OPTIONS[:flags].include?(:silent) && CLI_OPTIONS[:flags].include?(:verbose)
|
8
|
+
CLI_OPTIONS = Faraday::CLI::Option::Parser.new.parse!
|
9
|
+
Faraday::CLI::Option::Validator.validate(CLI_OPTIONS)
|
100
10
|
|
101
11
|
connection = Faraday.new do |builder|
|
102
12
|
builder.use Faraday::Response::RaiseError
|
@@ -107,12 +17,16 @@ connection = Faraday.new do |builder|
|
|
107
17
|
|
108
18
|
builder.request(:multipart) if CLI_OPTIONS[:flags].include?(:multipart)
|
109
19
|
|
110
|
-
if
|
111
|
-
builder.use Faraday::CLI::Middleware::VerboseRequest
|
20
|
+
if CLI_OPTIONS[:flags].include?(:verbose)
|
112
21
|
builder.response :logger
|
113
22
|
end
|
114
23
|
|
115
|
-
|
24
|
+
if CLI_OPTIONS[:flags].include?(:super_verbose)
|
25
|
+
builder.use Faraday::CLI::Middleware::VerboseRequest
|
26
|
+
builder.use Faraday::CLI::Middleware::VerboseResponse
|
27
|
+
end
|
28
|
+
|
29
|
+
builder.adapter(Faraday.default_adapter)
|
116
30
|
end
|
117
31
|
|
118
32
|
if CLI_OPTIONS[:flags].include?(:show_middlewares)
|
@@ -121,6 +35,7 @@ if CLI_OPTIONS[:flags].include?(:show_middlewares)
|
|
121
35
|
end
|
122
36
|
|
123
37
|
FARADAY_ACTIVE_CONNECTION= connection
|
38
|
+
|
124
39
|
def active_connection
|
125
40
|
FARADAY_ACTIVE_CONNECTION
|
126
41
|
end
|
@@ -144,10 +59,11 @@ begin
|
|
144
59
|
|
145
60
|
end
|
146
61
|
|
147
|
-
$stdout.puts(
|
62
|
+
$stdout.puts(response.body)
|
148
63
|
|
149
|
-
rescue Faraday::
|
64
|
+
rescue Faraday::ClientError => ex
|
150
65
|
$stdout.puts(ex.message)
|
66
|
+
$stdout.puts(ex.response[:body]) unless ex.response.nil?
|
151
67
|
exit(1)
|
152
68
|
|
153
69
|
rescue URI::InvalidURIError => ex
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class Faraday::CLI::Middleware::VerboseResponse < Faraday::Middleware
|
2
|
+
|
3
|
+
def call(request_env)
|
4
|
+
@app.call(request_env).on_complete do |src_response_env|
|
5
|
+
response_env = src_response_env.dup
|
6
|
+
response_env.delete(:body)
|
7
|
+
|
8
|
+
$stdout.puts(YAML.dump({'response' => response_env}),"\n")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
class Faraday::CLI::Option::Parser
|
3
|
+
|
4
|
+
def parse!
|
5
|
+
options = {}
|
6
|
+
merge_defaults(options)
|
7
|
+
OptionParser.new do |o|
|
8
|
+
|
9
|
+
o.banner.concat(' <url>')
|
10
|
+
|
11
|
+
o.on('-V', '--version', 'Show version number and quit') { $stdout.puts(Faraday::CLI::VERSION); exit }
|
12
|
+
|
13
|
+
o.on('-X', '--request COMMAND', 'Specify http request command to use') do |http_method|
|
14
|
+
options[:http_method]= http_method.to_s.strip.downcase
|
15
|
+
end
|
16
|
+
|
17
|
+
o.on('-H', '--header HEADER:VALUE', 'Pass custom header LINE to server (H)') do |header|
|
18
|
+
options[:http_headers].push(header.split(':'))
|
19
|
+
end
|
20
|
+
|
21
|
+
o.on('-q', '--query key=value', 'Pass Query key values to use in the request') do |raw_query_pair|
|
22
|
+
options[:params].push(raw_query_pair.split('='))
|
23
|
+
end
|
24
|
+
|
25
|
+
o.on('-d', '--data PAYLOAD_STRING', 'HTTP POST data (H)') { |payload| options[:body]= payload }
|
26
|
+
|
27
|
+
o.on('--upload_file KEY=FILE_PATH[:CONTENT_TYPE]', 'Pass File upload io in the request pointing to the given file') do |payload_file_path|
|
28
|
+
|
29
|
+
options[:flags] << :multipart
|
30
|
+
|
31
|
+
parts = payload_file_path.split('=')
|
32
|
+
raw_file_path = parts.pop
|
33
|
+
key = parts.first
|
34
|
+
|
35
|
+
file_path, content_type = raw_file_path.split(':')
|
36
|
+
content_type ||= 'application/octet-stream'
|
37
|
+
|
38
|
+
file_stream_io = Faraday::UploadIO.new(File.realpath(file_path), content_type)
|
39
|
+
|
40
|
+
options[:body] = {key => file_stream_io}
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
o.on('-A', '--user-agent STRING', 'Send User-Agent STRING to server (H)') do |user_agent|
|
45
|
+
options[:http_headers] << ['User-Agent', user_agent]
|
46
|
+
end
|
47
|
+
|
48
|
+
o.on('-o', '--output FILE_PATH', 'Write to FILE instead of stdout') do |out_file_path|
|
49
|
+
$stdout.reopen(out_file_path, 'a+')
|
50
|
+
$stderr.reopen(out_file_path, 'a+')
|
51
|
+
end
|
52
|
+
|
53
|
+
# o.on('-x', '--proxy HOST:PORT', 'HOST[:PORT] Use proxy on given port') do |host_port_str|
|
54
|
+
# host, port = host_port_str.split(':')
|
55
|
+
# port = '80' if port.nil?
|
56
|
+
#
|
57
|
+
# options[:proxy]= {host: host, port: port}
|
58
|
+
# end
|
59
|
+
|
60
|
+
o.on('-v', '--verbose', 'Make the operation more talkative') do
|
61
|
+
options[:flags] << :verbose
|
62
|
+
end
|
63
|
+
|
64
|
+
o.on('--super-verbose', 'Make the operation even more talkative') do
|
65
|
+
options[:flags] << :super_verbose
|
66
|
+
end
|
67
|
+
|
68
|
+
o.on('-s', '--silent', "Silent mode (don't output anything)") do
|
69
|
+
options[:flags] << :silent
|
70
|
+
$stdout.reopen('/dev/null', 'a+')
|
71
|
+
$stderr.reopen('/dev/null', 'a+')
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
options[:config_file_paths]= []
|
76
|
+
o.on('-K', '--config FILE_PATH', 'File path to the .faraday.rb if you want use other than default') do |file_path|
|
77
|
+
options[:config_file_paths] << File.absolute_path(file_path)
|
78
|
+
end
|
79
|
+
|
80
|
+
o.on('-M', '--middlewares', 'Show current middleware stack') do
|
81
|
+
options[:flags] << :show_middlewares
|
82
|
+
end
|
83
|
+
|
84
|
+
o.on('-W', '--without_middlewares', 'Make request without consuming middleware file(s)') do
|
85
|
+
options[:flags] << :without_middlewares
|
86
|
+
end
|
87
|
+
|
88
|
+
# Z
|
89
|
+
|
90
|
+
o.parse!
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
options
|
95
|
+
end
|
96
|
+
|
97
|
+
protected
|
98
|
+
|
99
|
+
def merge_defaults(options_hash)
|
100
|
+
options_hash[:flags]= []
|
101
|
+
options_hash[:params]= []
|
102
|
+
options_hash[:http_method]= 'get'
|
103
|
+
options_hash[:http_headers]= []
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Faraday::CLI::Option::Validator
|
2
|
+
extend self
|
3
|
+
|
4
|
+
ALLOWED_HTTP_METHODS = %w(get head post put patch delete options)
|
5
|
+
|
6
|
+
def validate(options_hash)
|
7
|
+
validate_http_method(options_hash)
|
8
|
+
end
|
9
|
+
|
10
|
+
protected
|
11
|
+
|
12
|
+
def validate_http_method(options_hash)
|
13
|
+
unless ALLOWED_HTTP_METHODS.include?(options_hash[:http_method])
|
14
|
+
$stderr.puts("invalid http method given: #{options_hash[:http_method].inspect}")
|
15
|
+
exit(1)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
data/lib/faraday/cli.rb
CHANGED
@@ -2,9 +2,10 @@ require 'faraday'
|
|
2
2
|
module Faraday::CLI
|
3
3
|
|
4
4
|
require 'faraday/cli/version'
|
5
|
+
|
5
6
|
require 'faraday/cli/client'
|
7
|
+
require 'faraday/cli/option'
|
6
8
|
require 'faraday/cli/middleware'
|
7
|
-
require 'faraday/cli/response_formatter'
|
8
9
|
require 'faraday/cli/middleware_fetcher'
|
9
10
|
|
10
11
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday-cli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Luzsi
|
@@ -120,9 +120,12 @@ files:
|
|
120
120
|
- lib/faraday/cli/client.rb
|
121
121
|
- lib/faraday/cli/middleware.rb
|
122
122
|
- lib/faraday/cli/middleware/verbose_request.rb
|
123
|
+
- lib/faraday/cli/middleware/verbose_response.rb
|
123
124
|
- lib/faraday/cli/middleware_fetcher.rb
|
124
125
|
- lib/faraday/cli/middleware_fetcher/container.rb
|
125
|
-
- lib/faraday/cli/
|
126
|
+
- lib/faraday/cli/option.rb
|
127
|
+
- lib/faraday/cli/option/parser.rb
|
128
|
+
- lib/faraday/cli/option/validator.rb
|
126
129
|
- lib/faraday/cli/version.rb
|
127
130
|
- prototype/.faraday.rb
|
128
131
|
- prototype/config.ru
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'yaml'
|
2
|
-
module Faraday::CLI::ResponseFormatter
|
3
|
-
extend self
|
4
|
-
|
5
|
-
def format(faraday_response,*flags)
|
6
|
-
formatted_message_parts = []
|
7
|
-
formatted_message_parts << YAML.dump(response_hash_by(faraday_response)) if flags.include?(:verbose)
|
8
|
-
formatted_message_parts << faraday_response.body
|
9
|
-
|
10
|
-
formatted_message_parts.join("\n")
|
11
|
-
end
|
12
|
-
|
13
|
-
protected
|
14
|
-
|
15
|
-
def response_hash_by(faraday_response)
|
16
|
-
{
|
17
|
-
'response' => {
|
18
|
-
'status' => faraday_response.status,
|
19
|
-
'headers' => Hash[faraday_response.headers]
|
20
|
-
}
|
21
|
-
}
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|