Class: Config

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/core/config.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

CONFIG_FILE_LOCATION =
".controlplane/controlplane.yml"
REQUIRED_SHARED_SECRET_GRANT_KEYS =
%i[name secret_name policy_name].freeze
SHARED_SECRET_RESOURCE_NAME_KEYS =
%i[secret_name policy_name].freeze
CONTROL_PLANE_RESOURCE_NAME_REGEX =
/\A[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

normalize_command_name, normalize_option_name, random_four_digits, strip_str_and_validate

Constructor Details

#initialize(args, options, required_options) ⇒ Config

Returns a new instance of Config.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/core/config.rb', line 17

def initialize(args, options, required_options)
  @args = args
  @options = options
  @required_options = required_options

  ensure_required_options!

  warn_deprecated_options

  Shell.verbose_mode(options[:verbose])
  trace_mode = options[:trace]
  return unless trace_mode

  ControlplaneApiDirect.trace = trace_mode
  Shell.warn("Trace mode is enabled. Sensitive data is redacted, but please review output before sharing.")
end

Instance Attribute Details

#app_comes_from_envObject (readonly)

Returns the value of attribute app_comes_from_env.



6
7
8
# File 'lib/core/config.rb', line 6

def app_comes_from_env
  @app_comes_from_env
end

#argsObject (readonly)

Returns the value of attribute args.



6
7
8
# File 'lib/core/config.rb', line 6

def args
  @args
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/core/config.rb', line 6

def options
  @options
end

#org_comes_from_envObject (readonly)

Returns the value of attribute org_comes_from_env.



6
7
8
# File 'lib/core/config.rb', line 6

def org_comes_from_env
  @org_comes_from_env
end

#required_optionsObject (readonly)

Returns the value of attribute required_options.



6
7
8
# File 'lib/core/config.rb', line 6

def required_options
  @required_options
end

Instance Method Details

#[](key) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/core/config.rb', line 88

def [](key)
  ensure_current_config!

  raise "Can't find option '#{key}' for app '#{app}' in 'controlplane.yml'." unless current.key?(key)

  current.fetch(key)
end

#appObject



38
39
40
# File 'lib/core/config.rb', line 38

def app
  @app ||= load_app_from_options || load_app_from_env
end

#app_cpln_dirObject



100
101
102
# File 'lib/core/config.rb', line 100

def app_cpln_dir
  "#{app_dir}/.controlplane"
end

#app_dirObject



108
109
110
# File 'lib/core/config.rb', line 108

def app_dir
  Pathname.new(config_file_path).parent.parent.to_s
end

#app_matches?(app_name1, app_name2, app_options) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
146
147
148
# File 'lib/core/config.rb', line 143

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

#app_prefixObject



42
43
44
# File 'lib/core/config.rb', line 42

def app_prefix
  current&.fetch(:name)
end

#appsObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/core/config.rb', line 122

def apps
  @apps ||= config[:apps].to_h do |app_name, app_options|
    ensure_config_app!(app_name, app_options)

    check_deprecated_options(app_options)

    app_options_with_new_keys = app_options.to_h do |key, value|
      new_key = new_option_keys[key]
      new_key ? [new_key, value] : [key, value]
    end

    [app_name, app_options_with_new_keys]
  end
end

#configObject



112
113
114
115
116
117
118
119
120
# File 'lib/core/config.rb', line 112

def config
  @config ||= begin
    global_config = YAML.safe_load_file(config_file_path, symbolize_names: true, aliases: true)
    ensure_config!(global_config)
    ensure_config_apps!(global_config)

    global_config
  end
end

#currentObject



137
138
139
140
141
# File 'lib/core/config.rb', line 137

def current
  return unless app

  @current ||= find_app_config(app)
end

#domainObject



84
85
86
# File 'lib/core/config.rb', line 84

def domain
  @domain ||= load_domain_from_options || load_domain_from_file
end

#find_app_config(app_name1) ⇒ Object



150
151
152
153
154
155
156
157
158
159
# File 'lib/core/config.rb', line 150

def find_app_config(app_name1)
  @app_configs ||= {}

  @app_configs[app_name1] ||= apps.filter_map do |app_name2, app_config|
                                next unless app_matches?(app_name1, app_name2, app_config)

                                app_config[:name] = app_name2
                                app_config
                              end&.last
end

#identityObject



46
47
48
# File 'lib/core/config.rb', line 46

def identity
  "#{app}-identity"
end


50
51
52
# File 'lib/core/config.rb', line 50

def identity_link
  "/org/#{org}/gvc/#{app}/identity/#{identity}"
end


80
81
82
# File 'lib/core/config.rb', line 80

def image_link(image)
  "/org/#{org}/image/#{image}"
end

#locationObject



72
73
74
# File 'lib/core/config.rb', line 72

def location
  @location ||= load_location_from_options || load_location_from_env || load_location_from_file
end


76
77
78
# File 'lib/core/config.rb', line 76

def location_link
  "/org/#{org}/location/#{location}"
end

#orgObject



34
35
36
# File 'lib/core/config.rb', line 34

def org
  @org ||= load_org_from_options || load_org_from_env || load_org_from_file
end

#script_pathObject



96
97
98
# File 'lib/core/config.rb', line 96

def script_path
  Pathname.new(__dir__).parent.parent
end

#secretsObject



54
55
56
# File 'lib/core/config.rb', line 54

def secrets
  current&.dig(:secrets_name) || "#{app_prefix}-secrets"
end

#secrets_policyObject



58
59
60
# File 'lib/core/config.rb', line 58

def secrets_policy
  current&.dig(:secrets_policy_name) || "#{secrets}-policy"
end

#shared_secret_grantsObject



62
63
64
# File 'lib/core/config.rb', line 62

def shared_secret_grants
  @shared_secret_grants ||= normalize_shared_secret_grants(current&.dig(:shared_secret_grants))
end

#shared_secret_placeholdersObject



66
67
68
69
70
# File 'lib/core/config.rb', line 66

def shared_secret_placeholders
  shared_secret_grants.to_h do |grant|
    ["{{SHARED_SECRET_#{grant.fetch(:name).upcase}}}", grant.fetch(:secret_name)]
  end
end

#should_app_start_with?(app_name) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/core/config.rb', line 104

def should_app_start_with?(app_name)
  apps[app_name.to_sym]&.dig(:match_if_app_name_starts_with) || false
end

#use_digest_image_ref?Boolean

Returns:

  • (Boolean)


161
162
163
164
165
166
167
# File 'lib/core/config.rb', line 161

def use_digest_image_ref?
  # Three-state: --use-digest-image-ref → true, --no-use-digest-image-ref → false
  # (both short-circuit YAML), absent → nil (fall through to YAML).
  return options[:use_digest_image_ref] unless options[:use_digest_image_ref].nil?

  current&.dig(:use_digest_image_ref) == true
end