console_kit 0.1.0 → 0.1.1
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 +4 -4
- data/.rubocop.yml +3 -5
- data/Rakefile +3 -3
- data/lib/console_kit/output.rb +43 -0
- data/lib/console_kit/railtie.rb +1 -0
- data/lib/console_kit/setup.rb +22 -81
- data/lib/console_kit/tenant_configurator.rb +37 -0
- data/lib/console_kit/tenant_selector.rb +62 -0
- data/lib/console_kit/version.rb +1 -1
- data/lib/console_kit.rb +3 -3
- data/lib/generators/console_kit/install_generator.rb +29 -0
- data/lib/generators/console_kit/templates/console_kit.rb +25 -0
- metadata +12 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 506930ef3ce84d9549862ba7339ab0c7924ccfbe50d532fcec00cdff412233bc
|
4
|
+
data.tar.gz: 763494b7a53f49a7ec3084282ebeb1dedaafde763c2fb5c36ebb5f9ce75b51b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fefffd21661ebf8a3189cf2d149d5bc62354cb242f75412da6e74ea5525e28ae3b5dcd37eb04d918b643d12b5c2e36e173d830c8fe680619f076bace89110e7c
|
7
|
+
data.tar.gz: f874596db1b50deea79214d533ff2c38f13cad33de50753728527884b3eebb423733239e6a3dcef78a8c57a845616415055b408b6e19b69d41938f7d0df487f1
|
data/.rubocop.yml
CHANGED
data/Rakefile
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rspec/core/rake_task'
|
5
5
|
|
6
6
|
RSpec::Core::RakeTask.new(:spec)
|
7
7
|
|
8
|
-
require
|
8
|
+
require 'rubocop/rake_task'
|
9
9
|
|
10
10
|
RuboCop::RakeTask.new
|
11
11
|
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ConsoleKit
|
4
|
+
# Handles Console outputs
|
5
|
+
module Output
|
6
|
+
class << self
|
7
|
+
def print_error(text)
|
8
|
+
print_message("[✗] #{text}", '1;31') # Red
|
9
|
+
end
|
10
|
+
|
11
|
+
def print_success(text)
|
12
|
+
print_message("[✓] #{text}", '1;32') # Green
|
13
|
+
end
|
14
|
+
|
15
|
+
def print_backtrace(exception)
|
16
|
+
exception.backtrace.each { |line| print_message(" #{line}", '0;90') } # Dim gray
|
17
|
+
end
|
18
|
+
|
19
|
+
def print_header(text)
|
20
|
+
print_message("\n=== #{text} ===", '1;34') # Bold Blue
|
21
|
+
end
|
22
|
+
|
23
|
+
def print_info(text)
|
24
|
+
print_message(text)
|
25
|
+
end
|
26
|
+
|
27
|
+
def print_prompt(text)
|
28
|
+
print_message(text, '1;36') # Cyan
|
29
|
+
end
|
30
|
+
|
31
|
+
def print_warning(text)
|
32
|
+
print_message("[!] #{text}", '1;33') # Yellow
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def print_message(text, color = nil)
|
38
|
+
msg = "[ConsoleKit] #{text}"
|
39
|
+
puts color ? "\e[#{color}m#{msg}\e[0m" : msg
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/console_kit/railtie.rb
CHANGED
data/lib/console_kit/setup.rb
CHANGED
@@ -1,20 +1,23 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'tenant_selector'
|
4
|
+
require_relative 'tenant_configurator'
|
5
|
+
require_relative 'output'
|
6
|
+
|
7
|
+
# Core Logic for initial Setup
|
3
8
|
module ConsoleKit
|
4
9
|
class << self
|
5
10
|
attr_accessor :tenants, :context_class
|
6
11
|
|
7
12
|
def setup
|
8
|
-
return print_error(
|
13
|
+
return Output.print_error('No tenants configured.') if tenants.nil? || tenants.empty?
|
9
14
|
|
10
|
-
tenant_key =
|
11
|
-
return print_error(
|
15
|
+
tenant_key = resolve_tenant_key
|
16
|
+
return Output.print_error('No tenant selected. Loading without tenant configuration.') unless tenant_key
|
12
17
|
|
13
|
-
|
14
|
-
print_success("Tenant initialized: #{tenant_key}")
|
18
|
+
initialize_tenant(tenant_key)
|
15
19
|
rescue StandardError => e
|
16
|
-
|
17
|
-
print_backtrace(e)
|
20
|
+
handle_setup_error(e)
|
18
21
|
end
|
19
22
|
|
20
23
|
def configure
|
@@ -23,88 +26,26 @@ module ConsoleKit
|
|
23
26
|
|
24
27
|
private
|
25
28
|
|
26
|
-
def
|
27
|
-
|
28
|
-
print_info(" 0. Load without tenant (no tenant configuration)")
|
29
|
-
keys.each_with_index do |key, index|
|
30
|
-
partner = tenants.dig(key, :constants, :partner_code) || 'N/A'
|
31
|
-
print_info(" #{index + 1}. #{key} (partner: #{partner})")
|
32
|
-
end
|
33
|
-
|
34
|
-
max_attempts = 3
|
35
|
-
selected_key = nil
|
36
|
-
|
37
|
-
max_attempts.times do
|
38
|
-
print_prompt("\nEnter the number of the tenant you want (or press Enter for default '1'): ")
|
39
|
-
input = $stdin.gets&.chomp&.strip
|
40
|
-
|
41
|
-
input = '1' if input.to_s.empty?
|
42
|
-
|
43
|
-
unless (index = input.to_i) && index.between?(0, keys.size)
|
44
|
-
print_warning("Invalid selection. Please try again.")
|
45
|
-
next
|
46
|
-
end
|
47
|
-
|
48
|
-
return nil if index.zero?
|
49
|
-
|
50
|
-
selected_key = keys[index - 1]
|
51
|
-
break
|
52
|
-
end
|
53
|
-
|
54
|
-
selected_key
|
55
|
-
end
|
56
|
-
|
57
|
-
def configure_tenant(key)
|
58
|
-
config = tenants[key]
|
59
|
-
constants = config[:constants]
|
60
|
-
|
61
|
-
tenant_shard = constants[:shard]
|
62
|
-
tenant_mongo_db = constants[:mongo_db]
|
63
|
-
|
64
|
-
context_class.tenant_shard = tenant_shard
|
65
|
-
context_class.tenant_mongo_db = tenant_mongo_db
|
66
|
-
context_class.partner_identifier = constants[:partner_code]
|
67
|
-
|
68
|
-
ApplicationRecord.establish_connection(tenant_shard.to_sym)
|
69
|
-
Mongoid.override_client(tenant_mongo_db.to_s)
|
70
|
-
|
71
|
-
print_success("Tenant set to: #{key}")
|
72
|
-
rescue StandardError => e
|
73
|
-
print_error("Failed to configure tenant '#{key}': #{e.message}")
|
74
|
-
print_backtrace(e)
|
75
|
-
end
|
76
|
-
|
77
|
-
def print_error(text)
|
78
|
-
print_message("[✗] #{text}", '1;31') # Red
|
79
|
-
end
|
80
|
-
|
81
|
-
def print_success(text)
|
82
|
-
print_message("[✓] #{text}", '1;32') # Green
|
83
|
-
end
|
84
|
-
|
85
|
-
def print_backtrace(exception)
|
86
|
-
exception.backtrace.each { |line| print_message(" #{line}", '0;90') } # Dim gray
|
87
|
-
end
|
88
|
-
|
89
|
-
def print_header(text)
|
90
|
-
print_message("\n=== #{text} ===", '1;34') # Bold Blue
|
29
|
+
def resolve_tenant_key
|
30
|
+
single_tenant? || non_interactive? ? tenants.keys.first : TenantSelector.select(tenants, tenants.keys)
|
91
31
|
end
|
92
32
|
|
93
|
-
def
|
94
|
-
|
33
|
+
def single_tenant?
|
34
|
+
tenants.size == 1
|
95
35
|
end
|
96
36
|
|
97
|
-
def
|
98
|
-
|
37
|
+
def non_interactive?
|
38
|
+
!$stdin.tty?
|
99
39
|
end
|
100
40
|
|
101
|
-
def
|
102
|
-
|
41
|
+
def initialize_tenant(tenant_key)
|
42
|
+
TenantConfigurator.configure_tenant(tenant_key, tenants, context_class)
|
43
|
+
Output.print_success("Tenant initialized: #{tenant_key}")
|
103
44
|
end
|
104
45
|
|
105
|
-
def
|
106
|
-
|
107
|
-
|
46
|
+
def handle_setup_error(error)
|
47
|
+
Output.print_error("Error setting up tenant: #{error.message}")
|
48
|
+
Output.print_backtrace(error)
|
108
49
|
end
|
109
50
|
end
|
110
51
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'output'
|
4
|
+
|
5
|
+
module ConsoleKit
|
6
|
+
# For tenant configuration
|
7
|
+
module TenantConfigurator
|
8
|
+
class << self
|
9
|
+
def configure_tenant(key, tenants, context_class)
|
10
|
+
config = tenants[key]
|
11
|
+
return Output.print_error("No configuration found for tenant: #{key}") unless config
|
12
|
+
|
13
|
+
constants = config[:constants]
|
14
|
+
apply_context(context_class, constants)
|
15
|
+
setup_database_connections(context_class)
|
16
|
+
|
17
|
+
Output.print_success("Tenant set to: #{key}")
|
18
|
+
rescue StandardError => e
|
19
|
+
Output.print_error("Failed to configure tenant '#{key}': #{e.message}")
|
20
|
+
Output.print_backtrace(e)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def apply_context(context_class, constants)
|
26
|
+
context_class.tenant_shard = constants[:shard]
|
27
|
+
context_class.tenant_mongo_db = constants[:mongo_db]
|
28
|
+
context_class.partner_identifier = constants[:partner_code]
|
29
|
+
end
|
30
|
+
|
31
|
+
def setup_database_connections(context_class)
|
32
|
+
ApplicationRecord.establish_connection(context_class.tenant_shard.to_sym)
|
33
|
+
Mongoid.override_client(context_class.tenant_mongo_db.to_s)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'output'
|
4
|
+
|
5
|
+
module ConsoleKit
|
6
|
+
# For tenant selection
|
7
|
+
module TenantSelector
|
8
|
+
class << self
|
9
|
+
def select(tenants, keys)
|
10
|
+
print_tenant_selection_menu(tenants, keys)
|
11
|
+
|
12
|
+
max_attempts = 3
|
13
|
+
max_attempts.times do
|
14
|
+
index = prompt_user_for_selection(keys.size)
|
15
|
+
return nil if index.zero?
|
16
|
+
return keys[index - 1] if index.positive?
|
17
|
+
end
|
18
|
+
|
19
|
+
nil
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def print_tenant_selection_menu(tenants, keys)
|
25
|
+
Output.print_header('Multiple tenants detected. Please choose one:')
|
26
|
+
Output.print_info(' 0. Load without tenant (no tenant configuration)')
|
27
|
+
|
28
|
+
keys.each_with_index do |key, index|
|
29
|
+
partner = tenants.dig(key, :constants, :partner_code) || 'N/A'
|
30
|
+
Output.print_info(" #{index + 1}. #{key} (partner: #{partner})")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def prompt_user_for_selection(max_index)
|
35
|
+
Output.print_prompt("\nEnter the number of the tenant you want (or press Enter for default '1'): ")
|
36
|
+
input = $stdin.gets&.chomp&.strip
|
37
|
+
input = '1' if input.to_s.empty?
|
38
|
+
|
39
|
+
return invalid_input_response unless valid_integer?(input)
|
40
|
+
|
41
|
+
parsed_index = input.to_i
|
42
|
+
return invalid_range_response(max_index) unless parsed_index.between?(0, max_index)
|
43
|
+
|
44
|
+
parsed_index
|
45
|
+
end
|
46
|
+
|
47
|
+
def valid_integer?(input)
|
48
|
+
input.match?(/\A\d+\z/)
|
49
|
+
end
|
50
|
+
|
51
|
+
def invalid_input_response
|
52
|
+
Output.print_warning('Invalid input. Please enter a number.')
|
53
|
+
-1
|
54
|
+
end
|
55
|
+
|
56
|
+
def invalid_range_response(max_index)
|
57
|
+
Output.print_warning("Selection must be between 0 and #{max_index}.")
|
58
|
+
-1
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/lib/console_kit/version.rb
CHANGED
data/lib/console_kit.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
4
|
-
require_relative
|
5
|
-
require_relative
|
3
|
+
require_relative 'console_kit/version'
|
4
|
+
require_relative 'console_kit/setup'
|
5
|
+
require_relative 'console_kit/railtie' if defined?(Rails::Railtie)
|
6
6
|
|
7
7
|
module ConsoleKit
|
8
8
|
class Error < StandardError; end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/generators'
|
4
|
+
require 'rails/generators/base'
|
5
|
+
|
6
|
+
module ConsoleKit
|
7
|
+
module Generators
|
8
|
+
# Generates the required files
|
9
|
+
class InstallGenerator < Rails::Generators::Base
|
10
|
+
source_root File.expand_path('templates', __dir__)
|
11
|
+
|
12
|
+
def copy_initializer
|
13
|
+
initializer_path = Rails.root.join('config', 'initializers', 'console_kit.rb')
|
14
|
+
|
15
|
+
if File.exist?(initializer_path)
|
16
|
+
say_status('skipped', "Initializer already exists: #{initializer_path}", :yellow)
|
17
|
+
else
|
18
|
+
template 'console_kit.rb', 'config/initializers/console_kit.rb'
|
19
|
+
say_status('created', "Initializer generated at #{initializer_path}", :green)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def remind_about_customization
|
24
|
+
say "\n✅ Setup complete!", :green
|
25
|
+
say '👉 Please update `config/initializers/console_kit.rb` to set your `tenants` and `context_class`.', :green
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Auto-generated by ConsoleKit
|
4
|
+
# 👉 Customize the following configuration to fit your application
|
5
|
+
|
6
|
+
Rails.application.config.after_initialize do
|
7
|
+
ConsoleKit.configure do |config|
|
8
|
+
# TODO: Set your tenants source in the following format
|
9
|
+
# {
|
10
|
+
# key:
|
11
|
+
# {
|
12
|
+
# constants:
|
13
|
+
# {
|
14
|
+
# shard: # Active Record Shard
|
15
|
+
# mongo_db: # Mongo Shard (If Mongo Is being used)
|
16
|
+
# partner_code: # If partners are needed
|
17
|
+
# }
|
18
|
+
# }
|
19
|
+
# }
|
20
|
+
config.tenants = nil
|
21
|
+
|
22
|
+
# TODO: Set your context class (e.g., CurrentContext)
|
23
|
+
config.context_class = nil
|
24
|
+
end
|
25
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: console_kit
|
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
|
- Soumyadeep Pal
|
@@ -10,33 +10,33 @@ cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
|
-
name:
|
13
|
+
name: mongoid
|
14
14
|
requirement: !ruby/object:Gem::Requirement
|
15
15
|
requirements:
|
16
16
|
- - ">="
|
17
17
|
- !ruby/object:Gem::Version
|
18
|
-
version:
|
18
|
+
version: '0'
|
19
19
|
type: :runtime
|
20
20
|
prerelease: false
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
22
22
|
requirements:
|
23
23
|
- - ">="
|
24
24
|
- !ruby/object:Gem::Version
|
25
|
-
version:
|
25
|
+
version: '0'
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
|
-
name:
|
27
|
+
name: rails
|
28
28
|
requirement: !ruby/object:Gem::Requirement
|
29
29
|
requirements:
|
30
30
|
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version:
|
32
|
+
version: 7.2.1
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
35
|
version_requirements: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
37
|
- - ">="
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version:
|
39
|
+
version: 7.2.1
|
40
40
|
description: Adds tenant selection to Rails consoles
|
41
41
|
email:
|
42
42
|
- soumyadeeppal2001@gmail.com
|
@@ -52,9 +52,14 @@ files:
|
|
52
52
|
- README.md
|
53
53
|
- Rakefile
|
54
54
|
- lib/console_kit.rb
|
55
|
+
- lib/console_kit/output.rb
|
55
56
|
- lib/console_kit/railtie.rb
|
56
57
|
- lib/console_kit/setup.rb
|
58
|
+
- lib/console_kit/tenant_configurator.rb
|
59
|
+
- lib/console_kit/tenant_selector.rb
|
57
60
|
- lib/console_kit/version.rb
|
61
|
+
- lib/generators/console_kit/install_generator.rb
|
62
|
+
- lib/generators/console_kit/templates/console_kit.rb
|
58
63
|
- sig/console_kit.rbs
|
59
64
|
homepage: https://github.com/Soumyadeep-ai/console_kit
|
60
65
|
licenses:
|