From 294231cfbbcb0067e36dcbcced6d65395dd052db Mon Sep 17 00:00:00 2001 From: Glenn Date: Tue, 24 Oct 2023 12:50:01 +0200 Subject: [PATCH] fix: rename to mfm and fix close --- shard.yml | 9 ++++++-- src/cli.cr | 21 ++++++++++++------ src/vault.cr | 61 +++++++++++++++++++++++++++------------------------- 3 files changed, 53 insertions(+), 38 deletions(-) diff --git a/shard.yml b/shard.yml index ee8a586..bb2986a 100644 --- a/shard.yml +++ b/shard.yml @@ -1,8 +1,9 @@ -name: gx-vault +--- +name: Minimalist FUSE Manager version: 0.1.0 targets: - gx-vault: + mfm: main: src/main.cr # authors: @@ -11,6 +12,10 @@ targets: # description: | # Short description of gx-vault +dependencies: + shellwords: + github: szTheory/shellwords-crystal + # dependencies: # pg: # github: will/crystal-pg diff --git a/src/cli.cr b/src/cli.cr index cf4b0b9..f903f1f 100644 --- a/src/cli.cr +++ b/src/cli.cr @@ -66,19 +66,26 @@ module GX def run() @config.load_from_file - # Correcting the fzf interaction part - names_display = @config.vaults.map do |vault| - vault.mounted? ? "#{vault.name} [#{ "open".colorize(:green) }]" : vault.name + + 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[result_name] = { + vault: vault, + ansi_name: ansi_name + } end - selected_vault_name = Fzf.run(names_display.sort) - puts ">> #{selected_vault_name}".colorize(:yellow) - selected_vault = @config.vaults.find { |v| v.name == selected_vault_name } + 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) if selected_vault selected_vault.mounted? ? selected_vault.unmount : selected_vault.mount else - STDERR.puts "Vault not found.".colorize(:red) + STDERR.puts "Vault not found: #{selected_vault}.".colorize(:red) end end diff --git a/src/vault.cr b/src/vault.cr index 36ddd94..31c9807 100644 --- a/src/vault.cr +++ b/src/vault.cr @@ -1,38 +1,41 @@ +require "shellwords" + module GX -class Vault - getter name : String - getter encrypted_path : String - getter mount_dir : String + class Vault + getter name : String + getter encrypted_path : String + getter mount_dir : String - def initialize(@name, @encrypted_path, mount_name : String) - home_dir = ENV["HOME"] || raise "Home directory not found" - @mount_dir = File.join(home_dir, "mnt/#{mount_name}") - end + def initialize(@name, @encrypted_path, mount_name : String) + home_dir = ENV["HOME"] || raise "Home directory not found" + @mount_dir = File.join(home_dir, "mnt/#{mount_name}") + end - def mounted? : Bool - `mount`.includes?("#{encrypted_path} on #{mount_dir}") - end + def mounted? : Bool + `mount`.includes?("#{encrypted_path} on #{mount_dir}") + end - def mount - Dir.mkdir_p(mount_dir) unless Dir.exists?(mount_dir) + def mount + Dir.mkdir_p(mount_dir) unless Dir.exists?(mount_dir) - if mounted? - puts "Already mounted. Skipping.".colorize(:yellow) - else - input = STDIN - output = STDOUT - error = STDERR - process = Process.new("gocryptfs", ["-idle", "15m", encrypted_path, mount_dir], input: input, output: output, error: error) - unless process.wait.success? - puts "Error mounting the vault".colorize(:red) - return + if mounted? + puts "Already mounted. Skipping.".colorize(:yellow) + else + input = STDIN + output = STDOUT + error = STDERR + process = Process.new("gocryptfs", ["-idle", "15m", encrypted_path, mount_dir], input: input, output: output, error: error) + unless process.wait.success? + puts "Error mounting the vault".colorize(:red) + return + end + puts "Vault #{name} is now available on #{mount_dir}".colorize(:green) 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 - def unmount - `fusermount -u #{mount_dir}` - puts "Vault #{name} is now closed.".colorize(:green) - end -end end