Skip to content

Commit

Permalink
feat: add --domain option to maintenance commands
Browse files Browse the repository at this point in the history
Also allow specifying domain through `default_domain`.
  • Loading branch information
rafaelgomesxyz committed Jan 12, 2024
1 parent 54b2586 commit f1fd41f
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions examples/controlplane.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions lib/command/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions lib/command/maintenance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 " \
Expand Down
9 changes: 7 additions & 2 deletions lib/command/maintenance_off.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 " \
Expand Down
9 changes: 7 additions & 2 deletions lib/command/maintenance_on.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 " \
Expand Down
14 changes: 14 additions & 0 deletions lib/core/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand Down Expand Up @@ -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?
Expand Down

0 comments on commit f1fd41f

Please sign in to comment.