fix: rename to mfm and fix close

This commit is contained in:
Glenn Y. Rolland 2023-10-24 12:50:01 +02:00
parent 8def1979c1
commit 294231cfbb
3 changed files with 53 additions and 38 deletions

View file

@ -1,8 +1,9 @@
name: gx-vault ---
name: Minimalist FUSE Manager
version: 0.1.0 version: 0.1.0
targets: targets:
gx-vault: mfm:
main: src/main.cr main: src/main.cr
# authors: # authors:
@ -11,6 +12,10 @@ targets:
# description: | # description: |
# Short description of gx-vault # Short description of gx-vault
dependencies:
shellwords:
github: szTheory/shellwords-crystal
# dependencies: # dependencies:
# pg: # pg:
# github: will/crystal-pg # github: will/crystal-pg

View file

@ -66,19 +66,26 @@ module GX
def run() def run()
@config.load_from_file @config.load_from_file
# Correcting the fzf interaction part
names_display = @config.vaults.map do |vault| names_display = {} of String => NamedTuple(vault: Vault, ansi_name: String)
vault.mounted? ? "#{vault.name} [#{ "open".colorize(:green) }]" : vault.name @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[result_name] = {
vault: vault,
ansi_name: ansi_name
}
end end
selected_vault_name = Fzf.run(names_display.sort) result_vault_name = Fzf.run(names_display.values.map(&.[:ansi_name]).sort)
puts ">> #{selected_vault_name}".colorize(:yellow) selected_vault = names_display[result_vault_name][:vault]
selected_vault = @config.vaults.find { |v| v.name == selected_vault_name } puts ">> #{selected_vault.name}".colorize(:yellow)
if selected_vault if selected_vault
selected_vault.mounted? ? selected_vault.unmount : selected_vault.mount selected_vault.mounted? ? selected_vault.unmount : selected_vault.mount
else else
STDERR.puts "Vault not found.".colorize(:red) STDERR.puts "Vault not found: #{selected_vault}.".colorize(:red)
end end
end end

View file

@ -1,38 +1,41 @@
require "shellwords"
module GX module GX
class Vault class Vault
getter name : String getter name : String
getter encrypted_path : String getter encrypted_path : String
getter mount_dir : String getter mount_dir : String
def initialize(@name, @encrypted_path, mount_name : String) def initialize(@name, @encrypted_path, mount_name : String)
home_dir = ENV["HOME"] || raise "Home directory not found" home_dir = ENV["HOME"] || raise "Home directory not found"
@mount_dir = File.join(home_dir, "mnt/#{mount_name}") @mount_dir = File.join(home_dir, "mnt/#{mount_name}")
end end
def mounted? : Bool def mounted? : Bool
`mount`.includes?("#{encrypted_path} on #{mount_dir}") `mount`.includes?("#{encrypted_path} on #{mount_dir}")
end end
def mount def mount
Dir.mkdir_p(mount_dir) unless Dir.exists?(mount_dir) Dir.mkdir_p(mount_dir) unless Dir.exists?(mount_dir)
if mounted? if mounted?
puts "Already mounted. Skipping.".colorize(:yellow) puts "Already mounted. Skipping.".colorize(:yellow)
else else
input = STDIN input = STDIN
output = STDOUT output = STDOUT
error = STDERR error = STDERR
process = Process.new("gocryptfs", ["-idle", "15m", encrypted_path, mount_dir], input: input, output: output, error: error) process = Process.new("gocryptfs", ["-idle", "15m", encrypted_path, mount_dir], input: input, output: output, error: error)
unless process.wait.success? unless process.wait.success?
puts "Error mounting the vault".colorize(:red) puts "Error mounting the vault".colorize(:red)
return return
end
puts "Vault #{name} is now available on #{mount_dir}".colorize(:green)
end end
puts "Vault #{name} is now available on #{mount_dir}".colorize(:green) end
def unmount
system("fusermount -u #{mount_dir.shellescape}")
puts "Vault #{name} is now closed.".colorize(:green)
end end
end end
def unmount
`fusermount -u #{mount_dir}`
puts "Vault #{name} is now closed.".colorize(:green)
end
end
end end