refactor: remove all mentions of vaults, make it generic for fs

This commit is contained in:
Glenn Y. Rolland 2023-10-24 14:49:46 +02:00
parent cc458b3af5
commit 002db7026b
5 changed files with 27 additions and 26 deletions

View file

@ -60,7 +60,7 @@ version: "1"
global: global:
mountpoint: "/home/user/mnt/{{name}}" mountpoint: "/home/user/mnt/{{name}}"
fsmap: filesystems:
- type: "gocryptfs" - type: "gocryptfs"
name: "Work - SSH Keys" name: "Work - SSH Keys"
encrypted_path: "/home/user/.ssh/keyring.work" encrypted_path: "/home/user/.ssh/keyring.work"

View file

@ -20,7 +20,7 @@ module GX
add_args = { name: "", path: "" } add_args = { name: "", path: "" }
delete_args = { name: "" } delete_args = { name: "" }
pparser = OptionParser.new do |parser| pparser = OptionParser.new do |parser|
parser.banner = "Usage: gx-vault [options]\n\nGlobal options" parser.banner = "Usage: #{PROGRAM_NAME} [options]\n\nGlobal options"
parser.on("-c", "--config FILE", "Set configuration file") do |path| parser.on("-c", "--config FILE", "Set configuration file") do |path|
@config.path = path @config.path = path
@ -34,7 +34,7 @@ module GX
parser.on("create", "Create vault") do parser.on("create", "Create vault") do
@config.mode = Config::Mode::Add @config.mode = Config::Mode::Add
parser.banner = "Usage: gx-vault create [options]\n\nGlobal options" parser.banner = "Usage: #{PROGRAM_NAME} create [options]\n\nGlobal options"
parser.separator("\nCommand options") parser.separator("\nCommand options")
parser.on("-n", "--name", "Set vault name") do |name| parser.on("-n", "--name", "Set vault name") do |name|
@ -48,7 +48,7 @@ module GX
parser.on("delete", "Delete vault") do parser.on("delete", "Delete vault") do
@config.mode = Config::Mode::Add @config.mode = Config::Mode::Add
parser.banner = "Usage: gx-vault delete [options]\n\nGlobal options" parser.banner = "Usage: #{PROGRAM_NAME} delete [options]\n\nGlobal options"
parser.separator("\nCommand options") parser.separator("\nCommand options")
parser.on("-n", "--name", "Set vault name") do |name| parser.on("-n", "--name", "Set vault name") do |name|
@ -67,25 +67,25 @@ module GX
def run() def run()
@config.load_from_file @config.load_from_file
names_display = {} of String => NamedTuple(vault: Vault, ansi_name: String) names_display = {} of String => NamedTuple(filesystem: GoCryptFS, ansi_name: String)
@config.vaults.each do |vault| @config.filesystems.each do |filesystem|
result_name = vault.mounted? ? "#{vault.name} [open]" : vault.name result_name = filesystem.mounted? ? "#{filesystem.name} [open]" : filesystem.name
ansi_name = vault.mounted? ? "#{vault.name} [#{ "open".colorize(:green) }]" : vault.name ansi_name = filesystem.mounted? ? "#{filesystem.name} [#{ "open".colorize(:green) }]" : filesystem.name
names_display[result_name] = { names_display[result_name] = {
vault: vault, filesystem: filesystem,
ansi_name: ansi_name ansi_name: ansi_name
} }
end end
result_vault_name = Fzf.run(names_display.values.map(&.[:ansi_name]).sort) result_filesystem_name = Fzf.run(names_display.values.map(&.[:ansi_name]).sort)
selected_vault = names_display[result_vault_name][:vault] selected_filesystem = names_display[result_filesystem_name][:filesystem]
puts ">> #{selected_vault.name}".colorize(:yellow) puts ">> #{selected_filesystem.name}".colorize(:yellow)
if selected_vault if selected_filesystem
selected_vault.mounted? ? selected_vault.unmount : selected_vault.mount selected_filesystem.mounted? ? selected_filesystem.unmount : selected_filesystem.mount
else else
STDERR.puts "Vault not found: #{selected_vault}.".colorize(:red) STDERR.puts "Vault not found: #{selected_filesystem}.".colorize(:red)
end end
end end

