mfm/src/config.cr

102 lines
2.7 KiB
Crystal
Raw Normal View History

2023-10-25 12:01:46 +00:00
# SPDX-License-Identifier: GPL-3.0-or-later
#
# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland <glenux@glenux.net>
# Copyright © 2023 Glenn Y. Rolland <glenux@glenux.net>
2023-10-22 21:23:56 +00:00
require "crinja"
require "./models"
2023-10-22 21:23:56 +00:00
module GX
class Config
Log = ::Log.for("config")
enum Mode
ConfigAdd
ConfigDelete
ConfigEdit
ShowVersion
Mount
end
2023-10-22 21:23:56 +00:00
record NoArgs
record AddArgs, name : String, path : String
record DelArgs, name : String
# getter filesystems : Array(Models::AbstractFilesystemConfig)
getter home_dir : String
getter root : Models::RootConfig?
property verbose : Bool
property mode : Mode
2023-11-18 22:31:04 +00:00
property path : String?
property args : AddArgs.class | DelArgs.class | NoArgs.class
2023-10-22 21:23:56 +00:00
def initialize()
if !ENV["HOME"]?
raise "Home directory not found"
end
@home_dir = ENV["HOME"]
@verbose = false
@mode = Mode::Mount
@filesystems = [] of Models::AbstractFilesystemConfig
2023-11-18 22:31:04 +00:00
@path = nil
@args = NoArgs
end
private def detect_config_file()
possible_files = [
File.join(@home_dir, ".config", "mfm", "config.yaml"),
File.join(@home_dir, ".config", "mfm", "config.yml"),
File.join(@home_dir, ".config", "mfm.yaml"),
File.join(@home_dir, ".config", "mfm.yml"),
File.join("/etc", "mfm", "config.yaml"),
File.join("/etc", "mfm", "config.yml"),
]
possible_files.each do |file_path|
if File.exists?(file_path)
Log.info { "Configuration file found: #{file_path}" }
return file_path if File.exists?(file_path)
else
Log.debug { "Configuration file not found: #{file_path}" }
end
end
Log.error { "No configuration file found in any of the standard locations" }
raise "Configuration file not found"
end
def load_from_file
config_path = @path
if config_path.nil?
config_path = detect_config_file()
2023-11-18 22:31:04 +00:00
end
@path = config_path
if !File.exists? config_path
2023-11-18 22:31:04 +00:00
Log.error { "File #{path} does not exist!".colorize(:red) }
exit(1)
end
file_data = File.read(config_path)
file_patched = Crinja.render(file_data, {"env" => ENV.to_h})
root = Models::RootConfig.from_yaml(file_patched)
global_mount_point = root.global.mount_point
raise "Invalid global mount point" if global_mount_point.nil?
root.filesystems.each do |selected_filesystem|
if !selected_filesystem.mount_point?
selected_filesystem.mount_point =
File.join(global_mount_point, selected_filesystem.mounted_name)
end
end
@root = root
2023-10-22 21:23:56 +00:00
end
end
end