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:
mountpoint: "/home/user/mnt/{{name}}"
fsmap:
filesystems:
- type: "gocryptfs"
name: "Work - SSH Keys"
encrypted_path: "/home/user/.ssh/keyring.work"

View file

@ -20,7 +20,7 @@ module GX
add_args = { name: "", path: "" }
delete_args = { name: "" }
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|
@config.path = path
@ -34,7 +34,7 @@ module GX
parser.on("create", "Create vault") do
@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.on("-n", "--name", "Set vault name") do |name|
@ -48,7 +48,7 @@ module GX
parser.on("delete", "Delete vault") do
@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.on("-n", "--name", "Set vault name") do |name|
@ -67,25 +67,25 @@ module GX
def run()
@config.load_from_file
names_display = {} of String => NamedTuple(vault: Vault, ansi_name: String)
@config.vaults.each do |vault|
result_name = vault.mounted? ? "#{vault.name} [open]" : vault.name
ansi_name = vault.mounted? ? "#{vault.name} [#{ "open".colorize(:green) }]" : vault.name
names_display = {} of String => NamedTuple(filesystem: GoCryptFS, ansi_name: String)
@config.filesystems.each do |filesystem|
result_name = filesystem.mounted? ? "#{filesystem.name} [open]" : filesystem.name
ansi_name = filesystem.mounted? ? "#{filesystem.name} [#{ "open".colorize(:green) }]" : filesystem.name
names_display[result_name] = {
vault: vault,
filesystem: filesystem,
ansi_name: ansi_name
}
end
result_vault_name = Fzf.run(names_display.values.map(&.[:ansi_name]).sort)
selected_vault = names_display[result_vault_name][:vault]
puts ">> #{selected_vault.name}".colorize(:yellow)
result_filesystem_name = Fzf.run(names_display.values.map(&.[:ansi_name]).sort)
selected_filesystem = names_display[result_filesystem_name][:filesystem]
puts ">> #{selected_filesystem.name}".colorize(:yellow)
if selected_vault
selected_vault.mounted? ? selected_vault.unmount : selected_vault.mount
if selected_filesystem
selected_filesystem.mounted? ? selected_filesystem.unmount : selected_filesystem.mount
else
STDERR.puts "Vault not found: #{selected_vault}.".colorize(:red)
STDERR.puts "Vault not found: #{selected_filesystem}.".colorize(:red)
end
end

View file

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

View file

@ -1,7 +1,7 @@
require "shellwords"
module GX
class Vault
class GoCryptFS
getter name : String
getter encrypted_path : String
getter mount_dir : String
@ -29,13 +29,13 @@ module GX
puts "Error mounting the vault".colorize(:red)
return
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
def unmount
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

View file

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