Skip to content

Commit

Permalink
Updates to info command (#139)
Browse files Browse the repository at this point in the history
* fix: ensure that profile is switched back to default
* fix: allow using upstream that has match_if_app_name_starts_with set to true
* feat: change precedence for CPLN_ORG_UPSTREAM
* feat: allow specifying upstream through env var
* feat: updates to info command
  • Loading branch information
rafaelgomesxyz authored Jan 22, 2024
1 parent ce3d2d0 commit b600032
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 33 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ _Please add entries here for your pull requests that are not yet released._

- `build-image` command now accepts extra options and passes them to `docker build`. [PR 126](https://github.com/shakacode/heroku-to-control-plane/pull/126) by [Rafael Gomes](https://github.com/rafaelgomesxyz).
- `CPLN_ORG_UPSTREAM` env var now takes precedence over config from `controlplane.yml` in `copy-image-from-upstream` command. [PR 137](https://github.com/shakacode/heroku-to-control-plane/pull/137) by [Rafael Gomes](https://github.com/rafaelgomesxyz).
- `info` command now works properly for apps with `match_if_app_name_starts_with` set to `true`.[PR 139](https://github.com/shakacode/heroku-to-control-plane/pull/139) by [Rafael Gomes](https://github.com/rafaelgomesxyz).
- `info` command now lists workloads in the same order as `controlplane.yml`. [PR 139](https://github.com/shakacode/heroku-to-control-plane/pull/139) by [Rafael Gomes](https://github.com/rafaelgomesxyz).

## [1.2.0] - 2024-01-03

Expand Down
37 changes: 11 additions & 26 deletions lib/command/info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def call
@missing_apps_workloads = {}
@missing_apps_starting_with = {}

if config.app && !config.current[:match_if_app_name_starts_with]
if config.app && !config.should_app_start_with?(config.app)
single_app_info
else
multiple_apps_info
Expand All @@ -41,20 +41,8 @@ def call

private

def app_matches?(app, app_name, app_options)
app == app_name.to_s || (app_options[:match_if_app_name_starts_with] && app.start_with?(app_name.to_s))
end

def find_app_options(app)
@app_options ||= {}
@app_options[app] ||= config.apps.find do |app_name, app_options|
app_matches?(app, app_name, app_options)
end&.last
end

def find_workloads(app)
app_options = find_app_options(app)
return [] if app_options.nil?
app_options = config.find_app_config(app)

(app_options[:app_workloads] + app_options[:additional_workloads] + [app_options[:one_off_workload]]).uniq
end
Expand All @@ -75,21 +63,19 @@ def fetch_app_workloads(org) # rubocop:disable Metrics/MethodLength
end

if config.app
result.select { |app, _| app_matches?(app, config.app, config.current) }
result.select { |app, _| config.app_matches?(app, config.app, config.current) }
else
result.reject { |app, _| find_app_options(app).nil? }
result.reject { |app, _| config.find_app_config(app).nil? }
end
end

def orgs # rubocop:disable Metrics/MethodLength
def orgs
result = []

if config.org
result.push(config.org)
else
config.apps.each do |app_name, app_options|
next if config.app && !app_matches?(config.app, app_name, app_options)

config.apps.each do |_, app_options|
org = app_options[:cpln_org]
result.push(org) if org && !result.include?(org)
end
Expand All @@ -102,7 +88,7 @@ def apps(org)
result = []

config.apps.each do |app_name, app_options|
next if config.app && !app_matches?(config.app, app_name, app_options)
next if config.app && !config.app_matches?(config.app, app_name, app_options)

app_org = app_options[:cpln_org]
result.push(app_name.to_s) if app_org == org
Expand All @@ -113,7 +99,7 @@ def apps(org)
end

def any_app_starts_with?(app)
@app_workloads.keys.find { |app_name| app_matches?(app_name, app, config.apps[app.to_sym]) }
@app_workloads.keys.find { |app_name| config.app_matches?(app_name, app, config.apps[app.to_sym]) }
end

def check_any_app_starts_with(app)
Expand Down Expand Up @@ -184,8 +170,7 @@ def print_missing_apps_starting_with
"(replace 'whatever' with whatever suffix you want):"

@missing_apps_starting_with.each do |app, _workloads|
app_with_suffix = "#{app}#{app.end_with?('-') ? '' : '-'}whatever"
puts " - `cpl setup-app -a #{app_with_suffix}`"
puts " - `cpl setup-app -a #{app}-whatever`"
end
end

Expand All @@ -197,7 +182,7 @@ def single_app_info
@defined_workloads = find_workloads(config.app)
@available_workloads = fetch_workloads(config.app)

workloads = (@defined_workloads + @available_workloads).uniq.sort
workloads = (@defined_workloads + @available_workloads).uniq
workloads.each do |workload|
print_workload(config.app, workload)
end
Expand All @@ -217,7 +202,7 @@ def multiple_apps_info # rubocop:disable Metrics/MethodLength
@defined_workloads = find_workloads(app)
@available_workloads = @app_workloads[app] || []

workloads = (@defined_workloads + @available_workloads).uniq.sort
workloads = (@defined_workloads + @available_workloads).uniq
workloads.each do |workload|
print_workload(app, workload)
end
Expand Down
14 changes: 7 additions & 7 deletions lib/core/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ def current
end
end

def app_matches?(app_name1, app_name2, app_options)
app_name1 && app_name2 &&
(app_name1.to_s == app_name2.to_s ||
(app_options[:match_if_app_name_starts_with] && app_name1.to_s.start_with?(app_name2.to_s))
)
end

def find_app_config(app_name1)
@app_configs ||= {}
@app_configs[app_name1] ||= apps.find do |app_name2, app_config|
Expand All @@ -127,13 +134,6 @@ def ensure_config_app!(app_name, app_options)
raise "Can't find config for app '#{app_name}' in 'controlplane.yml'." unless app_options
end

def app_matches?(app_name1, app_name2, app_options)
app_name1 && app_name2 &&
(app_name1.to_s == app_name2.to_s ||
(app_options[:match_if_app_name_starts_with] && app_name1.to_s.start_with?(app_name2.to_s))
)
end

def ensure_app!
return if app

Expand Down

0 comments on commit b600032

Please sign in to comment.