From ee3f57ec2011c5ad8b3d8c7a2b49a5e4c042e7a1 Mon Sep 17 00:00:00 2001 From: Glenn Date: Tue, 21 Nov 2023 23:11:21 +0100 Subject: [PATCH] refactor: define abstract defs & move most functions to concerns/base --- src/filesystems/abstract_filesystem.cr | 29 ++---------------- src/filesystems/concerns/base.cr | 41 ++++++++++++++++++++++++++ src/filesystems/gocryptfs.cr | 23 +++++---------- src/filesystems/httpdirfs.cr | 23 +++++---------- src/filesystems/sshfs.cr | 25 ++++++---------- 5 files changed, 69 insertions(+), 72 deletions(-) create mode 100644 src/filesystems/concerns/base.cr diff --git a/src/filesystems/abstract_filesystem.cr b/src/filesystems/abstract_filesystem.cr index 443535d..29af3f8 100644 --- a/src/filesystems/abstract_filesystem.cr +++ b/src/filesystems/abstract_filesystem.cr @@ -17,34 +17,11 @@ module GX } property type : String - end - module FilesystemBase - def unmount - system("fusermount -u #{mount_dir.shellescape}") - fusermount_status = $? - - if fusermount_status.success? - puts "Filesystem #{name} is now closed.".colorize(:green) - else - puts "Error: Unable to unmount filesystem #{name} (exit code: #{fusermount_status.exit_code}).".colorize(:red) - end - end - - def mount(&block) - Dir.mkdir_p(mount_dir) unless Dir.exists?(mount_dir) - if mounted? - puts "Already mounted. Skipping.".colorize(:yellow) - return - end - - yield - - puts "Filesystem #{name} is now available on #{mount_dir}".colorize(:green) - end + abstract def mount() + abstract def unmount() + abstract def mounted_prefix() end end end -require "./gocryptfs" -require "./sshfs" diff --git a/src/filesystems/concerns/base.cr b/src/filesystems/concerns/base.cr new file mode 100644 index 0000000..ac0cd17 --- /dev/null +++ b/src/filesystems/concerns/base.cr @@ -0,0 +1,41 @@ + +module GX::Filesystem::Concerns + module Base + def after_initialize() + home_dir = ENV["HOME"] || raise "Home directory not found" + + # Use default mountpoint if none defined + if @mount_dir.empty? + @mount_dir = File.join(home_dir, "mnt/#{@name}") + end + end + + def mounted? : Bool + `mount`.includes?(" on #{mount_dir} type ") + end + + def unmount : Nil + system("fusermount -u #{mount_dir.shellescape}") + fusermount_status = $? + + if fusermount_status.success? + puts "Filesystem #{name} is now closed.".colorize(:green) + else + puts "Error: Unable to unmount filesystem #{name} (exit code: #{fusermount_status.exit_code}).".colorize(:red) + end + end + + def _mount_wrapper(&block) : Nil + Dir.mkdir_p(mount_dir) unless Dir.exists?(mount_dir) + if mounted? + puts "Already mounted. Skipping.".colorize(:yellow) + return + end + + yield + + puts "Filesystem #{name} is now available on #{mount_dir}".colorize(:green) + end + end + +end diff --git a/src/filesystems/gocryptfs.cr b/src/filesystems/gocryptfs.cr index b405482..3e5e4a1 100644 --- a/src/filesystems/gocryptfs.cr +++ b/src/filesystems/gocryptfs.cr @@ -5,6 +5,7 @@ require "shellwords" require "./abstract_filesystem" +require "./concerns/base" module GX module Filesystem @@ -15,28 +16,20 @@ module GX @[YAML::Field(key: "mount_dir", ignore: true)] getter mount_dir : String = "" - include FilesystemBase + include Concerns::Base - def after_initialize() - home_dir = ENV["HOME"] || raise "Home directory not found" - @mount_dir = File.join(home_dir, "mnt/#{@name}.Open") - end - - def mounted? : Bool - `mount`.includes?("#{encrypted_path} on #{mount_dir}") + def mounted_prefix() + "#{encrypted_path}" end def mount - super do - input = STDIN - output = STDOUT - error = STDERR + _mount_wrapper do process = Process.new( "gocryptfs", ["-idle", "15m", encrypted_path, mount_dir], - input: input, - output: output, - error: error + input: STDIN, + output: STDOUT, + error: STDERR ) unless process.wait.success? puts "Error mounting the vault".colorize(:red) diff --git a/src/filesystems/httpdirfs.cr b/src/filesystems/httpdirfs.cr index 481e4ba..16d7550 100644 --- a/src/filesystems/httpdirfs.cr +++ b/src/filesystems/httpdirfs.cr @@ -5,6 +5,7 @@ require "shellwords" require "./abstract_filesystem" +require "./concerns/base" module GX module Filesystem @@ -15,28 +16,20 @@ module GX @[YAML::Field(key: "mount_dir", ignore: true)] getter mount_dir : String = "" - include FilesystemBase + include Concerns::Base - def after_initialize() - home_dir = ENV["HOME"] || raise "Home directory not found" - @mount_dir = File.join(home_dir, "mnt/#{@name}") - end - - def mounted? : Bool - `mount`.includes?("httpdirfs on #{mount_dir}") + def mounted_prefix() + "httpdirfs" end def mount - super do - input = STDIN - output = STDOUT - error = STDERR + _mount_wrapper do process = Process.new( "httpdirfs", ["#{url}", mount_dir], - input: input, - output: output, - error: error + input: STDIN, + output: STDOUT, + error: STDERR ) unless process.wait.success? puts "Error mounting the filesystem".colorize(:red) diff --git a/src/filesystems/sshfs.cr b/src/filesystems/sshfs.cr index 16ca006..7f445ae 100644 --- a/src/filesystems/sshfs.cr +++ b/src/filesystems/sshfs.cr @@ -5,6 +5,7 @@ require "shellwords" require "./abstract_filesystem" +require "./concerns/base" module GX module Filesystem @@ -18,22 +19,14 @@ module GX @[YAML::Field(key: "mount_dir", ignore: true)] getter mount_dir : String = "" - include FilesystemBase + include Concerns::Base - def after_initialize() - home_dir = ENV["HOME"] || raise "Home directory not found" - @mount_dir = File.join(home_dir, "mnt/#{@name}") + def mounted_prefix() + "#{remote_user}@#{remote_host}:#{remote_path}" end - def mounted? : Bool - `mount`.includes?("#{remote_user}@#{remote_host}:#{remote_path} on #{mount_dir}") - end - - def mount - super do - input = STDIN - output = STDOUT - error = STDERR + def mount() + _mount_wrapper do process = Process.new( "sshfs", [ @@ -41,9 +34,9 @@ module GX "#{remote_user}@#{remote_host}:#{remote_path}", mount_dir ], - input: input, - output: output, - error: error + input: STDIN, + output: STDOUT, + error: STDERR ) unless process.wait.success? puts "Error mounting the filesystem".colorize(:red)