From f1fd41f21ddf6811f85e12ede428da2d8f12ab89 Mon Sep 17 00:00:00 2001 From: Rafael Gomes Date: Fri, 12 Jan 2024 15:39:14 -0300 Subject: [PATCH] feat: add --domain option to maintenance commands Also allow specifying domain through `default_domain`. --- README.md | 4 ++++ examples/controlplane.yml | 4 ++++ lib/command/base.rb | 12 ++++++++++++ lib/command/maintenance.rb | 9 +++++++-- lib/command/maintenance_off.rb | 9 +++++++-- lib/command/maintenance_on.rb | 9 +++++++-- lib/core/config.rb | 14 ++++++++++++++ 7 files changed, 55 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 15b98a1c..3b3b23ce 100644 --- a/README.md +++ b/README.md @@ -258,6 +258,10 @@ apps: # This is relative to the `.controlplane/` directory. release_script: release_script + # default_domain is used for commands that require a domain + # including `maintenance`, `maintenance:on`, `maintenance:off`, `maintenance:set-page`. + default_domain: domain.com + my-app-other: <<: *common diff --git a/examples/controlplane.yml b/examples/controlplane.yml index 50dfd4c3..8710098d 100644 --- a/examples/controlplane.yml +++ b/examples/controlplane.yml @@ -101,6 +101,10 @@ apps: # This is relative to the `.controlplane/` directory. release_script: release_script + # default_domain is used for commands that require a domain + # including `maintenance`, `maintenance:on`, `maintenance:off`, `maintenance:set-page`. + default_domain: domain.com + my-app-other: <<: *common diff --git a/lib/command/base.rb b/lib/command/base.rb index 0cb45090..6d802837 100644 --- a/lib/command/base.rb +++ b/lib/command/base.rb @@ -125,6 +125,18 @@ def self.location_option(required: false) } end + def self.domain_option(required: false) + { + name: :domain, + params: { + banner: "DOMAIN_NAME", + desc: "Domain name", + type: :string, + required: required + } + } + end + def self.upstream_token_option(required: false) { name: :upstream_token, diff --git a/lib/command/maintenance.rb b/lib/command/maintenance.rb index b4ef3081..514dadd1 100644 --- a/lib/command/maintenance.rb +++ b/lib/command/maintenance.rb @@ -4,7 +4,8 @@ module Command class Maintenance < Base NAME = "maintenance" OPTIONS = [ - app_option(required: true) + app_option(required: true), + domain_option ].freeze DESCRIPTION = "Checks if maintenance mode is on or off for an app" LONG_DESCRIPTION = <<~DESC @@ -20,7 +21,11 @@ def call # rubocop:disable Metrics/MethodLength one_off_workload = config[:one_off_workload] maintenance_workload = config.current[:maintenance_workload] || "maintenance" - domain_data = cp.find_domain_for([one_off_workload, maintenance_workload]) + domain_data = if config.domain + cp.fetch_domain(config.domain) + else + cp.find_domain_for([one_off_workload, maintenance_workload]) + end unless domain_data raise "Can't find domain. " \ "Maintenance mode is only supported for domains that use path based routing mode " \ diff --git a/lib/command/maintenance_off.rb b/lib/command/maintenance_off.rb index 0cd06ea4..03d0c86a 100644 --- a/lib/command/maintenance_off.rb +++ b/lib/command/maintenance_off.rb @@ -4,7 +4,8 @@ module Command class MaintenanceOff < Base NAME = "maintenance:off" OPTIONS = [ - app_option(required: true) + app_option(required: true), + domain_option ].freeze DESCRIPTION = "Disables maintenance mode for an app" LONG_DESCRIPTION = <<~DESC @@ -18,7 +19,11 @@ def call # rubocop:disable Metrics/MethodLength one_off_workload = config[:one_off_workload] maintenance_workload = config.current[:maintenance_workload] || "maintenance" - domain_data = cp.find_domain_for([one_off_workload, maintenance_workload]) + domain_data = if config.domain + cp.fetch_domain(config.domain) + else + cp.find_domain_for([one_off_workload, maintenance_workload]) + end unless domain_data raise "Can't find domain. " \ "Maintenance mode is only supported for domains that use path based routing mode " \ diff --git a/lib/command/maintenance_on.rb b/lib/command/maintenance_on.rb index aa25b8cf..ba734b33 100644 --- a/lib/command/maintenance_on.rb +++ b/lib/command/maintenance_on.rb @@ -4,7 +4,8 @@ module Command class MaintenanceOn < Base NAME = "maintenance:on" OPTIONS = [ - app_option(required: true) + app_option(required: true), + domain_option ].freeze DESCRIPTION = "Enables maintenance mode for an app" LONG_DESCRIPTION = <<~DESC @@ -18,7 +19,11 @@ def call # rubocop:disable Metrics/MethodLength one_off_workload = config[:one_off_workload] maintenance_workload = config.current[:maintenance_workload] || "maintenance" - domain_data = cp.find_domain_for([one_off_workload, maintenance_workload]) + domain_data = if config.domain + cp.fetch_domain(config.domain) + else + cp.find_domain_for([one_off_workload, maintenance_workload]) + end unless domain_data raise "Can't find domain. " \ "Maintenance mode is only supported for domains that use path based routing mode " \ diff --git a/lib/core/config.rb b/lib/core/config.rb index 13e10f45..fec91235 100644 --- a/lib/core/config.rb +++ b/lib/core/config.rb @@ -38,6 +38,10 @@ def location @location ||= load_location_from_options || load_location_from_env || load_location_from_file end + def domain + @domain ||= load_domain_from_options || load_domain_from_file + end + def [](key) ensure_current_config! @@ -253,6 +257,16 @@ def load_location_from_file strip_str_and_validate(current.fetch(:default_location)) end + def load_domain_from_options + strip_str_and_validate(options[:domain]) + end + + def load_domain_from_file + return unless current&.key?(:default_domain) + + strip_str_and_validate(current.fetch(:default_domain)) + end + def warn_deprecated_options(app_options) deprecated_option_keys = new_option_keys.select { |old_key| app_options.key?(old_key) } return if deprecated_option_keys.empty?