Compare commits

..

No commits in common. "63c0bbbb1c9d0685761056c71a834c910816420c" and "8efe8ea5d9be105280f3d1043b4b825e0e0a6b2e" have entirely different histories.

16 changed files with 238 additions and 337 deletions

View file

@ -92,18 +92,13 @@ module GX
STDOUT.puts "#{PROGRAM_NAME} #{VERSION}"
when Config::Mode::Mount
@config.load_from_file
filesystem = choose_filesystem
mount_or_umount(filesystem) if !filesystem.nil?
mount
end
end
def choose_filesystem()
names_display = {} of String => NamedTuple(filesystem: Models::AbstractFilesystemConfig, ansi_name: String)
config_root = @config.root
return if config_root.nil?
config_root.filesystems.each do |filesystem|
def mount()
names_display = {} of String => NamedTuple(filesystem: Filesystem, ansi_name: String)
@config.filesystems.each do |filesystem|
fs_str = filesystem.type.ljust(12,' ')
suffix = ""
@ -126,22 +121,12 @@ module GX
selected_filesystem = names_display[result_filesystem_name][:filesystem]
puts ">> #{selected_filesystem.name}".colorize(:yellow)
if !selected_filesystem
STDERR.puts "Vault not found: #{selected_filesystem}.".colorize(:red)
return
end
return selected_filesystem
end
def mount_or_umount(selected_filesystem)
config_root_safe = @config.root
return if config_root_safe.nil?
if !selected_filesystem.mounted?
selected_filesystem.mount()
if selected_filesystem
selected_filesystem.mounted? ? selected_filesystem.unmount : selected_filesystem.mount
else
selected_filesystem.umount()
STDERR.puts "Vault not found: #{selected_filesystem}.".colorize(:red)
end
end
end
end

View file

@ -5,7 +5,7 @@
require "crinja"
require "./models"
require "./filesystems"
module GX
class Config
@ -23,10 +23,8 @@ module GX
record AddArgs, name : String, path : String
record DelArgs, name : String
# getter filesystems : Array(Models::AbstractFilesystemConfig)
getter filesystems : Array(Filesystem)
getter home_dir : String
getter root : Models::RootConfig?
property verbose : Bool
property mode : Mode
property path : String?
@ -40,13 +38,13 @@ module GX
@verbose = false
@mode = Mode::Mount
@filesystems = [] of Models::AbstractFilesystemConfig
@filesystems = [] of Filesystem
@path = nil
@args = NoArgs
end
private def detect_config_file()
def detect_config_file()
possible_files = [
File.join(@home_dir, ".config", "mfm", "config.yaml"),
File.join(@home_dir, ".config", "mfm", "config.yml"),
@ -70,32 +68,35 @@ module GX
end
def load_from_file
config_path = @path
if config_path.nil?
config_path = detect_config_file()
path = @path
if path.nil?
path = detect_config_file()
end
@path = config_path
@path = path
@filesystems = [] of Filesystem
if !File.exists? config_path
if !File.exists? path
Log.error { "File #{path} does not exist!".colorize(:red) }
exit(1)
end
load_filesystems(path)
end
private def load_filesystems(config_path : String)
file_data = File.read(config_path)
# FIXME: render template on a value basis (instead of global)
file_patched = Crinja.render(file_data, {"env" => ENV.to_h})
root = Models::RootConfig.from_yaml(file_patched)
yaml_data = YAML.parse(file_patched)
vaults_data = yaml_data["filesystems"].as_a
global_mount_point = root.global.mount_point
raise "Invalid global mount point" if global_mount_point.nil?
root.filesystems.each do |selected_filesystem|
if !selected_filesystem.mount_point?
selected_filesystem.mount_point =
File.join(global_mount_point, selected_filesystem.mounted_name)
end
vaults_data.each do |filesystem_data|
type = filesystem_data["type"].as_s
name = filesystem_data["name"].as_s
# encrypted_path = filesystem_data["encrypted_path"].as_s
@filesystems << Filesystem.from_yaml(filesystem_data.to_yaml)
# @filesystems << Filesystem.new(name, encrypted_path, "#{name}.Open")
end
@root = root
end
end
end

9
src/filesystems.cr Normal file
View file

@ -0,0 +1,9 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#
# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland <glenux@glenux.net>
# Copyright © 2023 Glenn Y. Rolland <glenux@glenux.net>
require "./filesystems/gocryptfs"
require "./filesystems/sshfs"
require "./filesystems/httpdirfs"
require "./filesystems/filesystem"

View file

@ -0,0 +1,48 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#
# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland <glenux@glenux.net>
# Copyright © 2023 Glenn Y. Rolland <glenux@glenux.net>
require "yaml"
module GX
abstract class Filesystem
include YAML::Serializable
use_yaml_discriminator "type", {
gocryptfs: GoCryptFS,
sshfs: SshFS,
httpdirfs: HttpDirFS
}
property type : String
end
module GenericFilesystem
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
end
end
require "./gocryptfs"
require "./sshfs"

View file

@ -0,0 +1,47 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#
# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland <glenux@glenux.net>
# Copyright © 2023 Glenn Y. Rolland <glenux@glenux.net>
require "shellwords"
require "./filesystem"
module GX
class GoCryptFS < Filesystem
getter name : String = ""
getter encrypted_path : String = ""
@[YAML::Field(key: "mount_dir", ignore: true)]
getter mount_dir : String = ""
include GenericFilesystem
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}")
end
def mount
super do
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
end
end
end
end

