papertrail 0.9.18 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 973d907611de02622b404c3fd7b1904e1158229c
4
- data.tar.gz: 8286cef976ddf379f33b5bea083cfdfcc448e98c
3
+ metadata.gz: ea790fef3b0c4239577138618f98b3aa12d4c366
4
+ data.tar.gz: bde482d33ce1b144314af54f01fe4f334924b59a
5
5
  SHA512:
6
- metadata.gz: 6b79a2b28652befee191d8415c879f3c92f7eecb3a92c1a4ba79ea37e871ce4baa21a57cb9cda3d9a6fe6e98b3bf38e5450c5c4e0bf676b1261c58c3fc3f5fac
7
- data.tar.gz: 40a8d133cb3c17bb54eecec80ab9086f7c4c9e1edaa1fed9d521f97a3c598d3a2e52b2cc13c93f11e36e351d7b8f8b24153c9799542034adf788b96518049469
6
+ metadata.gz: 17ed6f10ebd3ba1e8cfb4b4d1fee258314ac62293cbc9d8c31ba71e079724ce61188b22dd64ef5046ffe2756e3db385b4c05cca58f77636de60d47e3ffe5a7b2
7
+ data.tar.gz: d33c5d61a76d488afb1404aaa30740a082184c40ba6b75e6beeaa00db2ccadcff3302702c4f79986c2383f14eb946e6819364fc9fdaf0f8fdd123a940fb37237
data/Gemfile CHANGED
@@ -1,4 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gemspec
4
-
3
+ gemspec
@@ -0,0 +1,5 @@
1
+ guard :minitest do
2
+ watch(%r{^test/(.*)_test.rb$})
3
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[2]}_test.rb" }
4
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
5
+ end
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # papertrail command-line tail & search client for Papertrail log management service
2
2
 
