Skip to content

Commit b600032

Browse files
Updates to info command (#139)
* 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
1 parent ce3d2d0 commit b600032

File tree

3 files changed

+20
-33
lines changed

3 files changed

+20
-33
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ _Please add entries here for your pull requests that are not yet released._
2929

3030
- `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).
3131
- `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).
32+
- `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).
33+
- `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).
3234

3335
## [1.2.0] - 2024-01-03
3436

lib/command/info.rb

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def call
3232
@missing_apps_workloads = {}
3333
@missing_apps_starting_with = {}
3434

35-
if config.app && !config.current[:match_if_app_name_starts_with]
35+
if config.app && !config.should_app_start_with?(config.app)
3636
single_app_info
3737
else
3838
multiple_apps_info
@@ -41,20 +41,8 @@ def call
4141

4242
private
4343

44-
def app_matches?(app, app_name, app_options)
45-
app == app_name.to_s || (app_options[:match_if_app_name_starts_with] && app.start_with?(app_name.to_s))
46-
end
47-
48-
def find_app_options(app)
49-
@app_options ||= {}
50-
@app_options[app] ||= config.apps.find do |app_name, app_options|
51-
app_matches?(app, app_name, app_options)
52-
end&.last
53-
end
54-
5544
def find_workloads(app)
56-
app_options = find_app_options(app)
57-
return [] if app_options.nil?
45+
app_options = config.find_app_config(app)
5846

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

7765
if config.app
78-
result.select { |app, _| app_matches?(app, config.app, config.current) }
66+
result.select { |app, _| config.app_matches?(app, config.app, config.current) }
7967
else
80-
result.reject { |app, _| find_app_options(app).nil? }
68+
result.reject { |app, _| config.find_app_config(app).nil? }
8169
end
8270
end
8371

84-
def orgs # rubocop:disable Metrics/MethodLength
72+
def orgs
8573
result = []
8674

8775
if config.org
8876
result.push(config.org)
8977
else
90-
config.apps.each do |app_name, app_options|
91-
next if config.app && !app_matches?(config.app, app_name, app_options)
92-
78+
config.apps.each do |_, app_options|
9379
org = app_options[:cpln_org]
9480
result.push(org) if org && !result.include?(org)
9581
end
@@ -102,7 +88,7 @@ def apps(org)
10288
result = []
10389

10490
config.apps.each do |app_name, app_options|
105-
next if config.app && !app_matches?(config.app, app_name, app_options)
91+
next if config.app && !config.app_matches?(config.app, app_name, app_options)
10692

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

115101
def any_app_starts_with?(app)
116-
@app_workloads.keys.find { |app_name| app_matches?(app_name, app, config.apps[app.to_sym]) }
102+
@app_workloads.keys.find { |app_name| config.app_matches?(app_name, app, config.apps[app.to_sym]) }
117103
end
118104

119105
def check_any_app_starts_with(app)
@@ -184,8 +170,7 @@ def print_missing_apps_starting_with
184170
"(replace 'whatever' with whatever suffix you want):"
185171

186172
@missing_apps_starting_with.each do |app, _workloads|
187-
app_with_suffix = "#{app}#{app.end_with?('-') ? '' : '-'}whatever"
188-
puts " - `cpl setup-app -a #{app_with_suffix}`"
173+
puts " - `cpl setup-app -a #{app}-whatever`"
189174
end
190175
end
191176

@@ -197,7 +182,7 @@ def single_app_info
197182
@defined_workloads = find_workloads(config.app)
198183
@available_workloads = fetch_workloads(config.app)
199184

200-
workloads = (@defined_workloads + @available_workloads).uniq.sort
185+
workloads = (@defined_workloads + @available_workloads).uniq
201186
workloads.each do |workload|
202187
print_workload(config.app, workload)
203188
end
@@ -217,7 +202,7 @@ def multiple_apps_info # rubocop:disable Metrics/MethodLength
217202
@defined_workloads = find_workloads(app)
218203
@available_workloads = @app_workloads[app] || []
219204

220-
workloads = (@defined_workloads + @available_workloads).uniq.sort
205+
workloads = (@defined_workloads + @available_workloads).uniq
221206
workloads.each do |workload|
222207
print_workload(app, workload)
223208
end

lib/core/config.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ def current
102102
end
103103
end
104104

105+
def app_matches?(app_name1, app_name2, app_options)
106+
app_name1 && app_name2 &&
107+
(app_name1.to_s == app_name2.to_s ||
108+
(app_options[:match_if_app_name_starts_with] && app_name1.to_s.start_with?(app_name2.to_s))
109+
)
110+
end
111+
105112
def find_app_config(app_name1)
106113
@app_configs ||= {}
107114
@app_configs[app_name1] ||= apps.find do |app_name2, app_config|
@@ -127,13 +134,6 @@ def ensure_config_app!(app_name, app_options)
127134
raise "Can't find config for app '#{app_name}' in 'controlplane.yml'." unless app_options
128135
end
129136

130-
def app_matches?(app_name1, app_name2, app_options)
131-
app_name1 && app_name2 &&
132-
(app_name1.to_s == app_name2.to_s ||
133-
(app_options[:match_if_app_name_starts_with] && app_name1.to_s.start_with?(app_name2.to_s))
134-
)
135-
end
136-
137137
def ensure_app!
138138
return if app
139139

0 commit comments

Comments
 (0)