console_kit 0.1.1 ā 0.1.3
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/CHANGELOG.md +52 -1
- data/CODE_OF_CONDUCT.md +1 -1
- data/README.md +78 -11
- data/lib/console_kit/configuration.rb +14 -0
- data/lib/console_kit/output.rb +29 -22
- data/lib/console_kit/railtie.rb +1 -3
- data/lib/console_kit/setup.rb +56 -30
- data/lib/console_kit/tenant_configurator.rb +46 -11
- data/lib/console_kit/tenant_selector.rb +5 -11
- data/lib/console_kit/version.rb +1 -1
- data/lib/console_kit.rb +20 -1
- data/lib/generators/console_kit/install_generator.rb +10 -5
- data/lib/generators/console_kit/templates/console_kit.rb +26 -8
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2171674e2c587a2bb53387614758fefb8c6542f7ddb88c1b9dc55f85bf5a481a
|
|
4
|
+
data.tar.gz: 0fe05d8f5e3ee8d8814bba1b1eef1c3233b5c1996d805a4e2b06615326771480
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 763f319a96484ab677cad3eab21cc929e2775a98cf3d085db4fd317db8523f8220a3bcd808cedb3df857d193d1da9544de47a1263a8192a50888352e2fe1a575
|
|
7
|
+
data.tar.gz: 7acf3b5134fd7155ca4ceaadd44d07e93bf2f48fe322d4133055e11a8436157dcdf7b3ed7b303c76149674beccbd26780d5883590659aa040a8f55f8a7792cf0
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,56 @@
|
|
|
1
|
-
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
This project adheres to [Semantic Versioning](https://semver.org/).
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## [0.1.3] - 2025-08-12
|
|
10
|
+
### Added
|
|
11
|
+
- `ConsoleKit.current_tenant` method to retrieve the current tenant at runtime.
|
|
12
|
+
- `ConsoleKit.reset_current_tenant` to reset tenant selection.
|
|
13
|
+
- `pretty_output` configuration added with ability to manually toggle CLI verbosity.
|
|
14
|
+
|
|
15
|
+
### Changed
|
|
16
|
+
- Refactored internal logic for improved maintainability and future extensibility.
|
|
17
|
+
- Enhanced test coverage for better reliability and edge case handling.
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## [0.1.2] - 2025-07-23
|
|
22
|
+
### Added
|
|
23
|
+
- Changelog added.
|
|
24
|
+
- Readme and installation instructions added.
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## [0.1.1] - 2025-07-21
|
|
29
|
+
### Added
|
|
30
|
+
- Initial generator: `console_kit:install` to scaffold configuration.
|
|
31
|
+
- RSpec test suite to support core features.
|
|
32
|
+
|
|
33
|
+
### Changed
|
|
34
|
+
- Applied RuboCop fixes for code consistency and style.
|
|
35
|
+
|
|
36
|
+
---
|
|
2
37
|
|
|
3
38
|
## [0.1.0] - 2025-07-09
|
|
4
39
|
|
|
5
40
|
- Initial release
|
|
41
|
+
|
|
42
|
+
### Added
|
|
43
|
+
- Core setup logic for ConsoleKit:
|
|
44
|
+
- `ConsoleKit.setup`
|
|
45
|
+
- Tenant selection via CLI.
|
|
46
|
+
- Tenant-specific database configuration.
|
|
47
|
+
- Colorized console output for improved UX.
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## [Unreleased]
|
|
52
|
+
|
|
53
|
+
[0.1.3]: https://github.com/Soumyadeep-ai/console_kit/releases/tag/v0.1.3
|
|
54
|
+
[0.1.2]: https://github.com/Soumyadeep-ai/console_kit/releases/tag/v0.1.2
|
|
55
|
+
[0.1.1]: https://github.com/Soumyadeep-ai/console_kit/releases/tag/v0.1.1
|
|
56
|
+
[0.1.0]: https://github.com/Soumyadeep-ai/console_kit/releases/tag/v0.1.0
|
data/CODE_OF_CONDUCT.md
CHANGED
|
@@ -60,7 +60,7 @@ representative at an online or offline event.
|
|
|
60
60
|
|
|
61
61
|
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
62
|
reported to the community leaders responsible for enforcement at
|
|
63
|
-
[
|
|
63
|
+
[report here](https://github.com/Soumyadeep-ai/console_kit/issues).
|
|
64
64
|
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
65
|
|
|
66
66
|
All community leaders are obligated to respect the privacy and security of the
|
data/README.md
CHANGED
|
@@ -1,28 +1,95 @@
|
|
|
1
1
|
# ConsoleKit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
4
8
|
|
|
5
|
-
|
|
9
|
+
A simple and flexible multi-tenant console setup toolkit for Rails applications.
|
|
6
10
|
|
|
7
|
-
|
|
11
|
+
ConsoleKit helps you manage tenant-specific database connections and context configuration via an easy CLI interface and Rails integration.
|
|
8
12
|
|
|
9
|
-
|
|
13
|
+
## Installation
|
|
10
14
|
|
|
11
15
|
Install the gem and add to the application's Gemfile by executing:
|
|
12
16
|
|
|
13
|
-
```
|
|
14
|
-
bundle add
|
|
17
|
+
```ruby
|
|
18
|
+
bundle add console_kit
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Additionally you can also add this line to your application's Gemfile:
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
gem 'console_kit'
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
And then execute:
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
bundle install
|
|
15
31
|
```
|
|
16
32
|
|
|
17
33
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
18
34
|
|
|
19
|
-
```
|
|
20
|
-
gem install
|
|
35
|
+
```ruby
|
|
36
|
+
gem install console_kit
|
|
21
37
|
```
|
|
22
38
|
|
|
23
39
|
## Usage
|
|
24
40
|
|
|
25
|
-
|
|
41
|
+
After installing, generate the initializer and configuration files by running:
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
rails generate console_kit:install
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Then, edit config/initializers/console_kit.rb to define your tenants and context class. Example format:
|
|
48
|
+
|
|
49
|
+
```ruby
|
|
50
|
+
ConsoleKit.configure do |config|
|
|
51
|
+
config.tenants = {
|
|
52
|
+
tenant_one: {
|
|
53
|
+
constants: { shard: :tenant_one_db, mongo_db: :tenant_one_mongo, partner_code: 'partnerA' }
|
|
54
|
+
},
|
|
55
|
+
tenant_two: {
|
|
56
|
+
constants: { shard: :tenant_two_db, mongo_db: :tenant_two_mongo, partner_code: 'partnerB' }
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
config.context_class = CurrentContext
|
|
61
|
+
|
|
62
|
+
# Optional: Toggle pretty CLI output
|
|
63
|
+
config.pretty_output = true
|
|
64
|
+
end
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Console Usage
|
|
68
|
+
|
|
69
|
+
When launching the Rails console, ConsoleKit will prompt you to select a tenant (if tenants are configured).
|
|
70
|
+
You can also manually interact with it:
|
|
71
|
+
|
|
72
|
+
### Get Current Tenant
|
|
73
|
+
```ruby
|
|
74
|
+
ConsoleKit.current_tenant
|
|
75
|
+
# => :tenant_one
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Reset Current Tenant
|
|
79
|
+
```ruby
|
|
80
|
+
ConsoleKit.reset_current_tenant
|
|
81
|
+
# => nil
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Manually Disable Pretty Output
|
|
85
|
+
```ruby
|
|
86
|
+
ConsoleKit.enable_pretty_output
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Manually Disable Pretty Output
|
|
90
|
+
```ruby
|
|
91
|
+
ConsoleKit.disable_pretty_output
|
|
92
|
+
```
|
|
26
93
|
|
|
27
94
|
## Development
|
|
28
95
|
|
|
@@ -32,7 +99,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
|
32
99
|
|
|
33
100
|
## Contributing
|
|
34
101
|
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
|
102
|
+
Bug reports and pull requests are welcome on GitHub at [Console Kit](https://github.com/Soumyadeep-ai/console_kit). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/Soumyadeep-ai/console_kit/blob/main/CODE_OF_CONDUCT.md).
|
|
36
103
|
|
|
37
104
|
## License
|
|
38
105
|
|
|
@@ -40,4 +107,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
|
40
107
|
|
|
41
108
|
## Code of Conduct
|
|
42
109
|
|
|
43
|
-
Everyone interacting in the ConsoleKit project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
|
110
|
+
Everyone interacting in the ConsoleKit project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Soumyadeep-ai/console_kit/blob/main/CODE_OF_CONDUCT.md).
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ConsoleKit
|
|
4
|
+
# Stores configurations
|
|
5
|
+
class Configuration
|
|
6
|
+
attr_accessor :pretty_output, :tenants, :context_class
|
|
7
|
+
|
|
8
|
+
def initialize(pretty_output: true, tenants: nil, context_class: nil)
|
|
9
|
+
@pretty_output = pretty_output
|
|
10
|
+
@tenants = tenants
|
|
11
|
+
@context_class = context_class
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
data/lib/console_kit/output.rb
CHANGED
|
@@ -3,40 +3,47 @@
|
|
|
3
3
|
module ConsoleKit
|
|
4
4
|
# Handles Console outputs
|
|
5
5
|
module Output
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
PREFIX = '[ConsoleKit]'
|
|
7
|
+
TYPES = {
|
|
8
|
+
error: { symbol: '[ā]', color: '1;31' },
|
|
9
|
+
success: { symbol: '[ā]', color: '1;32' },
|
|
10
|
+
warning: { symbol: '[!]', color: '1;33' },
|
|
11
|
+
prompt: { symbol: nil, color: '1;36' },
|
|
12
|
+
header: { symbol: nil, color: '1;34' },
|
|
13
|
+
trace: { symbol: nil, color: '0;90' },
|
|
14
|
+
info: { symbol: nil, color: nil }
|
|
15
|
+
}.freeze
|
|
10
16
|
|
|
11
|
-
|
|
12
|
-
|
|
17
|
+
class << self
|
|
18
|
+
TYPES.each_key do |type|
|
|
19
|
+
define_method("print_#{type}") do |text|
|
|
20
|
+
formatted = (type == :header ? "\n=== #{text} ===" : text)
|
|
21
|
+
print_with(type, formatted)
|
|
22
|
+
end
|
|
13
23
|
end
|
|
14
24
|
|
|
15
25
|
def print_backtrace(exception)
|
|
16
|
-
exception
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
def print_header(text)
|
|
20
|
-
print_message("\n=== #{text} ===", '1;34') # Bold Blue
|
|
26
|
+
exception&.backtrace&.each { |line| print_with(:trace, " #{line}") }
|
|
21
27
|
end
|
|
22
28
|
|
|
23
|
-
|
|
24
|
-
print_message(text)
|
|
25
|
-
end
|
|
29
|
+
private
|
|
26
30
|
|
|
27
|
-
def
|
|
28
|
-
|
|
31
|
+
def print_with(type, text, timestamp: false)
|
|
32
|
+
meta = TYPES[type]
|
|
33
|
+
message = build_message(text, meta[:symbol], timestamp)
|
|
34
|
+
output(message, meta[:color])
|
|
29
35
|
end
|
|
30
36
|
|
|
31
|
-
def
|
|
32
|
-
|
|
37
|
+
def build_message(text, symbol, timestamp)
|
|
38
|
+
time = timestamp ? "[#{Time.current.strftime('%Y-%m-%d %H:%M:%S')}] " : ''
|
|
39
|
+
sym = symbol ? "#{symbol} " : ''
|
|
40
|
+
"#{PREFIX} #{time}#{sym}#{text}"
|
|
33
41
|
end
|
|
34
42
|
|
|
35
|
-
|
|
43
|
+
def output(message, color)
|
|
44
|
+
return puts message unless ConsoleKit.configuration.pretty_output && color
|
|
36
45
|
|
|
37
|
-
|
|
38
|
-
msg = "[ConsoleKit] #{text}"
|
|
39
|
-
puts color ? "\e[#{color}m#{msg}\e[0m" : msg
|
|
46
|
+
puts "\e[#{color}m#{message}\e[0m"
|
|
40
47
|
end
|
|
41
48
|
end
|
|
42
49
|
end
|
data/lib/console_kit/railtie.rb
CHANGED
data/lib/console_kit/setup.rb
CHANGED
|
@@ -6,46 +6,72 @@ require_relative 'output'
|
|
|
6
6
|
|
|
7
7
|
# Core Logic for initial Setup
|
|
8
8
|
module ConsoleKit
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
# Does the initial setup
|
|
10
|
+
module Setup
|
|
11
|
+
class << self
|
|
12
|
+
attr_reader :current_tenant
|
|
11
13
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
+
def setup
|
|
15
|
+
return Output.print_error('No tenants configured.') if no_tenants?
|
|
14
16
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
+
key = select_tenant_key
|
|
18
|
+
return Output.print_error('No tenant selected. Loading without tenant configuration.') unless key
|
|
17
19
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
configure(key)
|
|
21
|
+
rescue StandardError => e
|
|
22
|
+
handle_error(e)
|
|
23
|
+
end
|
|
22
24
|
|
|
23
|
-
|
|
24
|
-
yield self
|
|
25
|
-
end
|
|
25
|
+
def tenant_setup_successful? = !@current_tenant.to_s.empty?
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
def reset_current_tenant
|
|
28
|
+
return warn_no_tenants unless tenants?
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
end
|
|
30
|
+
warn_reset if @current_tenant
|
|
31
|
+
TenantConfigurator.clear(ConsoleKit.configuration.context_class) if @current_tenant
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
@current_tenant = nil
|
|
34
|
+
setup
|
|
35
|
+
tenant_setup_successful?
|
|
36
|
+
end
|
|
36
37
|
|
|
37
|
-
|
|
38
|
-
!$stdin.tty?
|
|
39
|
-
end
|
|
38
|
+
private
|
|
40
39
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
def configure(key)
|
|
41
|
+
return unless TenantConfigurator.configure_tenant(key, tenants, context_class)
|
|
42
|
+
|
|
43
|
+
@current_tenant = key
|
|
44
|
+
Output.print_success("Tenant initialized: #{key}")
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def tenants = ConsoleKit.configuration.tenants
|
|
48
|
+
def context_class = ConsoleKit.configuration.context_class
|
|
49
|
+
def tenants? = tenants&.any?
|
|
50
|
+
def no_tenants? = !tenants?
|
|
51
|
+
|
|
52
|
+
def select_tenant_key
|
|
53
|
+
return tenants.keys.first if auto_select?
|
|
54
|
+
|
|
55
|
+
TenantSelector.select(tenants, tenants.keys)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def auto_select? = single_tenant? || non_interactive?
|
|
59
|
+
def single_tenant? = tenants.size == 1
|
|
60
|
+
def non_interactive? = !$stdin.tty?
|
|
61
|
+
|
|
62
|
+
def warn_no_tenants
|
|
63
|
+
Output.print_warning('Cannot reset tenant: No tenants configured.')
|
|
64
|
+
false
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def warn_reset
|
|
68
|
+
Output.print_warning("Resetting tenant: #{@current_tenant}")
|
|
69
|
+
end
|
|
45
70
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
71
|
+
def handle_error(error)
|
|
72
|
+
Output.print_error("Error setting up tenant: #{error.message}")
|
|
73
|
+
Output.print_backtrace(error)
|
|
74
|
+
end
|
|
49
75
|
end
|
|
50
76
|
end
|
|
51
77
|
end
|
|
@@ -7,31 +7,66 @@ module ConsoleKit
|
|
|
7
7
|
module TenantConfigurator
|
|
8
8
|
class << self
|
|
9
9
|
def configure_tenant(key, tenants, context_class)
|
|
10
|
-
|
|
11
|
-
return
|
|
10
|
+
constants = tenants[key]&.[](:constants)
|
|
11
|
+
return missing_config_error(key) unless constants
|
|
12
12
|
|
|
13
|
-
constants
|
|
13
|
+
validate_constants!(constants)
|
|
14
14
|
apply_context(context_class, constants)
|
|
15
|
-
|
|
15
|
+
setup_connections(context_class)
|
|
16
16
|
|
|
17
17
|
Output.print_success("Tenant set to: #{key}")
|
|
18
|
+
true
|
|
18
19
|
rescue StandardError => e
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
handle_error(e, key)
|
|
21
|
+
false
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def clear(context_class)
|
|
25
|
+
%i[tenant_shard tenant_mongo_db partner_identifier].each do |attr|
|
|
26
|
+
context_class.public_send("#{attr}=", nil)
|
|
27
|
+
end
|
|
28
|
+
Output.print_info('Tenant context has been cleared.')
|
|
21
29
|
end
|
|
22
30
|
|
|
23
31
|
private
|
|
24
32
|
|
|
25
|
-
def
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
33
|
+
def validate_constants!(constants)
|
|
34
|
+
missing = %i[shard partner_code] - constants.keys
|
|
35
|
+
raise "Tenant constants missing keys: #{missing.join(', ')}" unless missing.empty?
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def missing_config_error(key)
|
|
39
|
+
Output.print_error("No configuration found for tenant: #{key}")
|
|
40
|
+
false
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def apply_context(ctx, constant)
|
|
44
|
+
ctx.tenant_shard = constant[:shard]
|
|
45
|
+
ctx.tenant_mongo_db = constant[:mongo_db]
|
|
46
|
+
ctx.partner_identifier = constant[:partner_code]
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def setup_connections(ctx)
|
|
50
|
+
ApplicationRecord.establish_connection(ctx.tenant_shard.to_sym) if defined?(ApplicationRecord)
|
|
51
|
+
return unless defined?(Mongoid) && Mongoid.respond_to?(:override_client)
|
|
52
|
+
return if ctx.tenant_mongo_db.nil? || ctx.tenant_mongo_db.empty?
|
|
53
|
+
|
|
54
|
+
client = ctx.tenant_mongo_db.to_s
|
|
55
|
+
Mongoid.override_client(client)
|
|
29
56
|
end
|
|
30
57
|
|
|
31
58
|
def setup_database_connections(context_class)
|
|
32
|
-
ApplicationRecord.establish_connection(context_class.tenant_shard.to_sym)
|
|
59
|
+
ApplicationRecord.establish_connection(context_class.tenant_shard.to_sym) if defined?(ApplicationRecord)
|
|
60
|
+
return unless defined?(Mongoid) && Mongoid.respond_to?(:override_client)
|
|
61
|
+
return if context_class.tenant_mongo_db.nil? || context_class.tenant_mongo_db.empty?
|
|
62
|
+
|
|
33
63
|
Mongoid.override_client(context_class.tenant_mongo_db.to_s)
|
|
34
64
|
end
|
|
65
|
+
|
|
66
|
+
def handle_error(error, key)
|
|
67
|
+
Output.print_error("Failed to configure tenant '#{key}': #{error.message}")
|
|
68
|
+
Output.print_backtrace(error)
|
|
69
|
+
end
|
|
35
70
|
end
|
|
36
71
|
end
|
|
37
72
|
end
|
|
@@ -9,11 +9,12 @@ module ConsoleKit
|
|
|
9
9
|
def select(tenants, keys)
|
|
10
10
|
print_tenant_selection_menu(tenants, keys)
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
max_attempts.times do
|
|
12
|
+
3.times do |attempt|
|
|
14
13
|
index = prompt_user_for_selection(keys.size)
|
|
15
14
|
return nil if index.zero?
|
|
16
15
|
return keys[index - 1] if index.positive?
|
|
16
|
+
|
|
17
|
+
print_tenant_selection_menu(tenants, keys) if attempt < 2
|
|
17
18
|
end
|
|
18
19
|
|
|
19
20
|
nil
|
|
@@ -35,7 +36,6 @@ module ConsoleKit
|
|
|
35
36
|
Output.print_prompt("\nEnter the number of the tenant you want (or press Enter for default '1'): ")
|
|
36
37
|
input = $stdin.gets&.chomp&.strip
|
|
37
38
|
input = '1' if input.to_s.empty?
|
|
38
|
-
|
|
39
39
|
return invalid_input_response unless valid_integer?(input)
|
|
40
40
|
|
|
41
41
|
parsed_index = input.to_i
|
|
@@ -44,14 +44,8 @@ module ConsoleKit
|
|
|
44
44
|
parsed_index
|
|
45
45
|
end
|
|
46
46
|
|
|
47
|
-
def valid_integer?(input)
|
|
48
|
-
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
def invalid_input_response
|
|
52
|
-
Output.print_warning('Invalid input. Please enter a number.')
|
|
53
|
-
-1
|
|
54
|
-
end
|
|
47
|
+
def valid_integer?(input) = input.match?(/\A\d+\z/)
|
|
48
|
+
def invalid_input_response = Output.print_warning('Invalid input. Please enter a number.').then { - 1 }
|
|
55
49
|
|
|
56
50
|
def invalid_range_response(max_index)
|
|
57
51
|
Output.print_warning("Selection must be between 0 and #{max_index}.")
|
data/lib/console_kit/version.rb
CHANGED
data/lib/console_kit.rb
CHANGED
|
@@ -1,10 +1,29 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative 'console_kit/version'
|
|
4
|
+
require_relative 'console_kit/configuration'
|
|
4
5
|
require_relative 'console_kit/setup'
|
|
5
6
|
require_relative 'console_kit/railtie' if defined?(Rails::Railtie)
|
|
6
7
|
|
|
8
|
+
# Main module for console kit
|
|
7
9
|
module ConsoleKit
|
|
8
10
|
class Error < StandardError; end
|
|
9
|
-
|
|
11
|
+
|
|
12
|
+
class << self
|
|
13
|
+
def configure = yield(configuration)
|
|
14
|
+
|
|
15
|
+
def configuration = Thread.current[:console_kit_configuration] ||= Configuration.new
|
|
16
|
+
def reset_configuration! = Thread.current[:console_kit_configuration] = nil
|
|
17
|
+
|
|
18
|
+
%i[pretty_output tenants context_class].each do |name|
|
|
19
|
+
define_method(name) { configuration.public_send(name) }
|
|
20
|
+
define_method("#{name}=") { |val| configuration.public_send("#{name}=", val) }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def current_tenant = Setup.current_tenant
|
|
24
|
+
def reset_current_tenant = Setup.reset_current_tenant
|
|
25
|
+
|
|
26
|
+
def enable_pretty_output = configuration.pretty_output = true
|
|
27
|
+
def disable_pretty_output = configuration.pretty_output = false
|
|
28
|
+
end
|
|
10
29
|
end
|
|
@@ -9,20 +9,25 @@ module ConsoleKit
|
|
|
9
9
|
class InstallGenerator < Rails::Generators::Base
|
|
10
10
|
source_root File.expand_path('templates', __dir__)
|
|
11
11
|
|
|
12
|
+
class_option :force, type: :boolean, default: false, desc: 'Overwrite existing files'
|
|
13
|
+
|
|
12
14
|
def copy_initializer
|
|
13
15
|
initializer_path = Rails.root.join('config', 'initializers', 'console_kit.rb')
|
|
14
16
|
|
|
15
|
-
if File.exist?(initializer_path)
|
|
16
|
-
say_status
|
|
17
|
+
if File.exist?(initializer_path) && !options[:force]
|
|
18
|
+
say_status :skipped, "Initializer already exists: #{initializer_path}", :yellow
|
|
17
19
|
else
|
|
18
|
-
template 'console_kit.rb', 'config/initializers/console_kit.rb'
|
|
19
|
-
say_status
|
|
20
|
+
template 'console_kit.rb', 'config/initializers/console_kit.rb', force: options[:force]
|
|
21
|
+
say_status :created, "Initializer generated at #{initializer_path}", :green
|
|
20
22
|
end
|
|
21
23
|
end
|
|
22
24
|
|
|
23
25
|
def remind_about_customization
|
|
24
26
|
say "\nā
Setup complete!", :green
|
|
25
|
-
say '
|
|
27
|
+
say 'š Modify `config/initializers/console_kit.rb`:', :green
|
|
28
|
+
%w[tenants context_class].each do |field|
|
|
29
|
+
say " - Set `#{field}` (required)", :green
|
|
30
|
+
end
|
|
26
31
|
end
|
|
27
32
|
end
|
|
28
33
|
end
|
|
@@ -5,15 +5,20 @@
|
|
|
5
5
|
|
|
6
6
|
Rails.application.config.after_initialize do
|
|
7
7
|
ConsoleKit.configure do |config|
|
|
8
|
-
# TODO: Set your tenants source
|
|
8
|
+
# TODO: Set your tenants source, example:
|
|
9
9
|
# {
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
#
|
|
16
|
-
#
|
|
10
|
+
# tenant_a: {
|
|
11
|
+
# constants: {
|
|
12
|
+
# shard: :shard_1,
|
|
13
|
+
# mongo_db: 'mongo_db_1',
|
|
14
|
+
# partner_code: 'partner_a'
|
|
15
|
+
# }
|
|
16
|
+
# },
|
|
17
|
+
# tenant_b: {
|
|
18
|
+
# constants: {
|
|
19
|
+
# shard: :shard_2,
|
|
20
|
+
# mongo_db: 'mongo_db_2',
|
|
21
|
+
# partner_code: 'partner_b'
|
|
17
22
|
# }
|
|
18
23
|
# }
|
|
19
24
|
# }
|
|
@@ -21,5 +26,18 @@ Rails.application.config.after_initialize do
|
|
|
21
26
|
|
|
22
27
|
# TODO: Set your context class (e.g., CurrentContext)
|
|
23
28
|
config.context_class = nil
|
|
29
|
+
|
|
30
|
+
# Toggle pretty output on/off (default: true)
|
|
31
|
+
config.pretty_output = true
|
|
32
|
+
|
|
33
|
+
if config.tenants.nil?
|
|
34
|
+
warn '[ConsoleKit] Warning: `tenants` is not configured. ' \
|
|
35
|
+
'Please set it in config/initializers/console_kit.rb'
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
if config.context_class.nil?
|
|
39
|
+
warn '[ConsoleKit] Warning: `context_class` is not configured. ' \
|
|
40
|
+
'Please set it in config/initializers/console_kit.rb'
|
|
41
|
+
end
|
|
24
42
|
end
|
|
25
43
|
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.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Soumyadeep Pal
|
|
@@ -52,6 +52,7 @@ files:
|
|
|
52
52
|
- README.md
|
|
53
53
|
- Rakefile
|
|
54
54
|
- lib/console_kit.rb
|
|
55
|
+
- lib/console_kit/configuration.rb
|
|
55
56
|
- lib/console_kit/output.rb
|
|
56
57
|
- lib/console_kit/railtie.rb
|
|
57
58
|
- lib/console_kit/setup.rb
|