From 46e4569475052f777af3f4959304c8ba570e506a Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Tue, 14 Nov 2023 15:21:18 +0330 Subject: [PATCH] Show org and app on every command (#94) * Show org and app on every command exclude info, version, maintenance, env, ps, and latest_image --- lib/command/base.rb | 2 ++ lib/command/env.rb | 1 + lib/command/info.rb | 1 + lib/command/latest_image.rb | 1 + lib/command/maintenance.rb | 1 + lib/command/no_command.rb | 1 + lib/command/ps.rb | 1 + lib/command/version.rb | 1 + lib/core/config.rb | 6 +++--- lib/cpl.rb | 18 ++++++++++++++++++ 10 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/command/base.rb b/lib/command/base.rb index cd0a0ab2..e4cedf8a 100644 --- a/lib/command/base.rb +++ b/lib/command/base.rb @@ -23,6 +23,8 @@ class Base # rubocop:disable Metrics/ClassLength EXAMPLES = "" # If `true`, hides the command from `cpl help` HIDE = false + # 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/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/info.rb b/lib/command/info.rb index 5a0b2106..f2bf0d9b 100644 --- a/lib/command/info.rb +++ b/lib/command/info.rb @@ -26,6 +26,7 @@ class Info < Base # rubocop:disable Metrics/ClassLength cpl info -a $APP_NAME ``` EX + WITH_INFO_HEADER = false def call @missing_apps_workloads = {} 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/maintenance.rb b/lib/command/maintenance.rb index ba79c1ea..b4ef3081 100644 --- a/lib/command/maintenance.rb +++ b/lib/command/maintenance.rb @@ -14,6 +14,7 @@ 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 one_off_workload = config[:one_off_workload] diff --git a/lib/command/no_command.rb b/lib/command/no_command.rb index 69fda9b1..9a1844f9 100644 --- a/lib/command/no_command.rb +++ b/lib/command/no_command.rb @@ -9,6 +9,7 @@ class NoCommand < Base - Called when no command was specified DESC HIDE = true + WITH_INFO_HEADER = false def call if config.options[:version] 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! diff --git a/lib/command/version.rb b/lib/command/version.rb index c722c53c..ee9fd81a 100644 --- a/lib/command/version.rb +++ b/lib/command/version.rb @@ -8,6 +8,7 @@ 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 puts Cpl::VERSION diff --git a/lib/core/config.rb b/lib/core/config.rb index 34f0de53..7b47cce3 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 @@ -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 diff --git a/lib/cpl.rb b/lib/cpl.rb index db8749b2..98813aaf 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) + show_info_header(config) if with_info_header + command_class.new(config).call rescue RuntimeError => e ::Shell.abort(e.message) @@ -186,6 +189,21 @@ def self.all_base_commands rescue StandardError => e ::Shell.abort("Unable to load command: #{e.message}") end + + private + + def show_info_header(config) + rows = {} + rows["ORG"] = config.org || "NOT PROVIDED!" + rows["APP"] = config.app || "NOT PROVIDED!" + + rows.each do |key, value| + puts "#{key}: #{value}" + end + + # Add a newline after the info header + puts + end end end