havesnippet-client 1.0.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/Gemfile +2 -0
- data/LICENSE.txt +21 -0
- data/README.md +51 -0
- data/Rakefile +1 -0
- data/bin/havesnippet +5 -0
- data/bin/hs +5 -0
- data/havesnippet-client.gemspec +25 -0
- data/lib/havesnippet-client/cli.rb +158 -0
- data/lib/havesnippet-client/client.rb +51 -0
- data/lib/havesnippet-client/version.rb +5 -0
- data/lib/havesnippet_client.rb +5 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0c1ad3e038f71529b02ca361f5d43e8d7b4fb232
|
4
|
+
data.tar.gz: 53615ba0e0e8da3fb3f3b806781161289ad791bb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c49090ebcdc5e2396426e66eb5ec8757da7f35cad89da7c6de084b79e21aabf1bcdff42d252b6696b518574dd13d8dfb30b4cbf265a5645bec0987c93637912a
|
7
|
+
data.tar.gz: e028d2fe10287bf74038c4152dd3f8d4b0cb671f76ea3c4a167a24e8fb9c8a2cf0397c017022d808b73a068a8f2d4b1bf986258b66c877f693b497d8c07c073a
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Jakub Skokan
|
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,51 @@
|
|
1
|
+
HaveSnippet Client
|
2
|
+
==================
|
3
|
+
|
4
|
+
CLI for [HaveSnippet](https://github.com/aither64/havesnippet), a self-hosted
|
5
|
+
paste service.
|
6
|
+
|
7
|
+
Installation
|
8
|
+
------------
|
9
|
+
|
10
|
+
$ gem install havesnippet-client
|
11
|
+
|
12
|
+
|
13
|
+
Usage
|
14
|
+
-----
|
15
|
+
There are two executables, `havesnippet` and its abbreviation `hs`.
|
16
|
+
|
17
|
+
$ havesnippet --help
|
18
|
+
Usage: havesnippet [options] [file]
|
19
|
+
|
20
|
+
Options:
|
21
|
+
-s, --server URL URL to the HaveSnippet server
|
22
|
+
-k, --api-key KEY API key used to authenticate the user
|
23
|
+
-f, --filename NAME File name, including the extension
|
24
|
+
-l, --language LANG Language used to highlight the syntax
|
25
|
+
-L, --list-languages List available languages
|
26
|
+
-t, --title TITLE Title for the snippet
|
27
|
+
-a, --access MODE Accessibility (public,unlisted,logged,private)
|
28
|
+
-e, --expiration DATE Date of expiration in ISO 8601
|
29
|
+
--hour Expiration in one hour
|
30
|
+
--day Expiration in one day
|
31
|
+
--week Expiration in one week
|
32
|
+
--month Expiration in one month
|
33
|
+
--year Expiration in one year
|
34
|
+
--clear Clear any previously configured options
|
35
|
+
--save Save settings to config file
|
36
|
+
-h, --help Show this message and exit
|
37
|
+
|
38
|
+
`havesnippet` either reads from stdin or from `file`, if it is provided as an
|
39
|
+
argument. It then uploads it using HaveSnippet's API and prints the URL of the
|
40
|
+
created snippet.
|
41
|
+
|
42
|
+
Server address must be provided by the `-s` switch every time, unless a config
|
43
|
+
file is created using the `--save` switch, e.g.:
|
44
|
+
|
45
|
+
$ havesnippet -s https://paste.vpsfree.cz --save
|
46
|
+
$ echo yo! | havesnippet
|
47
|
+
https://paste.vpsfree.cz/12345678
|
48
|
+
|
49
|
+
Switch `--save` saves all command line options to the config file, so you might
|
50
|
+
set a default language, title, access mode or expiration.
|
51
|
+
Options loaded from the config file can be overriden by command line options.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/bin/havesnippet
ADDED
data/bin/hs
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'havesnippet-client/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'havesnippet-client'
|
8
|
+
spec.version = HaveSnippet::Client::VERSION
|
9
|
+
spec.authors = ['Jakub Skokan']
|
10
|
+
spec.email = ['aither@havefun.cz']
|
11
|
+
spec.summary =
|
12
|
+
spec.description = 'Client for HaveSnippet, a self-hosted paste service'
|
13
|
+
spec.homepage = ''
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
|
24
|
+
spec.add_runtime_dependency 'json'
|
25
|
+
end
|
@@ -0,0 +1,158 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
require 'time'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
module HaveSnippet::Client
|
6
|
+
class Cli
|
7
|
+
ACCESS_MODES = %w(public unlisted logged private)
|
8
|
+
|
9
|
+
def self.run
|
10
|
+
new.run
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@opts = {}
|
15
|
+
load
|
16
|
+
end
|
17
|
+
|
18
|
+
def run
|
19
|
+
usage = <<END
|
20
|
+
Usage: #{$0} [options] [file]
|
21
|
+
|
22
|
+
Options:
|
23
|
+
END
|
24
|
+
|
25
|
+
opt_parser = OptionParser.new do |opts|
|
26
|
+
opts.banner = usage
|
27
|
+
|
28
|
+
opts.on('-s', '--server URL', 'URL to the HaveSnippet server') do |v|
|
29
|
+
@opts[:url] = v
|
30
|
+
end
|
31
|
+
|
32
|
+
opts.on('-k', '--api-key KEY', 'API key used to authenticate the user') do |v|
|
33
|
+
@opts[:api_key] = v
|
34
|
+
end
|
35
|
+
|
36
|
+
opts.on('-f', '--filename NAME', 'File name, including the extension') do |v|
|
37
|
+
@opts[:file_name] = v
|
38
|
+
end
|
39
|
+
|
40
|
+
opts.on('-l', '--language LANG', 'Language used to highlight the syntax') do |v|
|
41
|
+
@opts[:language] = v
|
42
|
+
end
|
43
|
+
|
44
|
+
opts.on('-L', '--list-languages', 'List available languages') do
|
45
|
+
@opts[:languages] = true
|
46
|
+
end
|
47
|
+
|
48
|
+
opts.on('-t', '--title TITLE', 'Title for the snippet') do |v|
|
49
|
+
@opts[:title] = v
|
50
|
+
end
|
51
|
+
|
52
|
+
opts.on('-a', '--access MODE', ACCESS_MODES, "Accessibility (#{ACCESS_MODES.join(',')})") do |v|
|
53
|
+
@opts[:accessibility] = translate_access(v)
|
54
|
+
end
|
55
|
+
|
56
|
+
opts.on('-e', '--expiration DATE', 'Date of expiration in ISO 8601') do |v|
|
57
|
+
@expiration_delta = nil
|
58
|
+
@opts[:expiration] = Time.iso8601(v).to_i
|
59
|
+
end
|
60
|
+
|
61
|
+
{
|
62
|
+
hour: 60*60,
|
63
|
+
day: 60*60*24,
|
64
|
+
week: 60*60*24*7,
|
65
|
+
month: 60*60*24*7*30,
|
66
|
+
year: 60*60*24*7*30*12
|
67
|
+
}.each do |k, v|
|
68
|
+
opts.on("--#{k}", "Expiration in one #{k}") do
|
69
|
+
@opts[:expiration] = Time.now.to_i + v
|
70
|
+
@expiration_delta = v
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
opts.on('--clear', 'Clear any previously configured options') do
|
75
|
+
@opts.clear
|
76
|
+
end
|
77
|
+
|
78
|
+
opts.on('--save', 'Save settings to config file') do
|
79
|
+
@opts[:save] = true
|
80
|
+
end
|
81
|
+
|
82
|
+
opts.on('-h', '--help', 'Show this message and exit') do
|
83
|
+
puts opts
|
84
|
+
exit
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
opt_parser.parse!
|
89
|
+
|
90
|
+
@opts[:file_name] = File.basename(ARGV[0]) if ARGV[0] && !@opts[:file_name]
|
91
|
+
|
92
|
+
c = HaveSnippet::Client::Client.new(@opts[:url], @opts[:api_key])
|
93
|
+
|
94
|
+
if @opts[:languages]
|
95
|
+
c.languages.sort do |a, b|
|
96
|
+
a[0] <=> b[0]
|
97
|
+
end.each { |k, v| puts "#{k.ljust(16)} #{v}" }
|
98
|
+
exit
|
99
|
+
end
|
100
|
+
|
101
|
+
if @opts[:save]
|
102
|
+
save
|
103
|
+
exit
|
104
|
+
end
|
105
|
+
|
106
|
+
@opts[:content] = (ARGV[0] ? File.open(ARGV[0]) : STDIN).read
|
107
|
+
|
108
|
+
res = c.paste(@opts)
|
109
|
+
|
110
|
+
if res.ok?
|
111
|
+
puts res.url
|
112
|
+
|
113
|
+
else
|
114
|
+
if res.errors
|
115
|
+
warn "Error occured:"
|
116
|
+
res.errors.each do |k, v|
|
117
|
+
warn "\t#{k}: #{v.join('; ')}"
|
118
|
+
end
|
119
|
+
|
120
|
+
else
|
121
|
+
warn "Unknown error occured"
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def translate_access(v)
|
127
|
+
i = ACCESS_MODES.index(v)
|
128
|
+
raise ArgumentError, "'#{v}' is not a valid access mode" unless i
|
129
|
+
i
|
130
|
+
end
|
131
|
+
|
132
|
+
def load
|
133
|
+
return unless File.exists?(config_path)
|
134
|
+
|
135
|
+
@opts = YAML.load(File.read(config_path))
|
136
|
+
|
137
|
+
if @opts[:expiration_interval]
|
138
|
+
@opts[:expiration] = Time.now.to_i + @opts[:expiration_interval]
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def save
|
143
|
+
data = @opts.clone
|
144
|
+
data.delete(:save)
|
145
|
+
|
146
|
+
if @expiration_delta
|
147
|
+
data[:expiration_interval] = @expiration_delta
|
148
|
+
data.delete(:expiration)
|
149
|
+
end
|
150
|
+
|
151
|
+
File.open(config_path, 'w') { |f| f.write(YAML.dump(data)) }
|
152
|
+
end
|
153
|
+
|
154
|
+
def config_path
|
155
|
+
File.join(Dir.home, '.havesnippet')
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module HaveSnippet::Client
|
5
|
+
class Client
|
6
|
+
def initialize(server, api_key = nil)
|
7
|
+
@server = server
|
8
|
+
@api_key = api_key
|
9
|
+
end
|
10
|
+
|
11
|
+
def languages
|
12
|
+
uri = URI(File.join(@server, '/api/languages/?format=json'))
|
13
|
+
res = Net::HTTP.get_response(uri)
|
14
|
+
fail "got #{res.code}" unless res.code == '200'
|
15
|
+
JSON.parse(res.body)
|
16
|
+
end
|
17
|
+
|
18
|
+
def paste(opts)
|
19
|
+
data = {}
|
20
|
+
data.update(opts)
|
21
|
+
|
22
|
+
data[:format] = :json
|
23
|
+
data[:api_key] = @api_key if @api_key
|
24
|
+
|
25
|
+
uri = URI(File.join(@server, '/api/paste/'))
|
26
|
+
|
27
|
+
Response.new(Net::HTTP.post_form(uri, data))
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class Response
|
32
|
+
attr_reader :response, :data
|
33
|
+
|
34
|
+
def initialize(res)
|
35
|
+
@response = res
|
36
|
+
@data = JSON.parse(res.body, symbolize_names: true) if res.code == '200'
|
37
|
+
end
|
38
|
+
|
39
|
+
def ok?
|
40
|
+
@data && @data.has_key?(:url)
|
41
|
+
end
|
42
|
+
|
43
|
+
def errors
|
44
|
+
@data && @data[:error]
|
45
|
+
end
|
46
|
+
|
47
|
+
def url
|
48
|
+
@data && @data[:url]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: havesnippet-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jakub Skokan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-15 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.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: json
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Client for HaveSnippet, a self-hosted paste service
|
56
|
+
email:
|
57
|
+
- aither@havefun.cz
|
58
|
+
executables:
|
59
|
+
- havesnippet
|
60
|
+
- hs
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/havesnippet
|
69
|
+
- bin/hs
|
70
|
+
- havesnippet-client.gemspec
|
71
|
+
- lib/havesnippet-client/cli.rb
|
72
|
+
- lib/havesnippet-client/client.rb
|
73
|
+
- lib/havesnippet-client/version.rb
|
74
|
+
- lib/havesnippet_client.rb
|
75
|
+
homepage: ''
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.2.5
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Client for HaveSnippet, a self-hosted paste service
|
99
|
+
test_files: []
|
100
|
+
has_rdoc:
|