2023-10-25 14:01:46 +02: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 23:23:56 +02:00
|
|
|
|
2023-11-18 19:56:03 +01:00
|
|
|
require "crinja"
|
|
|
|
|
2023-11-24 00:20:16 +01:00
|
|
|
require "./models"
|
2023-10-22 23:23:56 +02:00
|
|
|
|
|
|
|
module GX
|
2023-10-23 23:11:12 +02:00
|
|
|
class Config
|
2023-11-18 19:32:12 +01:00
|
|
|
Log = ::Log.for("config")
|
|
|
|
|
2023-10-23 23:11:12 +02:00
|
|
|
enum Mode
|
2023-11-02 12:44:37 +01:00
|
|
|
ConfigAdd
|
|
|
|
ConfigDelete
|
|
|
|
ConfigEdit
|
|
|
|
ShowVersion
|
|
|
|
Mount
|
2023-10-23 23:11:12 +02:00
|
|
|
end
|
2023-10-22 23:23:56 +02:00
|
|
|
|
2023-10-23 23:11:12 +02:00
|
|
|
record NoArgs
|
|
|
|
record AddArgs, name : String, path : String
|
|
|
|
record DelArgs, name : String
|
|
|
|
|
2023-11-24 00:20:16 +01:00
|
|
|
# getter filesystems : Array(Models::AbstractFilesystemConfig)
|
2023-10-23 23:11:12 +02:00
|
|
|
getter home_dir : String
|
2023-11-24 00:20:16 +01:00
|
|
|
getter root : Models::RootConfig?
|
|
|
|
|
2023-11-02 12:44:37 +01:00
|
|
|
property verbose : Bool
|
2023-10-23 23:11:12 +02:00
|
|
|
property mode : Mode
|
2023-11-18 23:31:04 +01:00
|
|
|
property path : String?
|
2023-10-23 23:11:12 +02:00
|
|
|
property args : AddArgs.class | DelArgs.class | NoArgs.class
|
2023-10-22 23:23:56 +02:00
|
|
|
|
2023-10-23 23:11:12 +02:00
|
|
|
def initialize()
|
|
|
|
if !ENV["HOME"]?
|
|
|
|
raise "Home directory not found"
|
|
|
|
end
|
|
|
|
@home_dir = ENV["HOME"]
|
|
|
|
|
2023-11-02 12:44:37 +01:00
|
|
|
@verbose = false
|
|
|
|
@mode = Mode::Mount
|
2023-11-24 00:20:16 +01:00
|
|
|
@filesystems = [] of Models::AbstractFilesystemConfig
|
2023-11-18 23:31:04 +01:00
|
|
|
@path = nil
|
2023-11-18 19:32:12 +01:00
|
|
|
|
2023-10-23 23:11:12 +02:00
|
|
|
@args = NoArgs
|
|
|
|
end
|
|
|
|
|
2023-11-24 00:20:16 +01:00
|
|
|
private def detect_config_file()
|
2023-11-18 19:32:12 +01:00
|
|
|
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
|
|
|
|
|
2023-10-23 23:11:12 +02:00
|
|
|
def load_from_file
|
2023-11-24 00:20:16 +01:00
|
|
|
config_path = @path
|
|
|
|
if config_path.nil?
|
|
|
|
config_path = detect_config_file()
|
2023-11-18 23:31:04 +01:00
|
|
|
end
|
2023-11-24 00:20:16 +01:00
|
|
|
@path = config_path
|
2023-10-23 23:11:12 +02:00
|
|
|
|
2023-11-24 00:20:16 +01:00
|
|
|
if !File.exists? config_path
|
2023-11-18 23:31:04 +01:00
|
|
|
Log.error { "File #{path} does not exist!".colorize(:red) }
|
2023-10-23 23:11:12 +02:00
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
|
2023-11-18 19:56:03 +01:00
|
|
|
file_data = File.read(config_path)
|
|
|
|
file_patched = Crinja.render(file_data, {"env" => ENV.to_h})
|
|
|
|
|
2023-11-24 00:20:16 +01:00
|
|
|
root = Models::RootConfig.from_yaml(file_patched)
|
2023-10-23 23:11:12 +02:00
|
|
|
|
2023-11-24 00:20:16 +01:00
|
|
|
global_mount_point = root.global.mount_point
|
|
|
|
raise "Invalid global mount point" if global_mount_point.nil?
|
2023-11-21 00:29:48 +01:00
|
|
|
|
2023-11-24 00:20:16 +01:00
|
|
|
root.filesystems.each do |selected_filesystem|
|
|
|
|
if !selected_filesystem.mount_point?
|
|
|
|
selected_filesystem.mount_point =
|
|
|
|
File.join(global_mount_point, selected_filesystem.mounted_name)
|
2023-11-21 00:29:48 +01:00
|
|
|
end
|
|
|
|
end
|
2023-11-24 00:20:16 +01:00
|
|
|
@root = root
|
2023-10-22 23:23:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|