View file

@ -0,0 +1,48 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#
# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland <glenux@glenux.net>
# Copyright © 2023 Glenn Y. Rolland <glenux@glenux.net>
require "shellwords"
require "./filesystem"
module GX
class HttpDirFS < Filesystem
getter name : String = ""
getter url : String = ""
@[YAML::Field(key: "mount_dir", ignore: true)]
getter mount_dir : String = ""
include GenericFilesystem
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}")
end
def mount
super do
input = STDIN
output = STDOUT
error = STDERR
process = Process.new(
"httpdirfs",
["#{url}", mount_dir],
input: input,
output: output,
error: error
)
unless process.wait.success?
puts "Error mounting the filesystem".colorize(:red)
return
end
end
end
end
end

54
src/filesystems/sshfs.cr Normal file
View file

@ -0,0 +1,54 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#
# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland <glenux@glenux.net>
# Copyright © 2023 Glenn Y. Rolland <glenux@glenux.net>
require "shellwords"
require "./filesystem"
module GX
class SshFS < Filesystem
getter name : String = ""
getter remote_path : String = ""
getter remote_user : String = ""
getter remote_host : String = ""
getter remote_port : String = "22"
@[YAML::Field(key: "mount_dir", ignore: true)]
getter mount_dir : String = ""
include GenericFilesystem
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?("#{remote_user}@#{remote_host}:#{remote_path} on #{mount_dir}")
end
def mount
super do
input = STDIN
output = STDOUT
error = STDERR
process = Process.new(
"sshfs",
[
"-p", remote_port,
"#{remote_user}@#{remote_host}:#{remote_path}",
mount_dir
],
input: input,
output: output,
error: error
)
unless process.wait.success?
puts "Error mounting the filesystem".colorize(:red)
return
end
end
end
end
end

View file

@ -8,6 +8,7 @@ require "colorize"
require "json"
require "log"
require "./filesystems/gocryptfs"
require "./config"
require "./cli"
@ -31,6 +32,7 @@ Log.setup do |config|
end
end
app = GX::Cli.new
app.parse_command_line(ARGV)
app.run

View file

@ -1,11 +0,0 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#
# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland <glenux@glenux.net>
# Copyright © 2023 Glenn Y. Rolland <glenux@glenux.net>
require "./models/root_config"
require "./models/global_config"
require "./models/gocryptfs_config"
require "./models/sshfs_config"
require "./models/httpdirfs_config"
require "./models/abstract_filesystem_config"

View file

@ -1,32 +0,0 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#
# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland <glenux@glenux.net>
# Copyright © 2023 Glenn Y. Rolland <glenux@glenux.net>
require "yaml"
module GX::Models
abstract class AbstractFilesystemConfig
include YAML::Serializable
# include YAML::Serializable::Strict
use_yaml_discriminator "type", {
gocryptfs: GoCryptFSConfig,
sshfs: SshFSConfig,
httpdirfs: HttpDirFSConfig
}
getter type : String
getter name : String
property mount_point : String?
abstract def _mount_wrapper(&block)
abstract def _mount_action()
abstract def _mounted_prefix()
abstract def mounted_name()
abstract def mounted?()
abstract def mount()
abstract def umount()
abstract def mount_point?()
end
end

View file

@ -1,51 +0,0 @@
module GX::Models::Concerns
module Base
def mounted?() : Bool
mount_point_safe = @mount_point
raise "Invalid mountpoint value" if mount_point_safe.nil?
`mount`.includes?(" on #{mount_point_safe} type ")
end
def umount() : Nil
mount_point_safe = @mount_point
raise "Invalid mountpoint value" if mount_point_safe.nil?
system("fusermount -u #{mount_point_safe.shellescape}")
fusermount_status = $?
if fusermount_status.success?
puts "Models #{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_point?()
!mount_point.nil?
end
def mount()
_mount_wrapper() do
_mount_action
end
end
def _mount_wrapper(&block) : Nil
mount_point_safe = mount_point
return if mount_point_safe.nil?
Dir.mkdir_p(mount_point_safe) unless Dir.exists?(mount_point_safe)
if mounted?
puts "Already mounted. Skipping.".colorize(:yellow)
return
end
yield
puts "Models #{name} is now available on #{mount_point_safe}".colorize(:green)
end
end
end

