papidev 0.1.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 +7 -0
- data/bin/papi +4 -0
- data/lib/papi/client.rb +128 -0
- data/lib/papi/version.rb +5 -0
- data/lib/papi.rb +2 -0
- metadata +44 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: d8b1dfdba70d5a50b0ba724e4cd67b179906cb1ee9ad5b77b409c78557379953
|
|
4
|
+
data.tar.gz: 183451b2bc03bfef7083981ecf61e7c3d0a757c391085d61d90e6dfc23c66977
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 221841c46571b55f681ea057e47da8eab75b898631e45b8ff8479a410c5c7f885025f65e18c5d4f7f6ffcd4b505dd00ef2508732da247d9d73287fd4caf3603c
|
|
7
|
+
data.tar.gz: 357950c450eb862a5939e8771b092680cd7db8f8b88813d88684b2a7569d4bcb06194f8c8bf161c12082a9741120a6a9107d9824f1adba83394ff84548f70b0e
|
data/bin/papi
ADDED
data/lib/papi/client.rb
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
require "fileutils"
|
|
2
|
+
require "io/console"
|
|
3
|
+
require "net/http"
|
|
4
|
+
require "openssl"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module Papi
|
|
8
|
+
class Client
|
|
9
|
+
DEFAULT_HOST = "https://papidev.com".freeze
|
|
10
|
+
ORIGIN = %r{\Ahttps?://[a-z0-9.-]+(:[0-9]+)?\z}
|
|
11
|
+
|
|
12
|
+
Refusal = Class.new(StandardError)
|
|
13
|
+
|
|
14
|
+
def initialize(env: ENV, home: Dir.home, stdin: $stdin, stdout: $stdout, stderr: $stderr)
|
|
15
|
+
@env = env
|
|
16
|
+
@dir = File.join(home, ".config", "papi")
|
|
17
|
+
@stdin = stdin
|
|
18
|
+
@stdout = stdout
|
|
19
|
+
@stderr = stderr
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def run(args)
|
|
23
|
+
case args.first
|
|
24
|
+
when "login" then login(args[1] || current_host)
|
|
25
|
+
when "use" then args[1] ? use(args[1]) : list_hosts
|
|
26
|
+
else send_command(args)
|
|
27
|
+
end
|
|
28
|
+
rescue Refusal => refusal
|
|
29
|
+
@stderr.puts "papi: #{refusal.message}"
|
|
30
|
+
1
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def login(host)
|
|
36
|
+
check_origin(host)
|
|
37
|
+
token = read_token(host)
|
|
38
|
+
FileUtils.mkdir_p(File.join(@dir, "tokens"), mode: 0o700)
|
|
39
|
+
File.write(token_file(host), "#{token}\n", perm: 0o600)
|
|
40
|
+
File.write(host_file, "#{host}\n")
|
|
41
|
+
send_command([ "whoami" ])
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def read_token(host)
|
|
45
|
+
line = @stdin.tty? ? prompt_for_token(host) : @stdin.gets
|
|
46
|
+
raise Refusal, "no token given. Create one at #{host}/tokens." if line.nil? || line.strip.empty?
|
|
47
|
+
|
|
48
|
+
line.strip
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def prompt_for_token(host)
|
|
52
|
+
@stderr.print "Token (create one at #{host}/tokens): "
|
|
53
|
+
@stdin.noecho(&:gets).tap { @stderr.puts }
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def use(host)
|
|
57
|
+
check_origin(host)
|
|
58
|
+
raise Refusal, "not logged in to #{host}. Run papi login #{host}." unless File.exist?(token_file(host))
|
|
59
|
+
|
|
60
|
+
File.write(host_file, "#{host}\n")
|
|
61
|
+
0
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def list_hosts
|
|
65
|
+
current = current_host
|
|
66
|
+
Dir.glob(File.join(@dir, "tokens", "*")).each do |file|
|
|
67
|
+
host = File.basename(file).sub("_", "://")
|
|
68
|
+
@stdout.puts "#{host == current ? "*" : " "} #{host}"
|
|
69
|
+
end
|
|
70
|
+
0
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def send_command(args)
|
|
74
|
+
host, token = host_and_token
|
|
75
|
+
check_origin(host)
|
|
76
|
+
response = post(host, token, args)
|
|
77
|
+
status = response["Papi-Exit-Status"]
|
|
78
|
+
raise Refusal, "#{host} answered HTTP #{response.code}" if status.nil?
|
|
79
|
+
|
|
80
|
+
(status == "1" ? @stderr : @stdout).print response.body.force_encoding(Encoding::UTF_8)
|
|
81
|
+
Integer(status)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def host_and_token
|
|
85
|
+
if env("PAPI_TOKEN")
|
|
86
|
+
[ env("PAPI_HOST") || DEFAULT_HOST, env("PAPI_TOKEN") ]
|
|
87
|
+
else
|
|
88
|
+
[ current_host, stored_token(current_host) ]
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def env(name)
|
|
93
|
+
value = @env.fetch(name, "")
|
|
94
|
+
value unless value.empty?
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def post(host, token, args)
|
|
98
|
+
headers = { "Content-Type" => "application/x-www-form-urlencoded", "Papi-Client-Version" => VERSION }
|
|
99
|
+
headers["Authorization"] = "Bearer #{token}" if token
|
|
100
|
+
Net::HTTP.post(URI("#{host}/cli"), URI.encode_www_form(args.map { [ "args[]", it ] }), headers)
|
|
101
|
+
rescue SystemCallError, SocketError, IOError, Timeout::Error, OpenSSL::SSL::SSLError,
|
|
102
|
+
Net::HTTPBadResponse, Net::ProtocolError => error
|
|
103
|
+
raise Refusal, "could not reach #{host}: #{error.message}"
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def check_origin(host)
|
|
107
|
+
raise Refusal, "#{host} is not a host. Give one as https://name or https://name:port." unless host.match?(ORIGIN)
|
|
108
|
+
return if host.start_with?("https://")
|
|
109
|
+
|
|
110
|
+
name = URI(host).host
|
|
111
|
+
return if name == "localhost" || name.end_with?(".localhost")
|
|
112
|
+
|
|
113
|
+
raise Refusal, "refusing #{host}: a token would go to it unencrypted. Use #{host.sub("http://", "https://")}."
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def current_host
|
|
117
|
+
File.exist?(host_file) ? File.read(host_file).chomp : DEFAULT_HOST
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def stored_token(host)
|
|
121
|
+
File.read(token_file(host)).chomp if File.exist?(token_file(host))
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def token_file(host) = File.join(@dir, "tokens", host.sub("://", "_"))
|
|
125
|
+
|
|
126
|
+
def host_file = File.join(@dir, "host")
|
|
127
|
+
end
|
|
128
|
+
end
|
data/lib/papi/version.rb
ADDED
data/lib/papi.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: papidev
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Bartosz Krajka
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
executables:
|
|
13
|
+
- papi
|
|
14
|
+
extensions: []
|
|
15
|
+
extra_rdoc_files: []
|
|
16
|
+
files:
|
|
17
|
+
- bin/papi
|
|
18
|
+
- lib/papi.rb
|
|
19
|
+
- lib/papi/client.rb
|
|
20
|
+
- lib/papi/version.rb
|
|
21
|
+
homepage: https://papidev.com
|
|
22
|
+
licenses:
|
|
23
|
+
- MIT
|
|
24
|
+
metadata:
|
|
25
|
+
rubygems_mfa_required: 'true'
|
|
26
|
+
rdoc_options: []
|
|
27
|
+
require_paths:
|
|
28
|
+
- lib
|
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '3.4'
|
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '0'
|
|
39
|
+
requirements: []
|
|
40
|
+
rubygems_version: 4.0.20
|
|
41
|
+
specification_version: 4
|
|
42
|
+
summary: The command line for Papi, API specs with review, semantic diff and a mock
|
|
43
|
+
server.
|
|
44
|
+
test_files: []
|