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: []
}.freeze
TOP_LEVEL_KEYS =
%i[environment compose kubectl infra interaction provision].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.



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

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

Class Method Details

.load_yaml(file_path = path) ⇒ Object



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

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)


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

def exist?
  finder.exist?
end

#file_pathObject



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

def file_path
  finder.file_path
end

#module_file(filename) ⇒ Object



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

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

#to_hObject



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

def to_h
  config
end

#validateObject



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

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