console_detective 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ef9da06b2847ed04dd93462272f647bf51ce89af78693a975234f8cb3ae011d8
4
+ data.tar.gz: ae806be239601ccc221519bb1b787d21341975736f739a0061296c437e130f57
5
+ SHA512:
6
+ metadata.gz: 7d63f6471350aa8b5d46bf53186eda1e98a5167c2f4772ba10f816a2ee1a90b182fd6632cea1bb77fb443f575bb6f43fbfdedacec76e58f6732a14f82777ebb9
7
+ data.tar.gz: 2c317ba8cc2603119f26ba809036248d7bbfa5959f02b82fcc3c5af4263c66b06acce7045b364fe84c6e8f5efeff9777443bcd0017483bf08fd2dda2ebc9cf9f
@@ -0,0 +1,30 @@
1
+ name: Console Detective
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ test:
7
+
8
+ runs-on: ubuntu-latest
9
+
10
+ strategy:
11
+ matrix:
12
+ ruby-version: ['2.6', '2.7', '3.0', head, truffleruby, truffleruby-head]
13
+
14
+ steps:
15
+ - uses: actions/checkout@v2
16
+ - name: Set up Ruby
17
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
18
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
19
+ uses: ruby/setup-ruby@v1
20
+ # uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
21
+ with:
22
+ ruby-version: ${{ matrix.ruby-version }}
23
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
24
+ - run: mkdir -p log/ tmp/ coverage/
25
+ - name: Run tests
26
+ run: bundle exec rake
27
+ - name: Upload Code Coverage
28
+ env: # Or as an environment variable
29
+ CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }}
30
+ run: bash <(curl -Ls https://coverage.codacy.com/get.sh) report -r coverage/lcov.info
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /log/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ .byebug_history
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in console_detective.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+
9
+ gem 'byebug'
10
+ gem 'pry'
11
+
12
+ gem 'simplecov', require: false
13
+ gem 'simplecov-lcov', require: false
data/Gemfile.lock ADDED
@@ -0,0 +1,52 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ console_detective (0.1.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ byebug (11.1.3)
10
+ coderay (1.1.3)
11
+ diff-lcs (1.4.4)
12
+ docile (1.4.0)
13
+ method_source (1.0.0)
14
+ pry (0.14.1)
15
+ coderay (~> 1.1)
16
+ method_source (~> 1.0)
17
+ rake (12.3.3)
18
+ rspec (3.10.0)
19
+ rspec-core (~> 3.10.0)
20
+ rspec-expectations (~> 3.10.0)
21
+ rspec-mocks (~> 3.10.0)
22
+ rspec-core (3.10.1)
23
+ rspec-support (~> 3.10.0)
24
+ rspec-expectations (3.10.1)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.10.0)
27
+ rspec-mocks (3.10.2)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.10.0)
30
+ rspec-support (3.10.2)
31
+ simplecov (0.21.2)
32
+ docile (~> 1.1)
33
+ simplecov-html (~> 0.11)
34
+ simplecov_json_formatter (~> 0.1)
35
+ simplecov-html (0.12.3)
36
+ simplecov-lcov (0.8.0)
37
+ simplecov_json_formatter (0.1.3)
38
+
39
+ PLATFORMS
40
+ ruby
41
+
42
+ DEPENDENCIES
43
+ byebug
44
+ console_detective!
45
+ pry
46
+ rake (~> 12.0)
47
+ rspec (~> 3.0)
48
+ simplecov
49
+ simplecov-lcov
50
+
51
+ BUNDLED WITH
52
+ 2.1.4
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 ArunkumarN
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # ConsoleDetective ![Console Detective](https://github.com/arunn/console_detective/actions/workflows/ci.yml/badge.svg)[![Codacy Badge](https://app.codacy.com/project/badge/Grade/7fabacab5ff445248655e1b9b35f1aef)](https://www.codacy.com?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=arunn/console_detective&amp;utm_campaign=Badge_Grade)[![Codacy Badge](https://app.codacy.com/project/badge/Coverage/7fabacab5ff445248655e1b9b35f1aef)](https://www.codacy.com?utm_source=github.com&utm_medium=referral&utm_content=arunn/console_detective&utm_campaign=Badge_Coverage)[![MIT license](https://img.shields.io/badge/License-MIT-blue.svg)](https://lbesson.mit-license.org/)[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/arunn/console_detective/graphs/commit-activity)
2
+
3
+ A gem to track commands typed in rails console along with tagging in realtime. The tags can be used to identify users. This works with plain [pry](https://github.com/pry/pry) and [IRB](https://github.com/ruby/ruby/tree/master/lib/irb) and rails console using pry or IRB. The values for log tags, log format, log file name, and memoization requirements are configurable.
4
+
5
+ Pry and IRB both provide options for recordng history. It has a few disadvantages:
6
+
7
+ 1. It is not possible to get the logs in realtime.
8
+ 2. There are no tagging options available.
9
+ 3. There is no way to know the time when the command was fired.
10
+ 4. If we're connecting to console using ssh, the logs will be lost if the connection is disconnected since the logs are only written only when we exit the session.
11
+
12
+ `console_detective` overcomes such disadvantages.
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ~~~~~ruby
18
+ gem 'console_detective'
19
+ ~~~~~
20
+
21
+ And then execute:
22
+
23
+ ~~~~~sh
24
+ $ bundle install
25
+ ~~~~~
26
+
27
+ Or install it yourself as:
28
+ ~~~~~sh
29
+ $ gem install console_detective
30
+ ~~~~~
31
+ ## Usage
32
+
33
+ There are meaningful defaults for the config. If you are using rails, run `rails console`. Otherwise, `irb -rconsole_detective` or `pry -I./lib -r console_detective` will load the respective consoles with `console_detective` loaded.
34
+
35
+ The configs can be overridden by creating a file named `console_detective.rb` with following code.
36
+
37
+ ~~~ruby
38
+ require 'console_detective'
39
+
40
+ ConsoleDetective.setup do |config|
41
+ # log_file_name is a string/file that mentions the location and name of the file where log will be written.
42
+ # default value is log/console.log
43
+ config.log_file_name = "log/console.log"
44
+
45
+ # log_tags is a lambda outputting the tag to tag the log entry
46
+ # default value is the ENV['USER']
47
+ config.log_tags = -> { ENV['USER'] }
48
+
49
+ # log_format is a lambda which takes tag and command as input and outputs the format in which the log will be entered in the log file
50
+ # default format is a hash of format { :tag => <tag>, :command => <command> }
51
+ config.log_format = -> (tag, command) { { tag: tag, command: command } }
52
+
53
+ # tag_memoization is a boolean to mention if the tag should be memoized or not.
54
+ # default is true
55
+ config.tag_memoization = true
56
+ end
57
+ ~~~
58
+
59
+ If you are using rails, place this file in `config/initializers` folder and run `rails console`. Otherwise, `irb -r ./console_detective.rb` or `pry -r ./console_detective.rb` will load the respective consoles with `console_detective` loaded with modified config.
60
+
61
+ ## Development
62
+
63
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. `bin/console` will run in IRB whereas `PRY=true bin/console` will run with Pry.
64
+
65
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
66
+
67
+ ## Contributing
68
+
69
+ Bug reports and pull requests are welcome on GitHub at https://github.com/arunn/console_detective.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require 'byebug'
5
+ # You can add fixtures and/or initialization code here to make experimenting
6
+ # with your gem easier. You can also use a different console, if you like.
7
+
8
+ # (If you use this, don't forget to add pry to your Gemfile!)
9
+
10
+ if(ENV['PRY'] == 'true')
11
+ require "pry"
12
+ require "console_detective" # Loading after loading irb/pry since we want those to be present when loading console_detective
13
+
14
+ Pry.start
15
+ else
16
+ require "irb"
17
+ require "console_detective" # Loading after loading irb/pry since we want those to be present when loading console_detective
18
+
19
+ IRB.start(__FILE__)
20
+ end
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/console_detective/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "console_detective"
5
+ spec.version = ConsoleDetective::VERSION
6
+ spec.authors = ["ArunkumarN"]
7
+ spec.email = ["arunn@arunn.dev"]
8
+ spec.licenses = ["MIT"]
9
+
10
+ spec.summary = %q{Track rails console user activity in real time}
11
+ spec.description = %q{Commands typed in rails console will be tracked realtime in a log file in the given format}
12
+ spec.homepage = "https://github.com/arunn/console_detective"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/arunn/console_detective"
19
+ spec.metadata["changelog_uri"] = "https://github.com/arunn/console_detective/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ end
@@ -0,0 +1,30 @@
1
+ require "console_detective/version"
2
+ require "console_detective/mod_attr_accessor"
3
+
4
+ module ConsoleDetective
5
+ extend ConsoleDetective::ModAttrAccessor
6
+
7
+ def self.setup
8
+ yield self
9
+ end
10
+
11
+ # log_file_name is a string/file that mentions the location and name of the file where log will be written.
12
+ # default value is log/console.log
13
+ mod_attr_accessor :log_file_name, "log/console.log"
14
+
15
+ # log_tags is a lambda outputting the tag to tag the log entry
16
+ # default value is the ENV['USER']
17
+ mod_attr_accessor :log_tags, -> { ENV['USER'] }
18
+
19
+ # log_format is a lambda which takes tag and command as input and outputs the format in which the log will be entered in the log file
20
+ # default format is a hash of format { :tag => <tag>, :command => <command> }
21
+ mod_attr_accessor :log_format, -> (tag, command) { { tag: tag, command: command } }
22
+
23
+ # tag_memoization is a boolean to mention if the tag should be memoized or not.
24
+ # default is true
25
+ mod_attr_accessor :tag_memoization, true
26
+ end
27
+
28
+ require 'console_detective/utils'
29
+ require 'console_detective/irb'
30
+ require 'console_detective/pry'
@@ -0,0 +1,10 @@
1
+ module ConsoleDetective
2
+ module IrbLogger
3
+ def evaluate(*args)
4
+ ConsoleDetective::Utils.log_command(args.first.chomp)
5
+ super(*args)
6
+ end
7
+ end
8
+ end
9
+
10
+ IRB::Context.prepend(ConsoleDetective::IrbLogger) if defined?(IRB::Context)
@@ -0,0 +1,22 @@
1
+ # Inspired and simplified from ActiveSupport's defintion of mattr_accessor
2
+ # https://github.com/rails/rails/blob/5db5de534106a44070374810a99853f38843b1d2/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
3
+
4
+ module ConsoleDetective
5
+ module ModAttrAccessor
6
+ def mod_attr_accessor(attr_name, default_value)
7
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
8
+ @@#{attr_name} = nil unless defined? @@#{attr_name}
9
+
10
+ def self.#{attr_name}
11
+ @@#{attr_name}
12
+ end
13
+
14
+ def self.#{attr_name}=(obj)
15
+ @@#{attr_name} = obj
16
+ end
17
+ EOS
18
+
19
+ send("#{attr_name}=", default_value) unless default_value.nil?
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,9 @@
1
+ if defined?(Pry)
2
+ Pry.hooks.add_hook(:before_eval, "log_before_eval") do |command, _pry|
3
+ ConsoleDetective::Utils.log_command(command.chomp)
4
+ end
5
+
6
+ Pry.hooks.add_hook(:after_session, "log_after_session") do |_output, _binding, _pry|
7
+ ConsoleDetective::Utils.log_command("exit", immediately: true)
8
+ end
9
+ end
@@ -0,0 +1,21 @@
1
+ require 'logger'
2
+
3
+ module ConsoleDetective
4
+ module Utils
5
+ LOGGER_PROC = ->(command) { ConsoleDetective::Utils.logger.info(ConsoleDetective.log_format.call(ConsoleDetective::Utils.get_tag, command)) }
6
+
7
+ def self.logger
8
+ @logger ||= Logger.new(ConsoleDetective.log_file_name)
9
+ end
10
+
11
+ def self.get_tag
12
+ return @tag if ConsoleDetective.tag_memoization && @tag
13
+ @tag = ConsoleDetective.log_tags.call
14
+ end
15
+
16
+ def self.log_command(command, immediately: false)
17
+ return Thread.new { ConsoleDetective::Utils::LOGGER_PROC.call(command) } unless immediately
18
+ ConsoleDetective::Utils::LOGGER_PROC.call(command)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module ConsoleDetective
2
+ VERSION = "0.1.1".freeze
3
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: console_detective
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - ArunkumarN
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-07-25 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Commands typed in rails console will be tracked realtime in a log file
14
+ in the given format
15
+ email:
16
+ - arunn@arunn.dev
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".github/workflows/ci.yml"
22
+ - ".gitignore"
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/setup
30
+ - console_detective.gemspec
31
+ - lib/console_detective.rb
32
+ - lib/console_detective/irb.rb
33
+ - lib/console_detective/mod_attr_accessor.rb
34
+ - lib/console_detective/pry.rb
35
+ - lib/console_detective/utils.rb
36
+ - lib/console_detective/version.rb
37
+ homepage: https://github.com/arunn/console_detective
38
+ licenses:
39
+ - MIT
40
+ metadata:
41
+ allowed_push_host: https://rubygems.org
42
+ homepage_uri: https://github.com/arunn/console_detective
43
+ source_code_uri: https://github.com/arunn/console_detective
44
+ changelog_uri: https://github.com/arunn/console_detective/CHANGELOG.md
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.3.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.1.6
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Track rails console user activity in real time
64
+ test_files: []