From 3a7ebf62ee57c66766b36efd5368b00821ca8479 Mon Sep 17 00:00:00 2001 From: Rafael Gomes Date: Fri, 27 Oct 2023 17:37:01 -0300 Subject: [PATCH] Small improvements (#88) * feat: add --org option to all commands * feat: change precedence for org * feat: default to CPLN_APP env var for --app option * fix: info command does not respect CPLN_ORG env var * feat: change color in config command from yellow to red * docs: update changelog --- CHANGELOG.md | 15 ++++++++++++++- docs/commands.md | 2 +- lib/command/base.rb | 14 ++++++++++---- lib/command/config.rb | 2 +- lib/command/info.rb | 12 ++++-------- lib/core/config.rb | 14 ++++++-------- lib/cpl.rb | 2 +- spec/command/cleanup_images_spec.rb | 1 + spec/command/copy_image_from_upstream_spec.rb | 1 + spec/command/maintenance_off_spec.rb | 1 + spec/command/maintenance_on_spec.rb | 1 + spec/command/maintenance_set_page_spec.rb | 1 + spec/command/maintenance_spec.rb | 1 + spec/command/run_cleanup_spec.rb | 1 + 14 files changed, 44 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 663b8c65..e14ab5ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,19 @@ Changes since the last non-beta release. _Please add entries here for your pull requests that are not yet released._ +### Fixed + +- Fixed issue where `info` command does not respect `CPLN_ORG` env var. [PR 88](https://github.com/shakacode/heroku-to-control-plane/pull/88) by [Rafael Gomes](https://github.com/rafaelgomesxyz). + +### Added + +- Added `--org` option to all commands. [PR 88](https://github.com/shakacode/heroku-to-control-plane/pull/88) by [Rafael Gomes](https://github.com/rafaelgomesxyz). +- Added option to set the app with a `CPLN_APP` env var. [PR 88](https://github.com/shakacode/heroku-to-control-plane/pull/88) by [Rafael Gomes](https://github.com/rafaelgomesxyz). + +### Changed + +- `--org` option now takes precedence over `CPLN_ORG` env var, which takes precedence over `cpln_org` from `controlplane.yml`. [PR 88](https://github.com/shakacode/heroku-to-control-plane/pull/88) by [Rafael Gomes](https://github.com/rafaelgomesxyz). + ## [1.1.2] - 2023-10-17 ### Fixed @@ -23,7 +36,7 @@ _Please add entries here for your pull requests that are not yet released._ ### Added - Added `open-console` command to open the app console on Control Plane. [PR 83](https://github.com/shakacode/heroku-to-control-plane/pull/83) by [Rafael Gomes](https://github.com/rafaelgomesxyz). -- Added option to set the org with a `CPLN_ORG` env var. [PR 83](https://github.com/shakacode/heroku-to-control-plane/pull/83) by [Rafael Gomes](https://github.com/rafaelgomesxyz). +- Added option to set the org with a `CPLN_ORG`/`CPLN_ORG_UPSTREAM` env var. [PR 83](https://github.com/shakacode/heroku-to-control-plane/pull/83) by [Rafael Gomes](https://github.com/rafaelgomesxyz). - Added `--verbose` option to all commands for more detailed logs. [PR 83](https://github.com/shakacode/heroku-to-control-plane/pull/83) by [Rafael Gomes](https://github.com/rafaelgomesxyz). ### Changed diff --git a/docs/commands.md b/docs/commands.md index dd1182a3..d77c32f3 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -140,7 +140,7 @@ if [ cpl exists -a $APP_NAME ]; ... - The diff is based on what's defined in the `.controlplane/controlplane.yml` file ```sh -# Shows diff for all apps in all orgs. +# Shows diff for all apps in all orgs (based on `.controlplane/controlplane.yml`). cpl info # Shows diff for all apps in a specific org. diff --git a/lib/command/base.rb b/lib/command/base.rb index 99c12d10..cd0a0ab2 100644 --- a/lib/command/base.rb +++ b/lib/command/base.rb @@ -38,7 +38,11 @@ def self.all_commands end end - def self.org_option(required: false) + def self.common_options + [org_option, verbose_option] + end + + def self.org_option(required: false) # rubocop:disable Metrics/MethodLength { name: :org, params: { @@ -46,12 +50,13 @@ def self.org_option(required: false) banner: "ORG_NAME", desc: "Organization name", type: :string, - required: required + required: required, + default: ENV.fetch("CPLN_ORG", nil) } } end - def self.app_option(required: false) + def self.app_option(required: false) # rubocop:disable Metrics/MethodLength { name: :app, params: { @@ -59,7 +64,8 @@ def self.app_option(required: false) banner: "APP_NAME", desc: "Application name", type: :string, - required: required + required: required, + default: ENV.fetch("CPLN_APP", nil) } } end diff --git a/lib/command/config.rb b/lib/command/config.rb index 3b5cdd7e..d468fb6f 100644 --- a/lib/command/config.rb +++ b/lib/command/config.rb @@ -22,7 +22,7 @@ class Config < Base def call # rubocop:disable Metrics/MethodLength if config.org_comes_from_env - puts Shell.color("Org comes from CPLN_ORG env var", :yellow) + puts Shell.color("Org comes from CPLN_ORG env var", :red) puts end diff --git a/lib/command/info.rb b/lib/command/info.rb index 9fdd4a4b..5a0b2106 100644 --- a/lib/command/info.rb +++ b/lib/command/info.rb @@ -4,7 +4,6 @@ module Command class Info < Base # rubocop:disable Metrics/ClassLength NAME = "info" OPTIONS = [ - org_option, app_option ].freeze DESCRIPTION = "Displays the diff between defined/available apps/workloads (apps equal GVCs)" @@ -17,7 +16,7 @@ class Info < Base # rubocop:disable Metrics/ClassLength DESC EXAMPLES = <<~EX ```sh - # Shows diff for all apps in all orgs. + # Shows diff for all apps in all orgs (based on `.controlplane/controlplane.yml`). cpl info # Shows diff for all apps in a specific org. @@ -81,15 +80,12 @@ def fetch_app_workloads(org) # rubocop:disable Metrics/MethodLength end end - def orgs # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity + def orgs # rubocop:disable Metrics/MethodLength result = [] - if config.options[:org] - result.push(config.options[:org]) + if config.org + result.push(config.org) else - org_from_env = ENV.fetch("CPLN_ORG", nil) - result.push(org_from_env) if org_from_env - config.apps.each do |app_name, app_options| next if config.app && !app_matches?(config.app, app_name, app_options) diff --git a/lib/core/config.rb b/lib/core/config.rb index c40131d1..34f0de53 100644 --- a/lib/core/config.rb +++ b/lib/core/config.rb @@ -12,7 +12,7 @@ def initialize(args, options) @args = args @options = options @org = options[:org] - @org_comes_from_env = false + @org_comes_from_env = true if ENV.fetch("CPLN_ORG", nil) @app = options[:app] load_app_config @@ -55,7 +55,8 @@ def ensure_current_config_org!(app_name) return if @org raise "Can't find option 'cpln_org' for app '#{app_name}' in 'controlplane.yml', " \ - "and CPLN_ORG env var is not set." + "and CPLN_ORG env var is not set. " \ + "The org can also be provided through --org." end def ensure_config! @@ -78,12 +79,9 @@ def pick_current_config(app_name, app_options) @current = app_options ensure_current_config_app!(app_name) - if current.key?(:cpln_org) - @org = current.fetch(:cpln_org) - else - @org = ENV.fetch("CPLN_ORG", nil) - @org_comes_from_env = true - end + return if @org + + @org = current.fetch(:cpln_org) if current.key?(:cpln_org) ensure_current_config_org!(app_name) end diff --git a/lib/cpl.rb b/lib/cpl.rb index 652dabf7..db8749b2 100644 --- a/lib/cpl.rb +++ b/lib/cpl.rb @@ -141,7 +141,7 @@ def self.all_base_commands usage = command_class::USAGE.empty? ? name : command_class::USAGE requires_args = command_class::REQUIRES_ARGS default_args = command_class::DEFAULT_ARGS - command_options = command_class::OPTIONS + [::Command::Base.verbose_option] + command_options = command_class::OPTIONS + ::Command::Base.common_options description = command_class::DESCRIPTION long_description = command_class::LONG_DESCRIPTION examples = command_class::EXAMPLES diff --git a/spec/command/cleanup_images_spec.rb b/spec/command/cleanup_images_spec.rb index b613e9d7..1b540a38 100644 --- a/spec/command/cleanup_images_spec.rb +++ b/spec/command/cleanup_images_spec.rb @@ -6,6 +6,7 @@ before do allow(ENV).to receive(:fetch).with("CPLN_ENDPOINT", "https://api.cpln.io").and_return("https://api.cpln.io") allow(ENV).to receive(:fetch).with("CPLN_TOKEN", nil).and_return("token") + allow(ENV).to receive(:fetch).with("CPLN_ORG", nil).and_return(nil) allow_any_instance_of(Config).to receive(:find_app_config_file).and_return("spec/fixtures/config.yml") # rubocop:disable RSpec/AnyInstance Timecop.freeze(Time.local(2023, 8, 23)) diff --git a/spec/command/copy_image_from_upstream_spec.rb b/spec/command/copy_image_from_upstream_spec.rb index e3fd52e6..270593f1 100644 --- a/spec/command/copy_image_from_upstream_spec.rb +++ b/spec/command/copy_image_from_upstream_spec.rb @@ -7,6 +7,7 @@ before do allow(ENV).to receive(:fetch).with("CPLN_ENDPOINT", "https://api.cpln.io").and_return("https://api.cpln.io") allow(ENV).to receive(:fetch).with("CPLN_TOKEN", nil).and_return("token") + allow(ENV).to receive(:fetch).with("CPLN_ORG", nil).and_return(nil) allow_any_instance_of(Config).to receive(:find_app_config_file).and_return("spec/fixtures/config.yml") allow_any_instance_of(described_class).to receive(:ensure_docker_running!) allow_any_instance_of(Controlplane).to receive(:profile_exists?).and_return(false) diff --git a/spec/command/maintenance_off_spec.rb b/spec/command/maintenance_off_spec.rb index e4b7ce33..01e3bb71 100644 --- a/spec/command/maintenance_off_spec.rb +++ b/spec/command/maintenance_off_spec.rb @@ -7,6 +7,7 @@ before do allow(ENV).to receive(:fetch).with("CPLN_ENDPOINT", "https://api.cpln.io").and_return("https://api.cpln.io") allow(ENV).to receive(:fetch).with("CPLN_TOKEN", nil).and_return("token") + allow(ENV).to receive(:fetch).with("CPLN_ORG", nil).and_return(nil) allow_any_instance_of(Config).to receive(:find_app_config_file).and_return("spec/fixtures/config.yml") allow_any_instance_of(described_class).to receive(:sleep).and_return(true) end diff --git a/spec/command/maintenance_on_spec.rb b/spec/command/maintenance_on_spec.rb index d19a1b25..455a6007 100644 --- a/spec/command/maintenance_on_spec.rb +++ b/spec/command/maintenance_on_spec.rb @@ -7,6 +7,7 @@ before do allow(ENV).to receive(:fetch).with("CPLN_ENDPOINT", "https://api.cpln.io").and_return("https://api.cpln.io") allow(ENV).to receive(:fetch).with("CPLN_TOKEN", nil).and_return("token") + allow(ENV).to receive(:fetch).with("CPLN_ORG", nil).and_return(nil) allow_any_instance_of(Config).to receive(:find_app_config_file).and_return("spec/fixtures/config.yml") allow_any_instance_of(described_class).to receive(:sleep).and_return(true) end diff --git a/spec/command/maintenance_set_page_spec.rb b/spec/command/maintenance_set_page_spec.rb index 74870865..33781992 100644 --- a/spec/command/maintenance_set_page_spec.rb +++ b/spec/command/maintenance_set_page_spec.rb @@ -6,6 +6,7 @@ before do allow(ENV).to receive(:fetch).with("CPLN_ENDPOINT", "https://api.cpln.io").and_return("https://api.cpln.io") allow(ENV).to receive(:fetch).with("CPLN_TOKEN", nil).and_return("token") + allow(ENV).to receive(:fetch).with("CPLN_ORG", nil).and_return(nil) allow_any_instance_of(Config).to receive(:find_app_config_file).and_return("spec/fixtures/config.yml") # rubocop:disable RSpec/AnyInstance end diff --git a/spec/command/maintenance_spec.rb b/spec/command/maintenance_spec.rb index 81d12d63..c0b6b4f1 100644 --- a/spec/command/maintenance_spec.rb +++ b/spec/command/maintenance_spec.rb @@ -6,6 +6,7 @@ before do allow(ENV).to receive(:fetch).with("CPLN_ENDPOINT", "https://api.cpln.io").and_return("https://api.cpln.io") allow(ENV).to receive(:fetch).with("CPLN_TOKEN", nil).and_return("token") + allow(ENV).to receive(:fetch).with("CPLN_ORG", nil).and_return(nil) allow_any_instance_of(Config).to receive(:find_app_config_file).and_return("spec/fixtures/config.yml") # rubocop:disable RSpec/AnyInstance end diff --git a/spec/command/run_cleanup_spec.rb b/spec/command/run_cleanup_spec.rb index a11a8469..6cc1219d 100644 --- a/spec/command/run_cleanup_spec.rb +++ b/spec/command/run_cleanup_spec.rb @@ -6,6 +6,7 @@ before do allow(ENV).to receive(:fetch).with("CPLN_ENDPOINT", "https://api.cpln.io").and_return("https://api.cpln.io") allow(ENV).to receive(:fetch).with("CPLN_TOKEN", nil).and_return("token") + allow(ENV).to receive(:fetch).with("CPLN_ORG", nil).and_return(nil) allow_any_instance_of(Config).to receive(:find_app_config_file).and_return("spec/fixtures/config.yml") # rubocop:disable RSpec/AnyInstance Timecop.freeze(Time.local(2023, 5, 15))