Skip to content

Commit

Permalink
feat: allow setting org with CPLN_ORG env var
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelgomesxyz committed Oct 19, 2023
1 parent d71ab14 commit 32fd57c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
5 changes: 5 additions & 0 deletions lib/command/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class Config < Base
EX

def call # rubocop:disable Metrics/MethodLength
if config.org_comes_from_env
puts Shell.color("Org comes from CPLN_ORG env var", :yellow)
puts
end

if config.app
puts "#{Shell.color("Current config (app '#{config.app}')", :blue)}:"
puts pretty_print(config.current)
Expand Down
7 changes: 5 additions & 2 deletions lib/command/copy_image_from_upstream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def call # rubocop:disable Metrics/MethodLength
ensure_docker_running!

@upstream = config[:upstream]
@upstream_org = config.apps[@upstream.to_sym][:cpln_org]
@upstream_org = config.apps[@upstream.to_sym][:cpln_org] || ENV.fetch("CPLN_ORG_UPSTREAM", nil)
ensure_upstream_org!

create_upstream_profile
Expand All @@ -51,7 +51,10 @@ def ensure_docker_running!
end

def ensure_upstream_org!
raise "Can't find option 'cpln_org' for app '#{@upstream}' in 'controlplane.yml'." unless @upstream_org
return if @upstream_org

raise "Can't find option 'cpln_org' for app '#{@upstream}' in 'controlplane.yml', " \
"and CPLN_ORG_UPSTREAM env var is not set."
end

def create_upstream_profile
Expand Down
7 changes: 5 additions & 2 deletions lib/command/info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,20 @@ def fetch_app_workloads(org) # rubocop:disable Metrics/MethodLength
end
end

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

if config.options[:org]
result.push(config.options[: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)

org = app_options[:cpln_org]
result.push(org) unless result.include?(org)
result.push(org) if org && !result.include?(org)
end
end

Expand Down
21 changes: 18 additions & 3 deletions lib/core/config.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

class Config
class Config # rubocop:disable Metrics/ClassLength
attr_reader :config, :current,
:org, :app, :apps, :app_dir,
:org, :org_comes_from_env, :app, :apps, :app_dir,
# command line options
:args, :options

Expand All @@ -12,6 +12,7 @@ def initialize(args, options)
@args = args
@options = options
@org = options[:org]
@org_comes_from_env = false
@app = options[:app]

load_app_config
Expand Down Expand Up @@ -48,6 +49,13 @@ def ensure_current_config_app!(app_name)
raise "Can't find app '#{app_name}' in 'controlplane.yml'." unless current
end

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."
end

def ensure_config!
raise "'controlplane.yml' is empty." unless config
end
Expand All @@ -66,8 +74,15 @@ def app_matches_current?(app_name, app_options)

def pick_current_config(app_name, app_options)
@current = app_options
@org = self[:cpln_org]
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
ensure_current_config_org!(app_name)
end

def load_apps # rubocop:disable Metrics/MethodLength
Expand Down

0 comments on commit 32fd57c

Please sign in to comment.