2023-10-22 21:23:56 +00:00
|
|
|
|
2023-10-24 12:49:46 +00:00
|
|
|
require "./filesystems/gocryptfs"
|
2023-10-22 21:23:56 +00:00
|
|
|
|
|
|
|
module GX
|
2023-10-23 21:11:12 +00:00
|
|
|
class Config
|
|
|
|
enum Mode
|
|
|
|
Add
|
|
|
|
Edit
|
|
|
|
Run
|
|
|
|
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 12:49:46 +00:00
|
|
|
getter filesystems : Array(GoCryptFS)
|
2023-10-23 21:11:12 +00:00
|
|
|
getter home_dir : String
|
|
|
|
property mode : Mode
|
|
|
|
property path : String
|
|
|
|
property args : AddArgs.class | DelArgs.class | NoArgs.class
|
2023-10-22 21:23:56 +00:00
|
|
|
|
2023-10-24 12:49:46 +00:00
|
|
|
DEFAULT_CONFIG_PATH = "mfm.yml"
|
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"]
|
|
|
|
|
|
|
|
@mode = Mode::Run
|
2023-10-24 12:49:46 +00:00
|
|
|
@filesystems = [] of GoCryptFS
|
2023-10-23 21:11:12 +00:00
|
|
|
@path = File.join(@home_dir, ".config", DEFAULT_CONFIG_PATH)
|
|
|
|
@args = NoArgs
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_from_file
|
2023-10-24 12:49:46 +00:00
|
|
|
@filesystems = [] of GoCryptFS
|
2023-10-23 21:11:12 +00:00
|
|
|
|
|
|
|
if !File.exists? @path
|
|
|
|
STDERR.puts "Error: file #{@path} does not exist!".colorize(:red)
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
load_vaults(@path)
|
|
|
|
end
|
|
|
|
|
|
|
|
private def load_vaults(config_path : String)
|
|
|
|
yaml_data = YAML.parse(File.read(config_path))
|
2023-10-24 12:49:46 +00:00
|
|
|
vaults_data = yaml_data["filesystems"].as_a
|
2023-10-23 21:11:12 +00:00
|
|
|
|
|
|
|
vaults_data.each do |vault_data|
|
2023-10-24 12:49:46 +00:00
|
|
|
type = vault_data["type"].as_s
|
2023-10-23 21:11:12 +00:00
|
|
|
name = vault_data["name"].as_s
|
|
|
|
encrypted_path = vault_data["encrypted_path"].as_s
|
2023-10-24 12:49:46 +00:00
|
|
|
@filesystems << GoCryptFS.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
|