plz 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/README.md +8 -0
- data/lib/plz.rb +1 -0
- data/lib/plz/arguments.rb +53 -12
- data/lib/plz/command_builder.rb +3 -0
- data/lib/plz/commands/help.rb +4 -3
- data/lib/plz/commands/invalid_json_from_stdin.rb +9 -0
- data/lib/plz/version.rb +1 -1
- data/schema.yml +2 -2
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 438be4824e988fa0a6ee8c9d115d1a3f0a484a96
|
4
|
+
data.tar.gz: 3355e4e54c324e5bab8470a27b03f147c32ad17e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 97b85d081396b16d302f8bb6471526197ebdab8b4df168cdc7e4b6260443eb302b2183491548047cbbc8d70ca3741527715e4097edc0cb49d2aec72f710ed79b
|
7
|
+
data.tar.gz: 563281675be4f77725203203cb59e4a4306e51189b756f9d6bea9a2e06b8a4ee8ece3d3fae92425d1784491c1a65e1fe06317da54dfc597f14312e938ee5ba50
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -54,6 +54,14 @@ while `key:=value` is parsed into JSON value (e.g. key:=17 will be `{"key":17}`)
|
|
54
54
|
$ plz create user name=alice age:=17
|
55
55
|
```
|
56
56
|
|
57
|
+
### Stdin
|
58
|
+
You can pass params via STDIN, instead of command line arguments.
|
59
|
+
|
60
|
+
```sh
|
61
|
+
$ plz create user < params.json
|
62
|
+
$ cat params.json | plz create user
|
63
|
+
```
|
64
|
+
|
57
65
|
### Options
|
58
66
|
Plz takes some command line options.
|
59
67
|
|
data/lib/plz.rb
CHANGED
@@ -20,6 +20,7 @@ require "plz/arguments"
|
|
20
20
|
require "plz/command_builder"
|
21
21
|
require "plz/commands/base_url_not_found"
|
22
22
|
require "plz/commands/help"
|
23
|
+
require "plz/commands/invalid_json_from_stdin"
|
23
24
|
require "plz/commands/invalid_schema"
|
24
25
|
require "plz/commands/link_not_found"
|
25
26
|
require "plz/commands/no_action_name"
|
data/lib/plz/arguments.rb
CHANGED
@@ -15,10 +15,48 @@ module Plz
|
|
15
15
|
ARGV[1]
|
16
16
|
end
|
17
17
|
|
18
|
-
# @return [Hash] Params parsed from given arguments
|
18
|
+
# @return [Hash] Params parsed from given arguments & STDIN
|
19
19
|
# @raise [Plz::UnparsableJsonParam]
|
20
20
|
def params
|
21
|
-
|
21
|
+
params_from_stdin.merge(params_from_argv)
|
22
|
+
end
|
23
|
+
|
24
|
+
# @return [Hash] Headers parsed from given arguments
|
25
|
+
def headers
|
26
|
+
ARGV[2..-1].inject({}) do |result, section|
|
27
|
+
case
|
28
|
+
when /(?<key>.+):(?<value>[^=]+)/ =~ section
|
29
|
+
result.merge(key => value)
|
30
|
+
else
|
31
|
+
result
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# @return [true, false] true if invalid JSON input is given via STDIN
|
37
|
+
def has_invalid_json_input?
|
38
|
+
params_from_stdin
|
39
|
+
false
|
40
|
+
rescue JSON::ParserError
|
41
|
+
true
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
# @return [Hash] Params extracted from STDIN
|
47
|
+
def params_from_stdin
|
48
|
+
@params_from_stdin ||= begin
|
49
|
+
if has_input_from_stdin?
|
50
|
+
JSON.parse(STDIN.read)
|
51
|
+
else
|
52
|
+
{}
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# @return [Hash] Params extracted from ARGV
|
58
|
+
def params_from_argv
|
59
|
+
@params_from_argv ||= ARGV[2..-1].inject({}) do |result, section|
|
22
60
|
case
|
23
61
|
when /(?<key>.+):=(?<value>.+)/ =~ section
|
24
62
|
begin
|
@@ -34,16 +72,19 @@ module Plz
|
|
34
72
|
end
|
35
73
|
end
|
36
74
|
|
37
|
-
# @return [
|
38
|
-
def
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
75
|
+
# @return [true, false] True if any input given via STDIN
|
76
|
+
def has_input_from_stdin?
|
77
|
+
has_pipe_input? || has_redirect_input?
|
78
|
+
end
|
79
|
+
|
80
|
+
# @return [true, false] True if any input given from redirection
|
81
|
+
def has_redirect_input?
|
82
|
+
File.select([STDIN], [], [], 0) != nil
|
83
|
+
end
|
84
|
+
|
85
|
+
# @return [true, false] True if any input given from pipe
|
86
|
+
def has_pipe_input?
|
87
|
+
File.pipe?(STDIN)
|
47
88
|
end
|
48
89
|
end
|
49
90
|
end
|
data/lib/plz/command_builder.rb
CHANGED
@@ -16,6 +16,7 @@ module Plz
|
|
16
16
|
:target_name,
|
17
17
|
:headers,
|
18
18
|
:params,
|
19
|
+
:has_invalid_json_input?,
|
19
20
|
to: :arguments,
|
20
21
|
)
|
21
22
|
|
@@ -47,6 +48,8 @@ module Plz
|
|
47
48
|
action_name: action_name,
|
48
49
|
target_name: target_name
|
49
50
|
)
|
51
|
+
when has_invalid_json_input?
|
52
|
+
Commands::InvalidJsonFromStdin.new
|
50
53
|
when has_unparsable_json_param?
|
51
54
|
Commands::UnparsableJsonParam.new(error: @json_parse_error)
|
52
55
|
else
|
data/lib/plz/commands/help.rb
CHANGED
@@ -23,9 +23,10 @@ module Plz
|
|
23
23
|
end.map do |link|
|
24
24
|
str = " plz #{link.title.underscore} #{target_name}"
|
25
25
|
if key = link.href[/{(.+)}/, 1]
|
26
|
-
|
27
|
-
|
28
|
-
|
26
|
+
path = CGI.unescape(key).gsub(/[()]/, "")
|
27
|
+
name = path.split("/").last
|
28
|
+
if property = JsonPointer.evaluate(@schema.data, path)
|
29
|
+
if example = property["example"]
|
29
30
|
str << " #{name}=#{example}"
|
30
31
|
end
|
31
32
|
end
|
data/lib/plz/version.rb
CHANGED
data/schema.yml
CHANGED
@@ -49,12 +49,12 @@ definitions:
|
|
49
49
|
rel: create
|
50
50
|
title: Create
|
51
51
|
- description: Update the user
|
52
|
-
href: "/users/{(#/definitions/
|
52
|
+
href: "/users/{(#/definitions/user/definitions/id)}"
|
53
53
|
method: PATCH
|
54
54
|
rel: update
|
55
55
|
title: Update
|
56
56
|
- description: Delete the user
|
57
|
-
href: "/users/{(#/definitions/
|
57
|
+
href: "/users/{(#/definitions/user/definitions/id)}"
|
58
58
|
method: DELETE
|
59
59
|
rel: delete
|
60
60
|
title: Delete
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: plz
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryo Nakamura
|
@@ -199,6 +199,7 @@ files:
|
|
199
199
|
- lib/plz/command_builder.rb
|
200
200
|
- lib/plz/commands/base_url_not_found.rb
|
201
201
|
- lib/plz/commands/help.rb
|
202
|
+
- lib/plz/commands/invalid_json_from_stdin.rb
|
202
203
|
- lib/plz/commands/invalid_schema.rb
|
203
204
|
- lib/plz/commands/link_not_found.rb
|
204
205
|
- lib/plz/commands/no_action_name.rb
|