From 40184f4433c20eaddb60e4672883c1680da423e8 Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Thu, 2 Nov 2023 18:00:57 +0330 Subject: [PATCH 01/11] Show org and app on every command --- lib/command/base.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/command/base.rb b/lib/command/base.rb index cd0a0ab2..ab530540 100644 --- a/lib/command/base.rb +++ b/lib/command/base.rb @@ -28,6 +28,20 @@ class Base # rubocop:disable Metrics/ClassLength def initialize(config) @config = config + + puts_info_header + end + + def puts_info_header + rows = {} + rows["ORG"] = config.org unless config.org.nil? || config.org.empty? + rows["APP"] = config.org unless config.app.nil? || config.app.empty? + + max_key_length = rows.keys.map(&:size).max + + rows.each do |key, value| + puts "#{key.ljust(max_key_length)}: #{value}" + end end def self.all_commands From 66a56bfe2d9676fcd7589f49429c3ec9be2e11ce Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Thu, 2 Nov 2023 18:01:22 +0330 Subject: [PATCH 02/11] Exclude info and version commands --- lib/command/info.rb | 4 ++++ lib/command/version.rb | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/lib/command/info.rb b/lib/command/info.rb index 5a0b2106..03e18f35 100644 --- a/lib/command/info.rb +++ b/lib/command/info.rb @@ -38,6 +38,10 @@ def call end end + def puts_info_header + # Do not print any info header + end + private def app_matches?(app, app_name, app_options) diff --git a/lib/command/version.rb b/lib/command/version.rb index c722c53c..c8353d62 100644 --- a/lib/command/version.rb +++ b/lib/command/version.rb @@ -12,5 +12,9 @@ class Version < Base def call puts Cpl::VERSION end + + def puts_info_header + # Do not print any info header + end end end From 24363d998c71b80093800376840c9d2d0048f14f Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Thu, 2 Nov 2023 18:25:07 +0330 Subject: [PATCH 03/11] Exclude maintenance command --- lib/command/maintenance.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/command/maintenance.rb b/lib/command/maintenance.rb index ba79c1ea..9742c787 100644 --- a/lib/command/maintenance.rb +++ b/lib/command/maintenance.rb @@ -33,5 +33,9 @@ def call # rubocop:disable Metrics/MethodLength puts "off" end end + + def puts_info_header + # Do not print any info header + end end end From 1893fabb84c7af7d5827027a1928ead9063e66e8 Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Fri, 3 Nov 2023 17:14:58 +0330 Subject: [PATCH 04/11] Move the logic into Clp::Cli class --- lib/command/base.rb | 16 ++-------------- lib/command/info.rb | 6 ++---- lib/command/maintenance.rb | 6 ++---- lib/command/no_command.rb | 2 ++ lib/command/version.rb | 6 ++---- lib/cpl.rb | 17 +++++++++++++++++ 6 files changed, 27 insertions(+), 26 deletions(-) diff --git a/lib/command/base.rb b/lib/command/base.rb index ab530540..cfa04d2a 100644 --- a/lib/command/base.rb +++ b/lib/command/base.rb @@ -23,25 +23,13 @@ class Base # rubocop:disable Metrics/ClassLength EXAMPLES = "" # If `true`, hides the command from `cpl help` HIDE = false + # weather to show key information like ORG and APP name in commands + WITH_INFO_HEADER = true NO_IMAGE_AVAILABLE = "NO_IMAGE_AVAILABLE" def initialize(config) @config = config - - puts_info_header - end - - def puts_info_header - rows = {} - rows["ORG"] = config.org unless config.org.nil? || config.org.empty? - rows["APP"] = config.org unless config.app.nil? || config.app.empty? - - max_key_length = rows.keys.map(&:size).max - - rows.each do |key, value| - puts "#{key.ljust(max_key_length)}: #{value}" - end end def self.all_commands diff --git a/lib/command/info.rb b/lib/command/info.rb index 03e18f35..35d41352 100644 --- a/lib/command/info.rb +++ b/lib/command/info.rb @@ -27,6 +27,8 @@ class Info < Base # rubocop:disable Metrics/ClassLength ``` EX + WITH_INFO_HEADER = false + def call @missing_apps_workloads = {} @missing_apps_starting_with = {} @@ -38,10 +40,6 @@ def call end end - def puts_info_header - # Do not print any info header - end - private def app_matches?(app, app_name, app_options) diff --git a/lib/command/maintenance.rb b/lib/command/maintenance.rb index 9742c787..81fca58b 100644 --- a/lib/command/maintenance.rb +++ b/lib/command/maintenance.rb @@ -15,6 +15,8 @@ class Maintenance < Base - Maintenance mode is only supported for domains that use path based routing mode and have a route configured for the prefix '/' on either port 80 or 443 DESC + WITH_INFO_HEADER = false + def call # rubocop:disable Metrics/MethodLength one_off_workload = config[:one_off_workload] maintenance_workload = config.current[:maintenance_workload] || "maintenance" @@ -33,9 +35,5 @@ def call # rubocop:disable Metrics/MethodLength puts "off" end end - - def puts_info_header - # Do not print any info header - end end end diff --git a/lib/command/no_command.rb b/lib/command/no_command.rb index 69fda9b1..9d0d692f 100644 --- a/lib/command/no_command.rb +++ b/lib/command/no_command.rb @@ -10,6 +10,8 @@ class NoCommand < Base DESC HIDE = true + WITH_INFO_HEADER = false + def call if config.options[:version] Cpl::Cli.start(["version"]) diff --git a/lib/command/version.rb b/lib/command/version.rb index c8353d62..7f824c14 100644 --- a/lib/command/version.rb +++ b/lib/command/version.rb @@ -9,12 +9,10 @@ class Version < Base - Can also be done with `cpl --version` or `cpl -v` DESC + WITH_INFO_HEADER = false + def call puts Cpl::VERSION end - - def puts_info_header - # Do not print any info header - end end end diff --git a/lib/cpl.rb b/lib/cpl.rb index db8749b2..25953136 100644 --- a/lib/cpl.rb +++ b/lib/cpl.rb @@ -146,6 +146,7 @@ def self.all_base_commands long_description = command_class::LONG_DESCRIPTION examples = command_class::EXAMPLES hide = command_class::HIDE || deprecated + with_info_header = command_class::WITH_INFO_HEADER long_description += "\n#{examples}" if examples.length.positive? @@ -178,6 +179,8 @@ def self.all_base_commands begin config = Config.new(args, options) + puts_info_header(config) if with_info_header + command_class.new(config).call rescue RuntimeError => e ::Shell.abort(e.message) @@ -186,6 +189,20 @@ def self.all_base_commands rescue StandardError => e ::Shell.abort("Unable to load command: #{e.message}") end + + private + + def puts_info_header(config) + rows = {} + rows["ORG"] = config.org unless config.org.nil? || config.org.empty? + rows["APP"] = config.org unless config.app.nil? || config.app.empty? + + max_key_length = rows.keys.map(&:size).max + + rows.each do |key, value| + puts "#{key.ljust(max_key_length)}: #{value}" + end + end end end From f8dbcdac2d8172612429625501fd6398b105a0cb Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Fri, 3 Nov 2023 17:20:10 +0330 Subject: [PATCH 05/11] Fix typo --- lib/cpl.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/cpl.rb b/lib/cpl.rb index 25953136..361231f2 100644 --- a/lib/cpl.rb +++ b/lib/cpl.rb @@ -195,12 +195,10 @@ def self.all_base_commands def puts_info_header(config) rows = {} rows["ORG"] = config.org unless config.org.nil? || config.org.empty? - rows["APP"] = config.org unless config.app.nil? || config.app.empty? - - max_key_length = rows.keys.map(&:size).max + rows["APP"] = config.app unless config.app.nil? || config.app.empty? rows.each do |key, value| - puts "#{key.ljust(max_key_length)}: #{value}" + puts "#{key}: #{value}" end end end From d8d17f9ccd9035b1234e141ffd2eb8ae50d5f3b7 Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Thu, 9 Nov 2023 15:18:37 +0330 Subject: [PATCH 06/11] Rename method to show_info_header --- lib/cpl.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cpl.rb b/lib/cpl.rb index 361231f2..7bbc332b 100644 --- a/lib/cpl.rb +++ b/lib/cpl.rb @@ -179,7 +179,7 @@ def self.all_base_commands begin config = Config.new(args, options) - puts_info_header(config) if with_info_header + show_info_header(config) if with_info_header command_class.new(config).call rescue RuntimeError => e @@ -192,7 +192,7 @@ def self.all_base_commands private - def puts_info_header(config) + def show_info_header(config) rows = {} rows["ORG"] = config.org unless config.org.nil? || config.org.empty? rows["APP"] = config.app unless config.app.nil? || config.app.empty? From 1ebcad0a1012b923d3beec7f0f1772c6e32e3944 Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Thu, 9 Nov 2023 16:20:20 +0330 Subject: [PATCH 07/11] Add newline after the info header --- lib/cpl.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/cpl.rb b/lib/cpl.rb index 7bbc332b..f4ffdc05 100644 --- a/lib/cpl.rb +++ b/lib/cpl.rb @@ -200,6 +200,8 @@ def show_info_header(config) rows.each do |key, value| puts "#{key}: #{value}" end + + puts unless rows.empty? end end end From 8ce0955f238690851db8e4dde09a940eb5430abc Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Thu, 9 Nov 2023 16:27:13 +0330 Subject: [PATCH 08/11] Minor fixes for with_info_header --- lib/command/base.rb | 2 +- lib/command/info.rb | 1 - lib/command/maintenance.rb | 1 - lib/command/no_command.rb | 1 - lib/command/version.rb | 1 - 5 files changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/command/base.rb b/lib/command/base.rb index cfa04d2a..e4cedf8a 100644 --- a/lib/command/base.rb +++ b/lib/command/base.rb @@ -23,7 +23,7 @@ class Base # rubocop:disable Metrics/ClassLength EXAMPLES = "" # If `true`, hides the command from `cpl help` HIDE = false - # weather to show key information like ORG and APP name in commands + # Whether or not to show key information like ORG and APP name in commands WITH_INFO_HEADER = true NO_IMAGE_AVAILABLE = "NO_IMAGE_AVAILABLE" diff --git a/lib/command/info.rb b/lib/command/info.rb index 35d41352..f2bf0d9b 100644 --- a/lib/command/info.rb +++ b/lib/command/info.rb @@ -26,7 +26,6 @@ class Info < Base # rubocop:disable Metrics/ClassLength cpl info -a $APP_NAME ``` EX - WITH_INFO_HEADER = false def call diff --git a/lib/command/maintenance.rb b/lib/command/maintenance.rb index 81fca58b..b4ef3081 100644 --- a/lib/command/maintenance.rb +++ b/lib/command/maintenance.rb @@ -14,7 +14,6 @@ class Maintenance < Base - Optionally specify the maintenance workload through `maintenance_workload` in the `.controlplane/controlplane.yml` file (defaults to 'maintenance') - Maintenance mode is only supported for domains that use path based routing mode and have a route configured for the prefix '/' on either port 80 or 443 DESC - WITH_INFO_HEADER = false def call # rubocop:disable Metrics/MethodLength diff --git a/lib/command/no_command.rb b/lib/command/no_command.rb index 9d0d692f..9a1844f9 100644 --- a/lib/command/no_command.rb +++ b/lib/command/no_command.rb @@ -9,7 +9,6 @@ class NoCommand < Base - Called when no command was specified DESC HIDE = true - WITH_INFO_HEADER = false def call diff --git a/lib/command/version.rb b/lib/command/version.rb index 7f824c14..ee9fd81a 100644 --- a/lib/command/version.rb +++ b/lib/command/version.rb @@ -8,7 +8,6 @@ class Version < Base - Displays the current version of the CLI - Can also be done with `cpl --version` or `cpl -v` DESC - WITH_INFO_HEADER = false def call From 9f2a2d287527eb1a88b68a0d738e7efc458b041d Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Thu, 9 Nov 2023 16:28:50 +0330 Subject: [PATCH 09/11] Disable with_info_header in env, ps, and latest_image --- lib/command/env.rb | 1 + lib/command/latest_image.rb | 1 + lib/command/ps.rb | 1 + 3 files changed, 3 insertions(+) diff --git a/lib/command/env.rb b/lib/command/env.rb index f1831607..fe7fc69b 100644 --- a/lib/command/env.rb +++ b/lib/command/env.rb @@ -10,6 +10,7 @@ class Env < Base LONG_DESCRIPTION = <<~DESC - Displays app-specific environment variables DESC + WITH_INFO_HEADER = false def call cp.fetch_gvc!.dig("spec", "env").map do |prop| diff --git a/lib/command/latest_image.rb b/lib/command/latest_image.rb index fc07ce05..eb439a79 100644 --- a/lib/command/latest_image.rb +++ b/lib/command/latest_image.rb @@ -10,6 +10,7 @@ class LatestImage < Base LONG_DESCRIPTION = <<~DESC - Displays the latest image name DESC + WITH_INFO_HEADER = false def call puts latest_image diff --git a/lib/command/ps.rb b/lib/command/ps.rb index f0bb61ed..3d7821da 100644 --- a/lib/command/ps.rb +++ b/lib/command/ps.rb @@ -20,6 +20,7 @@ class Ps < Base cpl ps -a $APP_NAME -w $WORKLOAD_NAME ``` EX + WITH_INFO_HEADER = false def call cp.fetch_gvc! From 39fe37057c9cf5e8286c6707173108ca2271fabd Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Sat, 11 Nov 2023 20:26:50 +0330 Subject: [PATCH 10/11] Strip the org and app before processing the info header --- lib/core/config.rb | 4 ++-- lib/cpl.rb | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/core/config.rb b/lib/core/config.rb index 34f0de53..65d8180b 100644 --- a/lib/core/config.rb +++ b/lib/core/config.rb @@ -11,9 +11,9 @@ class Config # rubocop:disable Metrics/ClassLength def initialize(args, options) @args = args @options = options - @org = options[:org] + @org = options[:org]&.strip @org_comes_from_env = true if ENV.fetch("CPLN_ORG", nil) - @app = options[:app] + @app = options[:app]&.strip load_app_config load_apps diff --git a/lib/cpl.rb b/lib/cpl.rb index f4ffdc05..98813aaf 100644 --- a/lib/cpl.rb +++ b/lib/cpl.rb @@ -194,14 +194,15 @@ def self.all_base_commands def show_info_header(config) rows = {} - rows["ORG"] = config.org unless config.org.nil? || config.org.empty? - rows["APP"] = config.app unless config.app.nil? || config.app.empty? + rows["ORG"] = config.org || "NOT PROVIDED!" + rows["APP"] = config.app || "NOT PROVIDED!" rows.each do |key, value| puts "#{key}: #{value}" end - puts unless rows.empty? + # Add a newline after the info header + puts end end end From 754940db89994381958d75a6cb024bf5898a498c Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Tue, 14 Nov 2023 15:17:08 +0330 Subject: [PATCH 11/11] Strip spaces on cpln_org --- lib/core/config.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core/config.rb b/lib/core/config.rb index 65d8180b..7b47cce3 100644 --- a/lib/core/config.rb +++ b/lib/core/config.rb @@ -81,7 +81,7 @@ def pick_current_config(app_name, app_options) return if @org - @org = current.fetch(:cpln_org) if current.key?(:cpln_org) + @org = current.fetch(:cpln_org)&.strip if current.key?(:cpln_org) ensure_current_config_org!(app_name) end