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"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#random_four_digits, #strip_str_and_validate

Constructor Details

#initialize(args, options, required_options) ⇒ Config

Returns a new instance of Config.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/core/config.rb', line 14

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

  ensure_required_options!

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

  ControlplaneApiDirect.trace = trace_mode
  Shell.warn("Trace mode is enabled, this will print sensitive information to the console.")
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



49
50
51
52
53
54
55
# File 'lib/core/config.rb', line 49

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



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

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

#app_cpln_dirObject



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

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

#app_dirObject



69
70
71
# File 'lib/core/config.rb', line 69

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

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

Returns:

  • (Boolean)


109
110
111
112
113
114
# File 'lib/core/config.rb', line 109

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



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

def app_prefix
  current&.fetch(:name)
end

#appsObject



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/core/config.rb', line 83

def apps
  @apps ||= config[:apps].to_h do |app_name, app_options|
    ensure_config_app!(app_name, 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



73
74
75
76
77
78
79
80
81
# File 'lib/core/config.rb', line 73

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



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/core/config.rb', line 96

def current
  return unless app

  @current ||= begin
    app_config = find_app_config(app)
    ensure_config_app!(app, app_config)

    warn_deprecated_options(app_config)

    app_config
  end
end

#domainObject



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

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

#find_app_config(app_name1) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/core/config.rb', line 116

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

#locationObject



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

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

#orgObject



29
30
31
# File 'lib/core/config.rb', line 29

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

#script_pathObject



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

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

#should_app_start_with?(app_name) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/core/config.rb', line 65

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