View file

@ -1,5 +1,5 @@
require "./vault" require "./filesystems/gocryptfs"
module GX module GX
class Config class Config
@ -13,13 +13,13 @@ module GX
record AddArgs, name : String, path : String record AddArgs, name : String, path : String
record DelArgs, name : String record DelArgs, name : String
getter vaults : Array(Vault) getter filesystems : Array(GoCryptFS)
getter home_dir : String getter home_dir : String
property mode : Mode property mode : Mode
property path : String property path : String
property args : AddArgs.class | DelArgs.class | NoArgs.class property args : AddArgs.class | DelArgs.class | NoArgs.class
DEFAULT_CONFIG_PATH = "gx-vault.yml" DEFAULT_CONFIG_PATH = "mfm.yml"
def initialize() def initialize()
if !ENV["HOME"]? if !ENV["HOME"]?
@ -28,13 +28,13 @@ module GX
@home_dir = ENV["HOME"] @home_dir = ENV["HOME"]
@mode = Mode::Run @mode = Mode::Run
@vaults = [] of Vault @filesystems = [] of GoCryptFS
@path = File.join(@home_dir, ".config", DEFAULT_CONFIG_PATH) @path = File.join(@home_dir, ".config", DEFAULT_CONFIG_PATH)
@args = NoArgs @args = NoArgs
end end
def load_from_file def load_from_file
@vaults = [] of Vault @filesystems = [] of GoCryptFS
if !File.exists? @path if !File.exists? @path
STDERR.puts "Error: file #{@path} does not exist!".colorize(:red) STDERR.puts "Error: file #{@path} does not exist!".colorize(:red)
@ -45,12 +45,13 @@ module GX
private def load_vaults(config_path : String) private def load_vaults(config_path : String)
yaml_data = YAML.parse(File.read(config_path)) yaml_data = YAML.parse(File.read(config_path))
vaults_data = yaml_data["vaults"].as_a vaults_data = yaml_data["filesystems"].as_a
vaults_data.each do |vault_data| vaults_data.each do |vault_data|
type = vault_data["type"].as_s
name = vault_data["name"].as_s name = vault_data["name"].as_s
encrypted_path = vault_data["encrypted_path"].as_s encrypted_path = vault_data["encrypted_path"].as_s
@vaults << Vault.new(name, encrypted_path, "#{name}.Open") @filesystems << GoCryptFS.new(name, encrypted_path, "#{name}.Open")
end end
end end
end end

View file

@ -1,7 +1,7 @@
require "shellwords" require "shellwords"
module GX module GX
class Vault class GoCryptFS
getter name : String getter name : String
getter encrypted_path : String getter encrypted_path : String
getter mount_dir : String getter mount_dir : String
@ -29,13 +29,13 @@ module GX
puts "Error mounting the vault".colorize(:red) puts "Error mounting the vault".colorize(:red)
return return
end end
puts "Vault #{name} is now available on #{mount_dir}".colorize(:green) puts "GoCryptFS #{name} is now available on #{mount_dir}".colorize(:green)
end end
end end
def unmount def unmount
system("fusermount -u #{mount_dir.shellescape}") system("fusermount -u #{mount_dir.shellescape}")
puts "Vault #{name} is now closed.".colorize(:green) puts "GoCryptFS #{name} is now closed.".colorize(:green)
end end
end end
end end

View file

@ -2,7 +2,7 @@ require "yaml"
require "colorize" require "colorize"
require "json" require "json"
require "./vault" require "./filesystems/gocryptfs"
require "./config" require "./config"
require "./cli" require "./cli"