Merge pull request 'Add option to auto-open directory after mount' (#33) from feature/30-add-option-to-auto-open-directory into develop
All checks were successful
continuous-integration/drone/push Build is passing

Reviewed-on: #33
This commit is contained in:
Glenn Y. Rolland 2023-11-24 09:26:23 +00:00
commit 7f789daefa
8 changed files with 77 additions and 15 deletions

View file

@ -38,6 +38,11 @@ module GX
@config.verbose = true @config.verbose = true
end end
parser.on("-o", "--open", "Automatically open directory after mount") do |flag|
Log.info { "Auto-open enabled" }
@config.auto_open = true
end
parser.on("--version", "Show version") do |flag| parser.on("--version", "Show version") do |flag|
@config.mode = Config::Mode::ShowVersion @config.mode = Config::Mode::ShowVersion
end end
@ -93,10 +98,56 @@ module GX
when Config::Mode::Mount when Config::Mode::Mount
@config.load_from_file @config.load_from_file
filesystem = choose_filesystem filesystem = choose_filesystem
mount_or_umount(filesystem) if !filesystem.nil? raise Models::InvalidFilesystemError.new("Invalid filesystem") if filesystem.nil?
mount_or_umount(filesystem)
auto_open(filesystem) if @config.auto_open
end end
end end
def auto_open(filesystem)
# FIXME: support xdg-open
# FIXME: support mailcap
# FIXME: support user-defined command
# FIXME: detect graphical environment
mount_point_safe = filesystem.mount_point
raise Models::InvalidMountpointError.new("Invalid filesystem") if mount_point_safe.nil?
if graphical_environment?
process = Process.new(
"xdg-open", ## FIXME: make configurable
[mount_point_safe],
input: STDIN,
output: STDOUT,
error: STDERR
)
unless process.wait.success?
puts "Error opening filesystem".colorize(:red)
return
end
else
process = Process.new(
"vifm", ## FIXME: make configurable
[mount_point_safe],
input: STDIN,
output: STDOUT,
error: STDERR
)
unless process.wait.success?
puts "Error opening filesystem".colorize(:red)
return
end
end
end
def graphical_environment?
if ENV["DISPLAY"]? || ENV["WAYLAND_DISPLAY"]?
return true
end
return false
end
def choose_filesystem() def choose_filesystem()
names_display = {} of String => NamedTuple(filesystem: Models::AbstractFilesystemConfig, ansi_name: String) names_display = {} of String => NamedTuple(filesystem: Models::AbstractFilesystemConfig, ansi_name: String)
@ -134,9 +185,6 @@ module GX
end end
def mount_or_umount(selected_filesystem) def mount_or_umount(selected_filesystem)
config_root_safe = @config.root
return if config_root_safe.nil?
if !selected_filesystem.mounted? if !selected_filesystem.mounted?
selected_filesystem.mount() selected_filesystem.mount()
else else

View file

@ -11,6 +11,9 @@ module GX
class Config class Config
Log = ::Log.for("config") Log = ::Log.for("config")
class MissingFileError < Exception
end
enum Mode enum Mode
ConfigAdd ConfigAdd
ConfigDelete ConfigDelete
@ -31,14 +34,15 @@ module GX
property mode : Mode property mode : Mode
property path : String? property path : String?
property args : AddArgs.class | DelArgs.class | NoArgs.class property args : AddArgs.class | DelArgs.class | NoArgs.class
property auto_open : Bool
def initialize() def initialize()
if !ENV["HOME"]? raise Models::InvalidEnvironmentError.new("Home directory not found") if !ENV["HOME"]?
raise "Home directory not found"
end
@home_dir = ENV["HOME"] @home_dir = ENV["HOME"]
@verbose = false @verbose = false
@auto_open = false
@mode = Mode::Mount @mode = Mode::Mount
@filesystems = [] of Models::AbstractFilesystemConfig @filesystems = [] of Models::AbstractFilesystemConfig
@path = nil @path = nil
@ -66,7 +70,7 @@ module GX
end end
Log.error { "No configuration file found in any of the standard locations" } Log.error { "No configuration file found in any of the standard locations" }
raise "Configuration file not found" raise MissingFileError.new("Configuration file not found")
end end
def load_from_file def load_from_file
@ -87,7 +91,7 @@ module GX
root = Models::RootConfig.from_yaml(file_patched) root = Models::RootConfig.from_yaml(file_patched)
global_mount_point = root.global.mount_point global_mount_point = root.global.mount_point
raise "Invalid global mount point" if global_mount_point.nil? raise Models::InvalidMountpointError.new("Invalid global mount point") if global_mount_point.nil?
root.filesystems.each do |selected_filesystem| root.filesystems.each do |selected_filesystem|
if !selected_filesystem.mount_point? if !selected_filesystem.mount_point?

View file

@ -6,6 +6,12 @@
require "yaml" require "yaml"
module GX::Models module GX::Models
class InvalidFilesystemError < Exception
end
class InvalidMountpointError < Exception
end
abstract class AbstractFilesystemConfig abstract class AbstractFilesystemConfig
include YAML::Serializable include YAML::Serializable
# include YAML::Serializable::Strict # include YAML::Serializable::Strict

View file

@ -3,14 +3,14 @@ module GX::Models::Concerns
module Base module Base
def mounted?() : Bool def mounted?() : Bool
mount_point_safe = @mount_point mount_point_safe = @mount_point
raise "Invalid mountpoint value" if mount_point_safe.nil? raise InvalidMountpointError.new("Invalid mountpoint value") if mount_point_safe.nil?
`mount`.includes?(" on #{mount_point_safe} type ") `mount`.includes?(" on #{mount_point_safe} type ")
end end
def umount() : Nil def umount() : Nil
mount_point_safe = @mount_point mount_point_safe = @mount_point
raise "Invalid mountpoint value" if mount_point_safe.nil? raise InvalidMountpointError.new("Invalid mountpoint value") if mount_point_safe.nil?
system("fusermount -u #{mount_point_safe.shellescape}") system("fusermount -u #{mount_point_safe.shellescape}")
fusermount_status = $? fusermount_status = $?

View file

@ -7,6 +7,9 @@ require "yaml"
require "./abstract_filesystem_config" require "./abstract_filesystem_config"
module GX::Models module GX::Models
class InvalidEnvironmentError < Exception
end
class GlobalConfig class GlobalConfig
include YAML::Serializable include YAML::Serializable
include YAML::Serializable::Strict include YAML::Serializable::Strict
@ -15,7 +18,8 @@ module GX::Models
getter mount_point : String? getter mount_point : String?
def after_initialize() def after_initialize()
home_dir = ENV["HOME"] || raise "Home directory not found" raise InvalidEnvironmentError.new("Home directory not found") if !ENV["HOME"]?
home_dir = ENV["HOME"]
# Set default mountpoint from global if none defined # Set default mountpoint from global if none defined
if @mount_point.nil? || @mount_point.try &.empty? if @mount_point.nil? || @mount_point.try &.empty?

View file

@ -23,7 +23,7 @@ module GX::Models
def _mount_action() def _mount_action()
mount_point_safe = @mount_point mount_point_safe = @mount_point
raise "Invalid mount point" if mount_point_safe.nil? raise InvalidMountpointError.new("Invalid mount point") if mount_point_safe.nil?
process = Process.new( process = Process.new(
"gocryptfs", "gocryptfs",

View file

@ -23,7 +23,7 @@ module GX::Models
def _mount_action() def _mount_action()
mount_point_safe = @mount_point mount_point_safe = @mount_point
raise "Invalid mount point" if mount_point_safe.nil? raise InvalidMountpointError.new("Invalid mount point") if mount_point_safe.nil?
process = Process.new( process = Process.new(
"httpdirfs", "httpdirfs",

View file

@ -26,7 +26,7 @@ module GX::Models
def _mount_action() def _mount_action()
mount_point_safe = @mount_point mount_point_safe = @mount_point
raise "Invalid mount point" if mount_point_safe.nil? raise InvalidMountpointError.new("Invalid mount point") if mount_point_safe.nil?
process = Process.new( process = Process.new(
"sshfs", "sshfs",