forked from glenux/mfm
refactor: clean up & add module
This commit is contained in:
parent
d19602ffac
commit
d49a3d1261
5 changed files with 132 additions and 86 deletions
42
src/cli.cr
Normal file
42
src/cli.cr
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
|
||||||
|
require "./config"
|
||||||
|
require "./fzf"
|
||||||
|
|
||||||
|
module GX
|
||||||
|
class Cli
|
||||||
|
@home_dir : String
|
||||||
|
@config : Config
|
||||||
|
|
||||||
|
def initialize()
|
||||||
|
# Main execution starts here
|
||||||
|
if !ENV["HOME"]?
|
||||||
|
raise "Home directory not found"
|
||||||
|
end
|
||||||
|
@home_dir = ENV["HOME"]
|
||||||
|
@config = Config.new(File.join(@home_dir, ".config/gx-vault.yml"))
|
||||||
|
|
||||||
|
## FIXME: check that FZF is installed
|
||||||
|
end
|
||||||
|
|
||||||
|
def run()
|
||||||
|
# Correcting the fzf interaction part
|
||||||
|
names_display = @config.vaults.map do |vault|
|
||||||
|
vault.mounted? ? "#{vault.name} [#{ "open".colorize(:green) }]" : vault.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 }
|
||||||
|
|
||||||
|
if selected_vault
|
||||||
|
selected_vault.mounted? ? selected_vault.unmount : selected_vault.mount
|
||||||
|
else
|
||||||
|
STDERR.puts "Vault not found.".colorize(:red)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
24
src/config.cr
Normal file
24
src/config.cr
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
|
||||||
|
require "./vault"
|
||||||
|
|
||||||
|
module GX
|
||||||
|
class Config
|
||||||
|
getter vaults : Array(Vault)
|
||||||
|
|
||||||
|
def initialize(config_path : String)
|
||||||
|
@vaults = [] of Vault
|
||||||
|
load_vaults(config_path)
|
||||||
|
end
|
||||||
|
|
||||||
|
private def load_vaults(config_path : String)
|
||||||
|
yaml_data = YAML.parse(File.read(config_path))
|
||||||
|
vaults_data = yaml_data["vaults"].as_a
|
||||||
|
|
||||||
|
vaults_data.each do |vault_data|
|
||||||
|
name = vault_data["name"].as_s
|
||||||
|
encrypted_path = vault_data["encrypted_path"].as_s
|
||||||
|
@vaults << Vault.new(name, encrypted_path, "#{name}.Open")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
23
src/fzf.cr
Normal file
23
src/fzf.cr
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
|
||||||
|
module GX
|
||||||
|
class Fzf
|
||||||
|
|
||||||
|
def self.run(list : Array(String)) : String
|
||||||
|
input = IO::Memory.new
|
||||||
|
input.puts list.join("\n")
|
||||||
|
input.rewind
|
||||||
|
|
||||||
|
output = IO::Memory.new
|
||||||
|
error = STDERR
|
||||||
|
process = Process.new("fzf", ["--ansi"], input: input, output: output, error: error)
|
||||||
|
|
||||||
|
unless process.wait.success?
|
||||||
|
STDERR.puts "Error executing fzf: #{error.to_s.strip}".colorize(:red)
|
||||||
|
exit(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
result = output.to_s.strip #.split.first?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
91
src/main.cr
91
src/main.cr
|
@ -2,92 +2,11 @@ require "yaml"
|
||||||
require "colorize"
|
require "colorize"
|
||||||
require "json"
|
require "json"
|
||||||
|
|
||||||
# Vault class
|
require "./vault"
|
||||||
class Vault
|
require "./config"
|
||||||
getter name : String
|
require "./cli"
|
||||||
getter encrypted_path : String
|
|
||||||
getter mount_dir : String
|
|
||||||
|
|
||||||
def initialize(@name, @encrypted_path, mount_name : String)
|
app = GX::Cli.new
|
||||||
home_dir = ENV["HOME"] || raise "Home directory not found"
|
app.run
|
||||||
@mount_dir = File.join(home_dir, "mnt/#{mount_name}")
|
|
||||||
end
|
|
||||||
|
|
||||||
def mounted? : Bool
|
|
||||||
`mount`.includes?("#{encrypted_path} on #{mount_dir}")
|
|
||||||
end
|
|
||||||
|
|
||||||
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
|
|
||||||
end
|
|
||||||
puts "Vault #{name} is now available on #{mount_dir}".colorize(:green)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
def unmount
|
|
||||||
`fusermount -u #{mount_dir}`
|
|
||||||
puts "Vault #{name} is now closed.".colorize(:green)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Config class
|
|
||||||
class Config
|
|
||||||
getter vaults : Array(Vault)
|
|
||||||
|
|
||||||
def initialize(config_path : String)
|
|
||||||
@vaults = [] of Vault
|
|
||||||
load_vaults(config_path)
|
|
||||||
end
|
|
||||||
|
|
||||||
private def load_vaults(config_path : String)
|
|
||||||
yaml_data = YAML.parse(File.read(config_path))
|
|
||||||
vaults_data = yaml_data["vaults"].as_a
|
|
||||||
|
|
||||||
vaults_data.each do |vault_data|
|
|
||||||
name = vault_data["name"].as_s
|
|
||||||
encrypted_path = vault_data["encrypted_path"].as_s
|
|
||||||
@vaults << Vault.new(name, encrypted_path, "#{name}.Open")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Main execution starts here
|
|
||||||
home_dir = ENV["HOME"] || raise "Home directory not found"
|
|
||||||
config = Config.new(File.join(home_dir, ".config/gx-vault.yml"))
|
|
||||||
|
|
||||||
# Correcting the fzf interaction part
|
|
||||||
input = IO::Memory.new
|
|
||||||
config.vaults.each do |vault|
|
|
||||||
name_display = vault.mounted? ? "#{vault.name} [#{ "open".colorize(:green) }]" : vault.name
|
|
||||||
input.puts name_display
|
|
||||||
end
|
|
||||||
input.rewind
|
|
||||||
|
|
||||||
output = IO::Memory.new
|
|
||||||
error = STDERR
|
|
||||||
process = Process.new("fzf", ["--ansi"], input: input, output: output, error: error)
|
|
||||||
|
|
||||||
unless process.wait.success?
|
|
||||||
puts "Error executing fzf: #{error.to_s.strip}".colorize(:red)
|
|
||||||
exit(1)
|
|
||||||
end
|
|
||||||
|
|
||||||
selected_vault_name = output.to_s.strip.split.first?
|
|
||||||
selected_vault = config.vaults.find { |v| v.name == selected_vault_name }
|
|
||||||
|
|
||||||
if selected_vault
|
|
||||||
selected_vault.mounted? ? selected_vault.unmount : selected_vault.mount
|
|
||||||
else
|
|
||||||
puts "Vault not found.".colorize(:red)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
38
src/vault.cr
Normal file
38
src/vault.cr
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
module GX
|
||||||
|
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 mounted? : Bool
|
||||||
|
`mount`.includes?("#{encrypted_path} on #{mount_dir}")
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
end
|
||||||
|
puts "Vault #{name} is now available on #{mount_dir}".colorize(:green)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
def unmount
|
||||||
|
`fusermount -u #{mount_dir}`
|
||||||
|
puts "Vault #{name} is now closed.".colorize(:green)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue