Class: Dip::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/dip/config.rb

Defined Under Namespace

Classes: ConfigFinder

Constant Summary collapse

DEFAULT_PATH =
"dip.yml"
CONFIG_DEFAULTS =
{
  environment: {},
  compose: {},
  kubectl: {},
  infra: {},
  interaction: {},
  provision: [],
  preflight: []
}.freeze
TOP_LEVEL_KEYS =
%i[environment compose kubectl infra interaction provision preflight].freeze
ConfigKeyMissingError =
Class.new(ArgumentError)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(work_dir = Dir.pwd) ⇒ Config

Returns a new instance of Config.



91
92
93
# File 'lib/dip/config.rb', line 91

def initialize(work_dir = Dir.pwd)
  @work_dir = work_dir
end

Class Method Details

.load_yaml(file_path = path) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dip/config.rb', line 72

def load_yaml(file_path = path)
  return {} unless File.exist?(file_path)

  data = if Gem::Version.new(Psych::VERSION) >= Gem::Version.new("4.0.0")
    YAML.safe_load(
      ERB.new(File.read(file_path)).result,
      aliases: true
    )
  else
    YAML.safe_load(
      ERB.new(File.read(file_path)).result,
      [], [], true
    )
  end

  data&.deep_symbolize_keys! || {}
end

Instance Method Details

#exist?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/dip/config.rb', line 103

def exist?
  finder.exist?
end

#file_pathObject



95
96
97
# File 'lib/dip/config.rb', line 95

def file_path
  finder.file_path
end

#module_file(filename) ⇒ Object



99
100
101
# File 'lib/dip/config.rb', line 99

def module_file(filename)
  finder.modules_dir / "#{filename}.yml"
end

#to_hObject



107
108
109
# File 'lib/dip/config.rb', line 107

def to_h
  config
end

#validateObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/dip/config.rb', line 117

def validate
  raise Dip::Error, "Config file path is not set" if file_path.nil?
  raise Dip::Error, "Config file not found: #{file_path}" unless File.exist?(file_path)

  schema_path = File.join(File.dirname(__FILE__), "../../schema.json")
  raise Dip::Error, "Schema file not found: #{schema_path}" unless File.exist?(schema_path)

  data = self.class.load_yaml(file_path)
  schema = JSON::Validator.parse(File.read(schema_path))
  JSON::Validator.validate!(schema, data)
rescue Psych::SyntaxError => e
  raise Dip::Error, "Invalid YAML syntax in config file: #{e.message}"
rescue JSON::Schema::ValidationError => e
  data_display = data ? data.to_yaml.gsub("\n", "\n  ") : "nil"
  error_message = "Schema validation failed: #{e.message}\nInput data:\n  #{data_display}"
  raise Dip::Error, error_message
rescue JSON::Schema::JsonParseError => e
  raise Dip::Error, "Error parsing schema file: #{e.message}"
end