From 07d3b92b1288d7aa63e7bf82f40621589e92ffbb Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Tue, 10 Jun 2025 08:47:59 -0500 Subject: [PATCH] feat: Add development variant toggle rake task (#11696) --- lib/tasks/dev/variant_toggle.rake | 126 ++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 lib/tasks/dev/variant_toggle.rake diff --git a/lib/tasks/dev/variant_toggle.rake b/lib/tasks/dev/variant_toggle.rake new file mode 100644 index 000000000..643a37b9d --- /dev/null +++ b/lib/tasks/dev/variant_toggle.rake @@ -0,0 +1,126 @@ +# frozen_string_literal: true + +# rubocop:disable Metrics/BlockLength +namespace :chatwoot do + namespace :dev do + desc 'Toggle between Chatwoot variants with interactive menu' + task toggle_variant: :environment do + # Only allow in development environment + return unless Rails.env.development? + + show_current_variant + show_variant_menu + handle_user_selection + end + + desc 'Show current Chatwoot variant status' + task show_variant: :environment do + return unless Rails.env.development? + + show_current_variant + end + + private + + def show_current_variant + puts "\n#{('=' * 50)}" + puts 'šŸš€ CHATWOOT VARIANT MANAGER' + puts '=' * 50 + + # Check InstallationConfig + deployment_env = InstallationConfig.find_by(name: 'DEPLOYMENT_ENV')&.value + pricing_plan = InstallationConfig.find_by(name: 'INSTALLATION_PRICING_PLAN')&.value + + # Determine current variant based on configs + current_variant = if deployment_env == 'cloud' + 'Cloud' + elsif pricing_plan == 'enterprise' + 'Enterprise' + else + 'Community' + end + + puts "šŸ“Š Current Variant: #{current_variant}" + puts " Deployment Environment: #{deployment_env || 'Not set'}" + puts " Pricing Plan: #{pricing_plan || 'community'}" + puts '' + end + + def show_variant_menu + puts 'šŸŽÆ Select a variant to switch to:' + puts '' + puts '1. šŸ†“ Community (Free version with basic features)' + puts '2. šŸ¢ Enterprise (Self-hosted with premium features)' + puts '3. šŸŒ„ļø Cloud (Cloud deployment with premium features)' + puts '' + puts '0. āŒ Cancel' + puts '' + print 'Enter your choice (0-3): ' + end + + def handle_user_selection + choice = $stdin.gets.chomp + + case choice + when '1' + switch_to_variant('Community') { configure_community_variant } + when '2' + switch_to_variant('Enterprise') { configure_enterprise_variant } + when '3' + switch_to_variant('Cloud') { configure_cloud_variant } + when '0' + cancel_operation + else + invalid_choice + end + + puts "\nšŸŽ‰ Changes applied successfully! No restart required." + end + + def switch_to_variant(variant_name) + puts "\nšŸ”„ Switching to #{variant_name} variant..." + yield + clear_cache + puts "āœ… Successfully switched to #{variant_name} variant!" + end + + def cancel_operation + puts "\nāŒ Cancelled. No changes made." + exit 0 + end + + def invalid_choice + puts "\nāŒ Invalid choice. Please select 0-3." + puts 'No changes made.' + exit 1 + end + + def configure_community_variant + update_installation_config('DEPLOYMENT_ENV', 'self-hosted') + update_installation_config('INSTALLATION_PRICING_PLAN', 'community') + end + + def configure_enterprise_variant + update_installation_config('DEPLOYMENT_ENV', 'self-hosted') + update_installation_config('INSTALLATION_PRICING_PLAN', 'enterprise') + end + + def configure_cloud_variant + update_installation_config('DEPLOYMENT_ENV', 'cloud') + update_installation_config('INSTALLATION_PRICING_PLAN', 'enterprise') + end + + def update_installation_config(name, value) + config = InstallationConfig.find_or_initialize_by(name: name) + config.value = value + config.save! + puts " šŸ’¾ Updated #{name} → #{value}" + end + + def clear_cache + GlobalConfig.clear_cache + puts ' šŸ—‘ļø Cleared configuration cache' + end + end +end +# rubocop:enable Metrics/BlockLength