3
+ [![Build Status](https://travis-ci.org/papertrail/papertrail-cli.svg?branch=master)](https://travis-ci.org/papertrail/papertrail-cli)
4
+
3
5
  Small standalone [binary] to retrieve, search, and tail recent app
4
6
  server log and system syslog messages from [Papertrail].
5
7
 
@@ -271,6 +273,12 @@ To see usage, run any of these commands with `--help`: `papertrail-add-system`,
271
273
 
272
274
  ## Contribute
273
275
 
276
+ Testing:
277
+
278
+ Run all the tests with `rake`
279
+
280
+ To run the tests when files save, run `bundle exec guard` (requires ruby version >= 2.2.5).
281
+
274
282
  Bug report:
275
283
 
276
284
  1. See whether the issue has already been reported:
data/Rakefile CHANGED
@@ -52,26 +52,10 @@ task :default => :test
52
52
  require 'rake/testtask'
53
53
  Rake::TestTask.new(:test) do |test|
54
54
  test.libs << 'lib' << 'test'
55
- test.pattern = 'test/**/test_*.rb'
55
+ test.pattern = 'test/*_test.rb'
56
56
  test.verbose = true
57
57
  end
58
58
 
59
- desc "Generate RCov test coverage and open in your browser"
60
- task :coverage do
61
- require 'rcov'
62
- sh "rm -fr coverage"
63
- sh "rcov test/test_*.rb"
64
- sh "open coverage/index.html"
65
- end
66
-
67
- require 'rdoc/task'
68
- Rake::RDocTask.new do |rdoc|
69
- rdoc.rdoc_dir = 'rdoc'
70
- rdoc.title = "#{name} #{version}"
71
- rdoc.rdoc_files.include('README*')
72
- rdoc.rdoc_files.include('lib/**/*.rb')
73
- end
74
-
75
59
  desc "Open an irb session preloaded with this library"
76
60
  task :console do
77
61
  sh "irb -rubygems -r ./lib/#{name}.rb"
@@ -1,5 +1,5 @@
1
1
  module Papertrail
2
- VERSION = '0.9.18'
2
+ VERSION = '0.10.0'
3
3
  end
4
4
 
5
5
  require 'papertrail/connection'
@@ -122,18 +122,18 @@ module Papertrail
122
122
  @query ||= ARGV.join ' '
123
123
 
124
124
  if options[:follow]
125
- search_query = connection.query(@query, query_options)
125
+ search_query = Papertrail::SearchQuery.new(connection, @query, query_options)
126
126
 
127
127
  loop do
128
- display_results(search_query.search)
128
+ display_results(search_query.next_results_page)
129
129
  sleep options[:delay]
130
130
  end
131
131
  elsif options[:min_time]
132
132
  query_time_range
133
133
  else
134
134
  set_min_max_time!(options, query_options)
135
- search_query = connection.query(@query, query_options)
136
- display_results(search_query.search)
135
+ search_query = Papertrail::SearchQuery.new(connection, @query, query_options)
136
+ display_results(search_query.next_results_page)
137
137
  end
138
138
  end
139
139
 
@@ -165,10 +165,6 @@ module Papertrail
165
165
  end
166
166
  end
167
167
 
168
- def query(query = nil, options = {})
169
- Papertrail::SearchQuery.new(self, query, options)
170
- end
171
-
172
168
  def each_event(query_term = nil, options = {}, &block)
173
169
  # If there was no query but there were options, shuffle around
174
170
  # the parameters
@@ -177,7 +173,7 @@ module Papertrail
177
173
  end
178
174
 
179
175
  # Remove all of the options that shouldn't be in each query
180
- options = options.dup
176
+ options = { :tail => false }.merge(options)
181
177
  min_id = options.delete(:min_id)
182
178
  max_id = options.delete(:max_id)
183
179
  min_time = options.delete(:min_time)
@@ -185,47 +181,39 @@ module Papertrail
185
181
 
186
182
  # Figure out where to start querying
187
183
  if min_id
188
- search_results = query(query_term, options.merge(:min_id => min_id, :tail => false)).search
184
+ search = search_query(query_term, options.merge(:min_id => min_id))
189
185
  elsif min_time
190
- search_results = query(query_term, options.merge(:min_time => min_time.to_i, :tail => false)).search
186
+ search = search_query(query_term, options.merge(:min_time => min_time.to_i))
191
187
  else
192
188
  raise ArgumentError, "Either :min_id or :min_time must be specified"
193
189
  end
194
190
 
195
191
  # Start processing events
196
192
  loop do
193
+ search_results = search.next_results_page
197
194
  search_results.events.each do |event|
198
195
  # If we've found an event beyond what we were looking for, we're done
199
- if max_time && event.received_at > max_time
200
- break
201
- end
202
-
203
- if max_id && event.id > max_id
204
- break
205
- end
196
+ break if max_time && event.received_at > max_time
197
+ break if max_id && event.id > max_id
206
198
 
207
199
  block.call(event)
208
200
  end
209
201
 
210
202
  # If we've found the end of what we're looking for, we're done
211
- if max_time && search_results.max_time_at > max_time
212
- break
213
- end
214
-
215
- if max_id && search_results.max_id > max_id
216
- break
217
- end
203
+ break if max_time && search_results.max_time_at > max_time
204
+ break if max_id && search_results.max_id > max_id
218
205
 
219
206
  # If we've reached the most current log message, we're done
220
- if search_results.reached_end?
221
- break
222
- end
223
-
224
- # Perform the next search
225
- search_results = query(query_term, options.merge(:min_id => search_results.max_id, :tail => false)).search
207
+ break if search_results.reached_end?
226
208
  end
227
209
 
228
210
  nil
229
211
  end
212
+
213
+ private
214
+
215
+ def search_query(query = nil, options = {})
216
+ Papertrail::SearchQuery.new(self, query, options)
217
+ end
230
218
  end
231
219
  end
@@ -47,7 +47,7 @@ module Papertrail
47
47
 
48
48
  def get(path, params = {})
49
49
  if params.size > 0
50
- path = "#{path}?#{build_nested_query(params)}"
50
+ path = "#{path}?cli_version=#{Papertrail::VERSION}&#{build_nested_query(params)}"
51
51
  end
52
52
  attempts = 0
53
53
  begin
@@ -1,21 +1,48 @@
1
1
  require 'papertrail/search_result'
2
2
 
3
+ # SearchQuery manages pagination.
4
+ # Once initialized, call `next_results_page` for a page of results
5
+ # And call it again to get the next page, and again and again
3
6
  module Papertrail
4
7
  class SearchQuery
8
+ def self.api_url
9
+ '/api/v1/events/search.json'
10
+ end
11
+
12
+ def self.initial_search_limit
13
+ 100
14
+ end
15
+
16
+ def self.subsequent_search_limit
17
+ 1000
18
+ end
19
+
5
20
  def initialize(connection, query = nil, options = {})
6
21
  @connection = connection
7
22
  @query = query
8
23
  @options = options
9
24
  end
10
25
 
11
- def search
26
+ attr_accessor :max_id, :subsequent_request
27
+
28
+ def next_results_page
12
29
  params = @options.dup
13
- params[:q] = @query if @query
30
+ params[:q] = @query if @query
14
31
  params[:min_id] = @max_id if @max_id
32
+ params[:limit] ||= default_request_limit
15
33
 
16
- response = @connection.get('/api/v1/events/search.json', params)
34
+ response = @connection.get(self.class.api_url, params)
17
35
  @max_id = response.body['max_id']
18
36
  Papertrail::SearchResult.new(response.body)
19
37
  end
38
+
39
+ def default_request_limit
40
+ if subsequent_request
41
+ self.class.subsequent_search_limit
42
+ else
43
+ @subsequent_request = true
44
+ self.class.initial_search_limit
45
+ end
46
+ end
20
47
  end
21
48
  end
@@ -1,9 +1,5 @@
1
- ## This is the rakegem gemspec template. Make sure you read and understand
2
- ## all of the comments. Some sections require modification, and others can
3
- ## be deleted if you don't need them. Once you understand the contents of
4
- ## this file, feel free to delete any comments that begin with two hash marks.
5
- ## You can find comprehensive Gem::Specification documentation, at
6
- ## http://docs.rubygems.org/read/chapter/20
1
+ ## This is the rakegem gemspec template
2
+ ## Look at the Rakefile for additional information on packaging, building and validation
7
3
  Gem::Specification.new do |s|
8
4
  s.specification_version = 2 if s.respond_to? :specification_version=
9
5
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
@@ -13,8 +9,8 @@ Gem::Specification.new do |s|
13
9
  ## If your rubyforge_project name is different, then edit it and comment out
14
10
  ## the sub! line in the Rakefile
15
11
  s.name = 'papertrail'
16
- s.version = '0.9.18'
17
- s.date = '2016-11-28'
12
+ s.version = '0.10.0'
13
+ s.date = '2016-12-02'
18
14
  s.rubyforge_project = 'papertrail'
19
15
 
20
16
  ## Make sure your summary is short. The description may be as long
@@ -56,6 +52,13 @@ Gem::Specification.new do |s|
56
52
  ## List your development dependencies here. Development dependencies are
57
53
  ## those that are only needed during development
58
54
  s.add_development_dependency('rake', '~> 10.4')
55
+ s.add_development_dependency('minitest')
56
+ s.add_development_dependency('mocha')
57
+
58
+ if RUBY_VERSION > '2.2.4' # ruby_dep dependency
59
+ s.add_development_dependency('guard', '~> 2.14.0')
60
+ s.add_development_dependency('guard-minitest', '~> 2.4.6')
61
+ end
59
62
 
60
63
  ## Leave this section as-is. It will be automatically generated from the
61
64
  ## contents of your Git repository via the gemspec task. DO NOT REMOVE
@@ -63,6 +66,7 @@ Gem::Specification.new do |s|
63
66
  # = MANIFEST =
64
67
  s.files = %w[
65
68
  Gemfile
69
+ Guardfile
66
70
  LICENSE
67
71
  README.md
68
72
  Rakefile
@@ -88,6 +92,9 @@ Gem::Specification.new do |s|
88
92
  lib/papertrail/search_query.rb
89
93
  lib/papertrail/search_result.rb
90
94
  papertrail.gemspec
95
+ test/connection_test.rb
96
+ test/search_query_test.rb
97
+ test/test_helper.rb
91
98
  ]
92
99
  # = MANIFEST =
93
100
 
@@ -0,0 +1,10 @@
1
+ require 'test_helper'
2
+
3
+ class ConnectionTest < Minitest::Test
4
+ let(:connection_options) { { :token => 'dummy' } }
5
+ let(:connection) { Papertrail::Connection.new(connection_options) }
6
+
7
+ def test_each_event
8
+ skip
9
+ end
10
+ end
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+
3
+ class SearchQueryTest < Minitest::Test
4
+ let(:connection_options) { { :token => 'dummy' } }
5
+ let(:connection) { Papertrail::Connection.new(connection_options) }
6
+ let(:api_url) { Papertrail::SearchQuery.api_url }
7
+ let(:default_initial_params) { { :limit => Papertrail::SearchQuery.initial_search_limit } }
8
+ let(:get_response) { OpenStruct.new(:body => { 'max_id' => 122, 'events' => [] }) }
9
+
10
+ describe 'default options' do
11
+ let(:search_query) { Papertrail::SearchQuery.new(connection) }
12
+ let(:target_params) do
13
+ {
14
+ :min_id => 122,
15
+ :limit => Papertrail::SearchQuery.subsequent_search_limit
16
+ }
17
+ end
18
+
19
+ def test_sets_min_id_for_initial_and_subsequent_requests
20
+ # Test that the first request happens with the default_initial_params
21
+ connection.expects(:get).with(api_url, default_initial_params).returns(get_response)
22
+ search_query.next_results_page
23
+ # And that subsequent requests use the updated min_id and limit
24
+ connection.expects(:get).with(api_url, target_params).returns(get_response)
25
+ search_query.next_results_page
26
+ end
27
+ end
28
+
29
+ describe 'passed options' do
30
+ let(:options) do
31
+ {
32
+ :group_id => 12,
33
+ :system_id => 22,
34
+ :min_id => 100,
35
+ :limit => 2
36
+ }
37
+ end
38
+ let(:search_query) { Papertrail::SearchQuery.new(connection, 'some search', options) }
39
+ let(:target_first_params) { options.merge(:q => 'some search') }
40
+ let(:target_subsequent_params) { target_first_params.merge(:min_id => 122) } # Max ID from get_response
41
+
42
+ def test_sets_options_from_passed_options_and_ups_min_id
43
+ connection.expects(:get).with(api_url, target_first_params).returns(get_response)
44
+ search_query.next_results_page
45
+ connection.expects(:get).with(api_url, target_subsequent_params).returns(get_response)
46
+ search_query.next_results_page
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,11 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride' # Color!
3
+ require 'mocha/mini_test'
4
+
5
+ require 'pp'
6
+
7
+ class Minitest::Test
8
+ extend Minitest::Spec::DSL # Add let blocks
9
+ end
10
+
11
+ require './lib/papertrail'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: papertrail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.18
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Papertrail
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-28 00:00:00.000000000 Z
11
+ date: 2016-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chronic
@@ -52,6 +52,62 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '10.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mocha
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 2.14.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 2.14.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-minitest
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 2.4.6
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 2.4.6
55
111
  description: Command-line client for Papertrail hosted log management service. Tails
56
112
  and searches app server logs and system syslog. Supports Boolean search and works
57
113
  with grep and pipe output (Unix).
@@ -67,6 +123,7 @@ extensions: []
67
123
  extra_rdoc_files: []
68
124
  files:
69
125
  - Gemfile
126
+ - Guardfile
70
127
  - LICENSE
71
128
  - README.md
72
129
  - Rakefile
@@ -92,6 +149,9 @@ files:
92
149
  - lib/papertrail/search_query.rb
93
150
  - lib/papertrail/search_result.rb
94
151
  - papertrail.gemspec
152
+ - test/connection_test.rb
153
+ - test/search_query_test.rb
154
+ - test/test_helper.rb
95
155
  homepage: http://github.com/papertrail/papertrail-cli
96
156
  licenses:
97
157
  - MIT
@@ -117,4 +177,5 @@ rubygems_version: 2.4.8
117
177
  signing_key:
118
178
  specification_version: 2
119
179
  summary: Command-line client for Papertrail hosted log management service.
120
- test_files: []
180
+ test_files:
181
+ - test/test_helper.rb