View file

@ -1,28 +0,0 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#
# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland <glenux@glenux.net>
# Copyright © 2023 Glenn Y. Rolland <glenux@glenux.net>
require "yaml"
require "./abstract_filesystem_config"
module GX::Models
class GlobalConfig
include YAML::Serializable
include YAML::Serializable::Strict
@[YAML::Field(key: "mount_point")]
getter mount_point : String?
def after_initialize()
home_dir = ENV["HOME"] || raise "Home directory not found"
# Set default mountpoint from global if none defined
if @mount_point.nil? || @mount_point.try &.empty?
@mount_point = File.join(home_dir, "mnt")
end
end
end
end

View file

@ -1,41 +0,0 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#
# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland <glenux@glenux.net>
# Copyright © 2023 Glenn Y. Rolland <glenux@glenux.net>
require "shellwords"
require "./abstract_filesystem_config"
require "./concerns/base"
module GX::Models
class GoCryptFSConfig < AbstractFilesystemConfig
getter encrypted_path : String = ""
include Concerns::Base
def _mounted_prefix()
"#{encrypted_path}"
end
def mounted_name()
"#{@name}.Open"
end
def _mount_action()
mount_point_safe = @mount_point
raise "Invalid mount point" if mount_point_safe.nil?
process = Process.new(
"gocryptfs",
["-idle", "15m", @encrypted_path, mount_point_safe],
input: STDIN,
output: STDOUT,
error: STDERR
)
unless process.wait.success?
puts "Error mounting the vault".colorize(:red)
return
end
end
end
end

View file

@ -1,41 +0,0 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#
# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland <glenux@glenux.net>
# Copyright © 2023 Glenn Y. Rolland <glenux@glenux.net>
require "shellwords"
require "./abstract_filesystem_config"
require "./concerns/base"
module GX::Models
class HttpDirFSConfig < AbstractFilesystemConfig
getter url : String = ""
include Concerns::Base
def _mounted_prefix()
"httpdirfs"
end
def mounted_name()
@name
end
def _mount_action()
mount_point_safe = @mount_point
raise "Invalid mount point" if mount_point_safe.nil?
process = Process.new(
"httpdirfs",
["#{@url}", mount_point_safe],
input: STDIN,
output: STDOUT,
error: STDERR
)
unless process.wait.success?
puts "Error mounting the filesystem".colorize(:red)
return
end
end
end
end

View file

@ -1,41 +0,0 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#
# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland <glenux@glenux.net>
# Copyright © 2023 Glenn Y. Rolland <glenux@glenux.net>
require "yaml"
require "./abstract_filesystem_config"
require "./global_config"
module GX::Models
# class CrinjaConverter
# def self.from_yaml(ctx : YAML::ParseContext , node : YAML::Nodes::Node)
# l_node = node
# if l_node.is_a?(YAML::Nodes::Scalar)
# value_patched = Crinja.render(l_node.value, {"env" => ENV.to_h})
# return value_patched
# end
# return "<null>"
# end
#
# def self.to_yaml(value, builder : YAML::Nodes::Builder)
# end
# end
class RootConfig
include YAML::Serializable
include YAML::Serializable::Strict
# @[YAML::Field(key: "version", converter: GX::Models::CrinjaConverter)]
@[YAML::Field(key: "version")]
getter version : String
@[YAML::Field(key: "global")]
getter global : GlobalConfig
@[YAML::Field(key: "filesystems")]
getter filesystems : Array(AbstractFilesystemConfig)
end
end

View file

@ -1,48 +0,0 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#
# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland <glenux@glenux.net>
# Copyright © 2023 Glenn Y. Rolland <glenux@glenux.net>
require "shellwords"
require "./abstract_filesystem_config"
require "./concerns/base"
module GX::Models
class SshFSConfig < AbstractFilesystemConfig
getter remote_path : String = ""
getter remote_user : String = ""
getter remote_host : String = ""
getter remote_port : String = "22"
include Concerns::Base
def _mounted_prefix()
"#{@remote_user}@#{@remote_host}:#{@remote_path}"
end
def mounted_name()
@name
end
def _mount_action()
mount_point_safe = @mount_point
raise "Invalid mount point" if mount_point_safe.nil?
process = Process.new(
"sshfs",
[
"-p", remote_port,
"#{@remote_user}@#{@remote_host}:#{@remote_path}",
mount_point_safe
],
input: STDIN,
output: STDOUT,
error: STDERR
)
unless process.wait.success?
puts "Error mounting the filesystem".colorize(:red)
return
end
end
end
end