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
|
|
|
|
2023-11-18 18:56:03 +00:00
|
|
|
require "crinja"
|
|
|
|
|
2023-10-24 13:53:28 +00:00
|
|
|
require "./filesystems"
|
2023-10-22 21:23:56 +00:00
|
|
|
|
|
|
|
module GX
|
2023-10-23 21:11:12 +00:00
|
|
|
class Config
|
2023-11-18 18:32:12 +00:00
|
|
|
Log = ::Log.for("config")
|
|
|
|
|
2023-10-23 21:11:12 +00:00
|
|
|
enum Mode
|
2023-11-02 11:44:37 +00:00
|
|
|
ConfigAdd
|
|
|
|
ConfigDelete
|
|
|
|
ConfigEdit
|
|
|
|
ShowVersion
|
|
|
|
Mount
|
2023-10-23 21:11:12 +00:00
|
|
|
end
|
2023-10-22 21:23:56 +00:00
|
|
|
|
2023-10-23 21:11:12 +00:00
|
|
|
record NoArgs
|
|
|
|
record AddArgs, name : String, path : String
|
|
|
|
record DelArgs, name : String
|
|
|
|
|
2023-10-24 13:53:28 +00:00
|
|
|
getter filesystems : Array(Filesystem)
|
2023-10-23 21:11:12 +00:00
|
|
|
getter home_dir : String
|
2023-11-20 23:29:48 +00:00
|
|
|
getter global_mount_point : String?
|
2023-11-02 11:44:37 +00:00
|
|
|
property verbose : Bool
|
2023-10-23 21:11:12 +00:00
|
|
|
property mode : Mode
|
2023-11-18 22:31:04 +00:00
|
|
|
property path : String?
|
2023-10-23 21:11:12 +00:00
|
|
|
property args : AddArgs.class | DelArgs.class | NoArgs.class
|
2023-10-22 21:23:56 +00:00
|
|
|
|
2023-10-23 21:11:12 +00:00
|
|
|
def initialize()
|
|
|
|
if !ENV["HOME"]?
|
|
|
|
raise "Home directory not found"
|
|
|
|
end
|
|
|
|
@home_dir = ENV["HOME"]
|
|
|
|
|
2023-11-02 11:44:37 +00:00
|
|
|
@verbose = false
|
|
|
|
@mode = Mode::Mount
|
2023-10-24 13:53:28 +00:00
|
|
|
@filesystems = [] of Filesystem
|
2023-11-18 22:31:04 +00:00
|
|
|
@path = nil
|
2023-11-20 23:29:48 +00:00
|
|
|
@global_mount_point = nil
|
2023-11-18 18:32:12 +00:00
|
|
|
|
2023-10-23 21:11:12 +00:00
|
|
|
@args = NoArgs
|
|
|
|
end
|
|
|
|
|
2023-11-18 18:32:12 +00:00
|
|
|
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
|
|
|
|
|
2023-10-23 21:11:12 +00:00
|
|
|
def load_from_file
|
2023-11-18 22:31:04 +00:00
|
|
|
path = @path
|
|
|
|
if path.nil?
|
|
|
|
path = detect_config_file()
|
|
|
|
end
|
|
|
|
@path = path
|
2023-10-24 13:53:28 +00:00
|
|
|
@filesystems = [] of Filesystem
|
2023-10-23 21:11:12 +00:00
|
|
|
|
2023-11-18 22:31:04 +00:00
|
|
|
if !File.exists? path
|
|
|
|
Log.error { "File #{path} does not exist!".colorize(:red) }
|
2023-10-23 21:11:12 +00:00
|
|
|
exit(1)
|
|
|
|
end
|
2023-11-18 22:31:04 +00:00
|
|
|
load_filesystems(path)
|
2023-10-23 21:11:12 +00:00
|
|
|
end
|
|
|
|
|
2023-11-20 23:29:48 +00:00
|
|
|
# FIXME: render template on a value basis (instead of global)
|
2023-10-24 13:53:28 +00:00
|
|
|
private def load_filesystems(config_path : String)
|
2023-11-20 23:29:48 +00:00
|
|
|
schema_version = nil
|
2023-11-18 18:56:03 +00:00
|
|
|
file_data = File.read(config_path)
|
|
|
|
file_patched = Crinja.render(file_data, {"env" => ENV.to_h})
|
|
|
|
|
|
|
|
yaml_data = YAML.parse(file_patched)
|
2023-10-23 21:11:12 +00:00
|
|
|
|
2023-11-20 23:29:48 +00:00
|
|
|
# Extract schema version
|
|
|
|
if yaml_data["version"]?
|
|
|
|
schema_version = yaml_data["version"].as_s?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Extract global settings
|
|
|
|
if yaml_data["global"]?.try &.as_h?
|
|
|
|
global_data = yaml_data["global"]
|
|
|
|
if global_data["mountpoint"]?
|
|
|
|
@global_mount_point = global_data["mountpoint"].as_s?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Extract filesystem data
|
|
|
|
vaults_data = yaml_data["filesystems"].as_a
|
2023-10-24 13:53:28 +00:00
|
|
|
vaults_data.each do |filesystem_data|
|
|
|
|
type = filesystem_data["type"].as_s
|
|
|
|
name = filesystem_data["name"].as_s
|
|
|
|
# encrypted_path = filesystem_data["encrypted_path"].as_s
|
|
|
|
@filesystems << Filesystem.from_yaml(filesystem_data.to_yaml)
|
|
|
|
# @filesystems << Filesystem.new(name, encrypted_path, "#{name}.Open")
|
2023-10-23 21:11:12 +00:00
|
|
|
end
|
2023-10-22 21:23:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|