From 3f0a0adddd6fa93cd804a0401036102e47dc0137 Mon Sep 17 00:00:00 2001 From: Glenn Date: Sun, 26 Nov 2023 16:05:01 +0100 Subject: [PATCH] feat: add base structure for commands --- src/cli.cr | 7 ++- src/config.cr | 7 +-- src/models.cr | 9 +-- src/models/concerns/base.cr | 56 ------------------- src/models/exceptions.cr | 15 +++++ .../abstract_config.cr} | 10 +--- src/models/filesystems/concerns/mount.cr | 14 +++++ .../filesystems/concerns/mount_point.cr | 14 +++++ .../filesystems/concerns/mount_wrapper.cr | 29 ++++++++++ src/models/filesystems/concerns/mounted.cr | 17 ++++++ src/models/filesystems/concerns/umount.cr | 23 ++++++++ .../{ => filesystems}/gocryptfs_config.cr | 18 ++++-- .../{ => filesystems}/httpdirfs_config.cr | 19 +++++-- src/models/{ => filesystems}/sshfs_config.cr | 18 ++++-- src/models/global_config.cr | 2 +- src/models/root_config.cr | 4 +- src/operations/filesystem_create_command.cr | 12 ---- src/operations/filesystem_delete_command.cr | 12 ---- src/operations/filesystem_edit_command.cr | 12 ---- src/operations/filesystem_mount_command.cr | 12 ---- src/operations/filesystem_umount_command.cr | 12 ---- src/operations/filesystems/create_command.cr | 14 +++++ src/operations/filesystems/delete_command.cr | 14 +++++ src/operations/filesystems/edit_command.cr | 14 +++++ src/operations/filesystems/list_command.cr | 15 +++++ src/operations/filesystems/mount_command.cr | 14 +++++ src/operations/filesystems/select_command.cr | 15 +++++ src/operations/filesystems/umount_command.cr | 14 +++++ src/operations/global/edit_command.cr | 15 +++++ 29 files changed, 286 insertions(+), 151 deletions(-) delete mode 100644 src/models/concerns/base.cr create mode 100644 src/models/exceptions.cr rename src/models/{abstract_filesystem_config.cr => filesystems/abstract_config.cr} (82%) create mode 100644 src/models/filesystems/concerns/mount.cr create mode 100644 src/models/filesystems/concerns/mount_point.cr create mode 100644 src/models/filesystems/concerns/mount_wrapper.cr create mode 100644 src/models/filesystems/concerns/mounted.cr create mode 100644 src/models/filesystems/concerns/umount.cr rename src/models/{ => filesystems}/gocryptfs_config.cr (64%) rename src/models/{ => filesystems}/httpdirfs_config.cr (62%) rename src/models/{ => filesystems}/sshfs_config.cr (69%) delete mode 100644 src/operations/filesystem_create_command.cr delete mode 100644 src/operations/filesystem_delete_command.cr delete mode 100644 src/operations/filesystem_edit_command.cr delete mode 100644 src/operations/filesystem_mount_command.cr delete mode 100644 src/operations/filesystem_umount_command.cr create mode 100644 src/operations/filesystems/create_command.cr create mode 100644 src/operations/filesystems/delete_command.cr create mode 100644 src/operations/filesystems/edit_command.cr create mode 100644 src/operations/filesystems/list_command.cr create mode 100644 src/operations/filesystems/mount_command.cr create mode 100644 src/operations/filesystems/select_command.cr create mode 100644 src/operations/filesystems/umount_command.cr create mode 100644 src/operations/global/edit_command.cr diff --git a/src/cli.cr b/src/cli.cr index 9195377..a08710a 100644 --- a/src/cli.cr +++ b/src/cli.cr @@ -151,8 +151,13 @@ module GX return false end + alias FsTuple = NamedTuple( + filesystem: Models::Filesystems::AbstractConfig, + ansi_name: String + ) + def choose_filesystem() - names_display = {} of String => NamedTuple(filesystem: Models::AbstractFilesystemConfig, ansi_name: String) + names_display = {} of String => FsTuple config_root = @config.root return if config_root.nil? diff --git a/src/config.cr b/src/config.cr index bb0d0d7..5df0593 100644 --- a/src/config.cr +++ b/src/config.cr @@ -26,7 +26,6 @@ module GX record AddArgs, name : String, path : String record DelArgs, name : String - # getter filesystems : Array(Models::AbstractFilesystemConfig) getter home_dir : String getter root : Models::RootConfig? @@ -44,7 +43,7 @@ module GX @auto_open = false @mode = Mode::Mount - @filesystems = [] of Models::AbstractFilesystemConfig + @filesystems = [] of Models::Filesystems::AbstractConfig @path = nil @args = NoArgs @@ -62,10 +61,10 @@ module GX possible_files.each do |file_path| if File.exists?(file_path) - Log.info { "Configuration file found: #{file_path}" } + Log.info { "Configuration file found: #{file_path}" } if @verbose return file_path if File.exists?(file_path) else - Log.debug { "Configuration file not found: #{file_path}" } + Log.debug { "Configuration file not found: #{file_path}" } if @verbose end end diff --git a/src/models.cr b/src/models.cr index a5d97a4..5f84751 100644 --- a/src/models.cr +++ b/src/models.cr @@ -5,7 +5,8 @@ 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" +require "./models/filesystems/gocryptfs_config" +require "./models/filesystems/sshfs_config" +require "./models/filesystems/httpdirfs_config" +require "./models/filesystems/abstract_config" +require "./models/exceptions" diff --git a/src/models/concerns/base.cr b/src/models/concerns/base.cr deleted file mode 100644 index a12fab2..0000000 --- a/src/models/concerns/base.cr +++ /dev/null @@ -1,56 +0,0 @@ - -module GX::Models::Concerns - module Base - def mounted?() : Bool - mount_point_safe = @mount_point - raise InvalidMountpointError.new("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 InvalidMountpointError.new("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 - - result_status = yield - - if result_status.success? - puts "Models #{name} is now available on #{mount_point_safe}".colorize(:green) - else - puts "Error mounting the vault".colorize(:red) - return - end - end - end - -end diff --git a/src/models/exceptions.cr b/src/models/exceptions.cr new file mode 100644 index 0000000..0fccbe3 --- /dev/null +++ b/src/models/exceptions.cr @@ -0,0 +1,15 @@ + +# SPDX-License-Identifier: GPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland +# Copyright © 2023 Glenn Y. Rolland + +require "yaml" + +module GX::Models + class InvalidFilesystemError < Exception + end + + class InvalidMountpointError < Exception + end +end diff --git a/src/models/abstract_filesystem_config.cr b/src/models/filesystems/abstract_config.cr similarity index 82% rename from src/models/abstract_filesystem_config.cr rename to src/models/filesystems/abstract_config.cr index afa55bf..9cb6a34 100644 --- a/src/models/abstract_filesystem_config.cr +++ b/src/models/filesystems/abstract_config.cr @@ -5,14 +5,8 @@ require "yaml" -module GX::Models - class InvalidFilesystemError < Exception - end - - class InvalidMountpointError < Exception - end - - abstract class AbstractFilesystemConfig +module GX::Models::Filesystems + abstract class AbstractConfig include YAML::Serializable # include YAML::Serializable::Strict diff --git a/src/models/filesystems/concerns/mount.cr b/src/models/filesystems/concerns/mount.cr new file mode 100644 index 0000000..227cc82 --- /dev/null +++ b/src/models/filesystems/concerns/mount.cr @@ -0,0 +1,14 @@ +# SPDX-License-Identifier: GPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland +# Copyright © 2023 Glenn Y. Rolland + +module GX::Models::Concerns + module Mount + def mount() + _mount_wrapper() do + _mount_action + end + end + end +end diff --git a/src/models/filesystems/concerns/mount_point.cr b/src/models/filesystems/concerns/mount_point.cr new file mode 100644 index 0000000..ae1e9c0 --- /dev/null +++ b/src/models/filesystems/concerns/mount_point.cr @@ -0,0 +1,14 @@ +# SPDX-License-Identifier: GPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland +# Copyright © 2023 Glenn Y. Rolland + +module GX::Models::Concerns + module MountPoint + def mount_point?() + !mount_point.nil? + end + end +end + + diff --git a/src/models/filesystems/concerns/mount_wrapper.cr b/src/models/filesystems/concerns/mount_wrapper.cr new file mode 100644 index 0000000..a4130a6 --- /dev/null +++ b/src/models/filesystems/concerns/mount_wrapper.cr @@ -0,0 +1,29 @@ +# SPDX-License-Identifier: GPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland +# Copyright © 2023 Glenn Y. Rolland + +module GX::Models::Concerns + module MountWrapper + 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 + + result_status = yield + + if result_status.success? + puts "Models #{name} is now available on #{mount_point_safe}".colorize(:green) + else + puts "Error mounting the vault".colorize(:red) + return + end + end + end +end + diff --git a/src/models/filesystems/concerns/mounted.cr b/src/models/filesystems/concerns/mounted.cr new file mode 100644 index 0000000..a4b8f94 --- /dev/null +++ b/src/models/filesystems/concerns/mounted.cr @@ -0,0 +1,17 @@ +# SPDX-License-Identifier: GPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland +# Copyright © 2023 Glenn Y. Rolland + +module GX::Models::Concerns + module Mounted + def mounted?() : Bool + mount_point_safe = @mount_point + raise InvalidMountpointError.new("Invalid mountpoint value") if mount_point_safe.nil? + + `mount`.includes?(" on #{mount_point_safe} type ") + end + end +end + + diff --git a/src/models/filesystems/concerns/umount.cr b/src/models/filesystems/concerns/umount.cr new file mode 100644 index 0000000..b72e9f8 --- /dev/null +++ b/src/models/filesystems/concerns/umount.cr @@ -0,0 +1,23 @@ +# SPDX-License-Identifier: GPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland +# Copyright © 2023 Glenn Y. Rolland + +module GX::Models::Concerns + module Umount + def umount() : Nil + mount_point_safe = @mount_point + raise InvalidMountpointError.new("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 + end +end + diff --git a/src/models/gocryptfs_config.cr b/src/models/filesystems/gocryptfs_config.cr similarity index 64% rename from src/models/gocryptfs_config.cr rename to src/models/filesystems/gocryptfs_config.cr index 3cbcd61..1434e28 100644 --- a/src/models/gocryptfs_config.cr +++ b/src/models/filesystems/gocryptfs_config.cr @@ -4,14 +4,22 @@ # Copyright © 2023 Glenn Y. Rolland require "shellwords" -require "./abstract_filesystem_config" -require "./concerns/base" +require "./abstract_config" +require "./concerns/mount_wrapper" +require "./concerns/mounted" +require "./concerns/mount" +require "./concerns/umount" +require "./concerns/mount_point" -module GX::Models - class GoCryptFSConfig < AbstractFilesystemConfig +module GX::Models::Filesystems + class GoCryptFSConfig < AbstractConfig getter encrypted_path : String = "" - include Concerns::Base + include Concerns::Mount + include Concerns::Mounted + include Concerns::MountPoint + include Concerns::MountWrapper + include Concerns::Umount def _mounted_prefix() "#{encrypted_path}" diff --git a/src/models/httpdirfs_config.cr b/src/models/filesystems/httpdirfs_config.cr similarity index 62% rename from src/models/httpdirfs_config.cr rename to src/models/filesystems/httpdirfs_config.cr index fdcf012..04ead22 100644 --- a/src/models/httpdirfs_config.cr +++ b/src/models/filesystems/httpdirfs_config.cr @@ -4,14 +4,23 @@ # Copyright © 2023 Glenn Y. Rolland require "shellwords" -require "./abstract_filesystem_config" -require "./concerns/base" -module GX::Models - class HttpDirFSConfig < AbstractFilesystemConfig +require "./abstract_config" +require "./concerns/mount_wrapper" +require "./concerns/mounted" +require "./concerns/mount" +require "./concerns/umount" +require "./concerns/mount_point" + +module GX::Models::Filesystems + class HttpDirFSConfig < AbstractConfig getter url : String = "" - include Concerns::Base + include Concerns::Mount + include Concerns::Mounted + include Concerns::MountPoint + include Concerns::MountWrapper + include Concerns::Umount def _mounted_prefix() "httpdirfs" diff --git a/src/models/sshfs_config.cr b/src/models/filesystems/sshfs_config.cr similarity index 69% rename from src/models/sshfs_config.cr rename to src/models/filesystems/sshfs_config.cr index fee42e5..537a101 100644 --- a/src/models/sshfs_config.cr +++ b/src/models/filesystems/sshfs_config.cr @@ -4,17 +4,25 @@ # Copyright © 2023 Glenn Y. Rolland require "shellwords" -require "./abstract_filesystem_config" -require "./concerns/base" +require "./abstract_config" +require "./concerns/mount_wrapper" +require "./concerns/mounted" +require "./concerns/mount" +require "./concerns/umount" +require "./concerns/mount_point" -module GX::Models - class SshFSConfig < AbstractFilesystemConfig +module GX::Models::Filesystems + class SshFSConfig < AbstractConfig getter remote_path : String = "" getter remote_user : String = "" getter remote_host : String = "" getter remote_port : String = "22" - include Concerns::Base + include Concerns::Mount + include Concerns::Mounted + include Concerns::MountPoint + include Concerns::MountWrapper + include Concerns::Umount def _mounted_prefix() "#{@remote_user}@#{@remote_host}:#{@remote_path}" diff --git a/src/models/global_config.cr b/src/models/global_config.cr index 2e827a7..5eef035 100644 --- a/src/models/global_config.cr +++ b/src/models/global_config.cr @@ -4,7 +4,7 @@ # Copyright © 2023 Glenn Y. Rolland require "yaml" -require "./abstract_filesystem_config" +require "./filesystems/abstract_config" module GX::Models class InvalidEnvironmentError < Exception diff --git a/src/models/root_config.cr b/src/models/root_config.cr index c6de32b..1c1a310 100644 --- a/src/models/root_config.cr +++ b/src/models/root_config.cr @@ -4,7 +4,7 @@ # Copyright © 2023 Glenn Y. Rolland require "yaml" -require "./abstract_filesystem_config" +require "./filesystems/abstract_config" require "./global_config" module GX::Models @@ -35,7 +35,7 @@ module GX::Models getter global : GlobalConfig @[YAML::Field(key: "filesystems")] - getter filesystems : Array(AbstractFilesystemConfig) + getter filesystems : Array(Filesystems::AbstractConfig) end end diff --git a/src/operations/filesystem_create_command.cr b/src/operations/filesystem_create_command.cr deleted file mode 100644 index b041d01..0000000 --- a/src/operations/filesystem_create_command.cr +++ /dev/null @@ -1,12 +0,0 @@ - -require "./abstract_command" - -module GX - module Operations - class FilesystemCreateCommand < AbstractCommand - def execute() - raise "Not Implemented" - end - end - end -end diff --git a/src/operations/filesystem_delete_command.cr b/src/operations/filesystem_delete_command.cr deleted file mode 100644 index 494180c..0000000 --- a/src/operations/filesystem_delete_command.cr +++ /dev/null @@ -1,12 +0,0 @@ - -require "./abstract_command" - -module GX - module Operations - class FilesystemDeleteCommand < AbstractCommand - def execute() - raise "Not Implemented" - end - end - end -end diff --git a/src/operations/filesystem_edit_command.cr b/src/operations/filesystem_edit_command.cr deleted file mode 100644 index 270ace9..0000000 --- a/src/operations/filesystem_edit_command.cr +++ /dev/null @@ -1,12 +0,0 @@ - -require "./abstract_command" - -module GX - module Operations - class FilesystemEditCommand < AbstractCommand - def execute() - raise "Not Implemented" - end - end - end -end diff --git a/src/operations/filesystem_mount_command.cr b/src/operations/filesystem_mount_command.cr deleted file mode 100644 index 944184a..0000000 --- a/src/operations/filesystem_mount_command.cr +++ /dev/null @@ -1,12 +0,0 @@ - -require "./abstract_command" - -module GX - module Operations - class FilesystemMountCommand < AbstractCommand - def execute() - raise "Not Implemented" - end - end - end -end diff --git a/src/operations/filesystem_umount_command.cr b/src/operations/filesystem_umount_command.cr deleted file mode 100644 index fd76741..0000000 --- a/src/operations/filesystem_umount_command.cr +++ /dev/null @@ -1,12 +0,0 @@ - -require "./abstract_command" - -module GX - module Operations - class FilesystemUmountCommand < AbstractCommand - def execute() - raise "Not Implemented" - end - end - end -end diff --git a/src/operations/filesystems/create_command.cr b/src/operations/filesystems/create_command.cr new file mode 100644 index 0000000..8c242c0 --- /dev/null +++ b/src/operations/filesystems/create_command.cr @@ -0,0 +1,14 @@ +# SPDX-License-Identifier: GPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland +# Copyright © 2023 Glenn Y. Rolland + +require "../abstract_command" + +module GX::Operations::Filesystems + class CreateCommand < AbstractCommand + def execute() + raise NotImplementedError.new("CreateCommand is not Implemented") + end + end +end diff --git a/src/operations/filesystems/delete_command.cr b/src/operations/filesystems/delete_command.cr new file mode 100644 index 0000000..495bd2f --- /dev/null +++ b/src/operations/filesystems/delete_command.cr @@ -0,0 +1,14 @@ +# SPDX-License-Identifier: GPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland +# Copyright © 2023 Glenn Y. Rolland + +require "../abstract_command" + +module GX::Operations::Filesystems + class DeleteCommand < AbstractCommand + def execute() + raise NotImplementedError.new("DeleteCommand is not Implemented") + end + end +end diff --git a/src/operations/filesystems/edit_command.cr b/src/operations/filesystems/edit_command.cr new file mode 100644 index 0000000..b31fb84 --- /dev/null +++ b/src/operations/filesystems/edit_command.cr @@ -0,0 +1,14 @@ +# SPDX-License-Identifier: GPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland +# Copyright © 2023 Glenn Y. Rolland + +require "../abstract_command" + +module GX::Operations::Filesystems + class EditCommand < AbstractCommand + def execute() + raise NotImplementedError.new("EditCommand is not Implemented") + end + end +end diff --git a/src/operations/filesystems/list_command.cr b/src/operations/filesystems/list_command.cr new file mode 100644 index 0000000..094f67e --- /dev/null +++ b/src/operations/filesystems/list_command.cr @@ -0,0 +1,15 @@ +# SPDX-License-Identifier: GPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland +# Copyright © 2023 Glenn Y. Rolland + +require "../abstract_command" + +module GX::Operations::Filesystems + class ListCommand < AbstractCommand + def execute() + raise NotImplementedError.new("ListCommand is not Implemented") + end + end +end + diff --git a/src/operations/filesystems/mount_command.cr b/src/operations/filesystems/mount_command.cr new file mode 100644 index 0000000..abcef57 --- /dev/null +++ b/src/operations/filesystems/mount_command.cr @@ -0,0 +1,14 @@ +# SPDX-License-Identifier: GPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland +# Copyright © 2023 Glenn Y. Rolland + +require "../abstract_command" + +module GX::Operations::Filesystems + class MountCommand < AbstractCommand + def execute() + raise NotImplementedError.new("MountCommand is not Implemented") + end + end +end diff --git a/src/operations/filesystems/select_command.cr b/src/operations/filesystems/select_command.cr new file mode 100644 index 0000000..b188642 --- /dev/null +++ b/src/operations/filesystems/select_command.cr @@ -0,0 +1,15 @@ +# SPDX-License-Identifier: GPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland +# Copyright © 2023 Glenn Y. Rolland + +require "../abstract_command" + +module GX::Operations::Filesystems + class SelectCommand < AbstractCommand + def execute() + raise NotImplementedError.new("SelectCommand is not Implemented") + end + end +end + diff --git a/src/operations/filesystems/umount_command.cr b/src/operations/filesystems/umount_command.cr new file mode 100644 index 0000000..5841ecb --- /dev/null +++ b/src/operations/filesystems/umount_command.cr @@ -0,0 +1,14 @@ +# SPDX-License-Identifier: GPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland +# Copyright © 2023 Glenn Y. Rolland + +require "../abstract_command" + +module GX::Operations::Filesystems + class UmountCommand < AbstractCommand + def execute() + raise NotImplementedError.new("UmountCommand is not Implemented") + end + end +end diff --git a/src/operations/global/edit_command.cr b/src/operations/global/edit_command.cr new file mode 100644 index 0000000..4633836 --- /dev/null +++ b/src/operations/global/edit_command.cr @@ -0,0 +1,15 @@ +# SPDX-License-Identifier: GPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland +# Copyright © 2023 Glenn Y. Rolland + +require "../abstract_command" + +module GX::Operations::Global + class EditCommand < AbstractCommand + def execute() + raise NotImplementedError.new("EditCommand is not Implemented") + end + end +end +