refactor: define abstract defs & move most functions to concerns/base
This commit is contained in:
parent
994f9e1885
commit
ee3f57ec20
5 changed files with 69 additions and 72 deletions
|
@ -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"
|
||||
|
|
41
src/filesystems/concerns/base.cr
Normal file
41
src/filesystems/concerns/base.cr
Normal file
|
@ -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
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue