Skip to content

Commit

Permalink
Small improvements (#88)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rafaelgomesxyz authored Oct 27, 2023
1 parent 9baf46f commit 3a7ebf6
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 24 deletions.
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 10 additions & 4 deletions lib/command/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,34 @@ 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: {
aliases: ["-o"],
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: {
aliases: ["-a"],
banner: "APP_NAME",
desc: "Application name",
type: :string,
required: required
required: required,
default: ENV.fetch("CPLN_APP", nil)
}
}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/command/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 4 additions & 8 deletions lib/command/info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand All @@ -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.
Expand Down Expand Up @@ -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)

Expand Down
14 changes: 6 additions & 8 deletions lib/core/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/cpl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/command/cleanup_images_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions spec/command/copy_image_from_upstream_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions spec/command/maintenance_off_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/command/maintenance_on_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/command/maintenance_set_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions spec/command/maintenance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions spec/command/run_cleanup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 3a7ebf6

Please sign in to comment.