From ad3af05032bfd3ab10e028d175dd5497125b65c7 Mon Sep 17 00:00:00 2001 From: "Glenn Y. Rolland" Date: Sun, 27 Oct 2024 22:08:58 +0100 Subject: [PATCH] refactor: remove hardcoded filesystem testing --- src/commands/mapping_create.cr | 59 ++++++++----------- src/config.cr | 5 +- src/models/filesystem_factory.cr | 21 +++++++ src/models/gocryptfs_config.cr | 6 ++ src/models/httpdirfs_config.cr | 6 ++ src/models/sshfs_config.cr | 9 +++ src/parsers/mapping_parser.cr | 51 +++++++++++----- src/parsers/options/mapping_create_options.cr | 19 ++++++ src/parsers/options/mapping_delete_options.cr | 12 ++++ src/parsers/options/mapping_mount_options.cr | 12 ++++ src/parsers/options/mapping_umount_options.cr | 12 ++++ 11 files changed, 163 insertions(+), 49 deletions(-) create mode 100644 src/models/filesystem_factory.cr create mode 100644 src/parsers/options/mapping_create_options.cr create mode 100644 src/parsers/options/mapping_delete_options.cr create mode 100644 src/parsers/options/mapping_mount_options.cr create mode 100644 src/parsers/options/mapping_umount_options.cr diff --git a/src/commands/mapping_create.cr b/src/commands/mapping_create.cr index 54170a0..40dbaa7 100644 --- a/src/commands/mapping_create.cr +++ b/src/commands/mapping_create.cr @@ -13,51 +13,44 @@ module GX::Commands end def execute - # FIXME: verify that filesystem is valid or return an error - - # Assuming create_args is passed to this command with necessary details - create_args = @config.create_args + # Assuming mapping_create_options is passed to this command with necessary details + create_options = @config.mapping_create_options # Validate required arguments - if create_args[:name].empty? || create_args[:path].empty? - raise ArgumentError.new("Name and path are required to create a mapping.") + if create_options.nil? + raise ArgumentError.new("Mapping create options are required") + end + if create_options.name.nil? || create_options.name.try &.empty? + raise ArgumentError.new("Name is required to create a mapping.") + end + if create_options.type.nil? || create_options.type.try &.empty? + raise ArgumentError.new("Type is required to create a mapping.") end # Create the appropriate filesystem config based on the type - filesystem_config = case create_args[:type] - when "gocryptfs" - GX::Models::GocryptfsConfig.new( - name: create_args[:name], - path: create_args[:path], - encrypted_path: create_args[:encrypted_path] - ) - when "sshfs" - GX::Models::SshfsConfig.new( - name: create_args[:name], - path: create_args[:path], - remote_user: create_args[:remote_user], - remote_host: create_args[:remote_host], - remote_path: create_args[:remote_path], - remote_port: create_args[:remote_port] - ) - when "httpdirfs" - GX::Models::HttpdirfsConfig.new( - name: create_args[:name], - path: create_args[:path], - url: create_args[:url] - ) - else - raise ArgumentError.new("Unsupported mapping type: #{create_args[:type]}") - end + filesystem_config = GX::Models::FilesystemFactory.build(create_options) # Append the new filesystem config to the root config - @config.root.try &.filesystems << filesystem_config + @config.root.try do |root| + root.filesystems ||= [] of GX::Models::AbstractFilesystemConfig + root.filesystems << filesystem_config + root.file_system_manager.mount_or_umount(filesystem_config) + end - puts "Mapping '#{create_args[:name]}' created and added to configuration successfully." + puts "Mapping '#{create_options.name}' created and added to configuration successfully." end def self.handles_mode GX::Types::Mode::MappingCreate end + + + # validate create_options.PARAMETER and display error with description if + # missing + macro option_check(create_options, parameter, description) + if create_options.{{ parameter.id }}.nil? || create_options.{{ parameter.id }}.try &.empty? + raise ArgumentError.new("Parameter for " + {{description}} + " is required") + end + end end end diff --git a/src/config.cr b/src/config.cr index 78bbbcc..853e7cc 100644 --- a/src/config.cr +++ b/src/config.cr @@ -37,10 +37,11 @@ module GX property args : AddArgs.class | DelArgs.class | NoArgs.class property? auto_open : Bool - # FIXME: refactor and remove these parts from here - property help_options : Parsers::Options::HelpOptions? + # TODO: refactor and remove these parts from here property config_init_options : Parsers::Options::ConfigInitOptions? property config_options : Parsers::Options::ConfigOptions? + property help_options : Parsers::Options::HelpOptions? + property mapping_create_options : Parsers::Options::MappingCreateOptions? property mapping_create_options : Parsers::Options::MappingCreateOptions? def initialize diff --git a/src/models/filesystem_factory.cr b/src/models/filesystem_factory.cr new file mode 100644 index 0000000..9d43bf5 --- /dev/null +++ b/src/models/filesystem_factory.cr @@ -0,0 +1,21 @@ +# SPDX-License-Identifier: GPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2024 Glenn Y. Rolland +# Copyright © 2024 Glenn Y. Rolland + +module GX::Models + class FilesystemFactory + def self.build(create_options) + case create_options.type + when "gocryptfs" + GoCryptFSConfig.new(create_options) + when "sshfs" + SshFSConfig.new(create_options) + when "httpdirfs" + HttpDirFSConfig.new(create_options) + else + raise ArgumentError.new("Unsupported mapping type: #{create_options.type}") + end + end + end +end diff --git a/src/models/gocryptfs_config.cr b/src/models/gocryptfs_config.cr index f6ea5b0..6e3b497 100644 --- a/src/models/gocryptfs_config.cr +++ b/src/models/gocryptfs_config.cr @@ -13,6 +13,11 @@ module GX::Models include Concerns::Base + def initialize(create_options) + @name = create_options.name.as(String) + @encrypted_path = create_options.encrypted_path.as(String) + end + def _mounted_prefix "#{encrypted_path}" end @@ -34,5 +39,6 @@ module GX::Models ) process.wait end + def self.name ; "gocryptfs" ; end end end diff --git a/src/models/httpdirfs_config.cr b/src/models/httpdirfs_config.cr index bcf0227..9b3f603 100644 --- a/src/models/httpdirfs_config.cr +++ b/src/models/httpdirfs_config.cr @@ -13,6 +13,11 @@ module GX::Models include Concerns::Base + def initialize(create_options) + @name = create_options.name.as(String) + @url = create_options.url.as(String) + end + def _mounted_prefix "httpdirfs" end @@ -34,5 +39,6 @@ module GX::Models ) process.wait end + def self.name ; "httpdirfs" ; end end end diff --git a/src/models/sshfs_config.cr b/src/models/sshfs_config.cr index 82b3538..08cf2b0 100644 --- a/src/models/sshfs_config.cr +++ b/src/models/sshfs_config.cr @@ -16,6 +16,14 @@ module GX::Models include Concerns::Base + def initialize(create_options) + @name = create_options.name.as(String) + @remote_user = create_options.remote_user.as(String) + @remote_host = create_options.remote_host.as(String) + @remote_path = create_options.remote_path.as(String) + @remote_port = create_options.remote_port.as(String) + end + def _mounted_prefix "#{@remote_user}@#{@remote_host}:#{@remote_path}" end @@ -41,5 +49,6 @@ module GX::Models ) process.wait end + def self.name ; "sshfs" ; end end end diff --git a/src/parsers/mapping_parser.cr b/src/parsers/mapping_parser.cr index 7c7365f..8c4a1d1 100644 --- a/src/parsers/mapping_parser.cr +++ b/src/parsers/mapping_parser.cr @@ -10,7 +10,6 @@ module GX::Parsers class MappingParser < AbstractParser def build(parser, ancestors, config) breadcrumbs = ancestors + "mapping" - create_args = {name: "", path: ""} delete_args = {name: ""} mount_args = {name: ""} umount_args = {name: ""} @@ -29,35 +28,53 @@ module GX::Parsers parser.on("create", "Create mapping") do config.mode = Types::Mode::MappingCreate - # pp parser + config.mode = Types::Mode::MappingCreate + config.mapping_create_options = Parsers::Options::MappingCreateOptions.new + parser.banner = Utils.usage_line(breadcrumbs + "create", "Create mapping", true) parser.separator("\nCreate options") parser.on("-t", "--type TYPE", "Set filesystem type") do |type| - create_args = create_args.merge({type: type}) + config.mapping_create_options.try do |opts| + opts.type = type + end end - parser.on("-n", "--name", "Set mapping name") do |name| - create_args = create_args.merge({name: name}) + parser.on("-n", "--name NAME", "Set mapping name") do |name| + config.mapping_create_options.try do |opts| + opts.name = name + end end # Filesystem specific parser.on("--encrypted-path PATH", "Set encrypted path (for gocryptfs)") do |path| - create_args = create_args.merge({encrypted_path: path}) + config.mapping_create_options.try do |opts| + opts.encrypted_path = path + end end parser.on("--remote-user USER", "Set SSH user (for sshfs)") do |user| - create_args = create_args.merge({remote_user: user}) + config.mapping_create_options.try do |opts| + opts.remote_user = user + end end parser.on("--remote-host HOST", "Set SSH host (for sshfs)") do |host| - create_args = create_args.merge({remote_host: host}) + config.mapping_create_options.try do |opts| + opts.remote_host = host + end end parser.on("--source-path PATH", "Set remote path (for sshfs)") do |path| - create_args = create_args.merge({remote_path: path}) + config.mapping_create_options.try do |opts| + opts.remote_path = path + end end parser.on("--remote-port PORT", "Set SSH port (for sshfs)") do |port| - create_args = create_args.merge({remote_port: port}) + config.mapping_create_options.try do |opts| + opts.remote_port = port + end end parser.on("--url URL", "Set URL (for httpdirfs)") do |url| - create_args = create_args.merge({url: url}) + config.mapping_create_options.try do |opts| + opts.url = url + end end parser.separator(Utils.help_line(breadcrumbs + "create")) @@ -67,13 +84,19 @@ module GX::Parsers config.mode = Types::Mode::MappingEdit parser.on("--remote-user USER", "Set SSH user") do |user| - create_args = create_args.merge({remote_user: user}) + config.mapping_create_options.try do |opts| + opts.remote_user = user + end end parser.on("--remote-host HOST", "Set SSH host") do |host| - create_args = create_args.merge({remote_host: host}) + config.mapping_create_options.try do |opts| + opts.remote_host = host + end end parser.on("--source-path PATH", "Set remote path") do |path| - create_args = create_args.merge({remote_path: path}) + config.mapping_create_options.try do |opts| + opts.remote_path = path + end end parser.separator(Utils.help_line(breadcrumbs + "edit")) diff --git a/src/parsers/options/mapping_create_options.cr b/src/parsers/options/mapping_create_options.cr new file mode 100644 index 0000000..1137777 --- /dev/null +++ b/src/parsers/options/mapping_create_options.cr @@ -0,0 +1,19 @@ +# SPDX-License-Identifier: GPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2024 Glenn Y. Rolland +# Copyright © 2024 Glenn Y. Rolland + +require "option_parser" + +module GX::Parsers::Options + class MappingCreateOptions + property type : String? + property name : String? + property encrypted_path : String? + property remote_user : String? + property remote_host : String? + property remote_path : String? + property remote_port : String? + property url : String? + end +end diff --git a/src/parsers/options/mapping_delete_options.cr b/src/parsers/options/mapping_delete_options.cr new file mode 100644 index 0000000..c881f24 --- /dev/null +++ b/src/parsers/options/mapping_delete_options.cr @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2024 Glenn Y. Rolland +# Copyright © 2024 Glenn Y. Rolland + +require "option_parser" + +module GX::Parsers::Options + class MappingDeleteOptions + # Add your options here + end +end diff --git a/src/parsers/options/mapping_mount_options.cr b/src/parsers/options/mapping_mount_options.cr new file mode 100644 index 0000000..c5cf059 --- /dev/null +++ b/src/parsers/options/mapping_mount_options.cr @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2024 Glenn Y. Rolland +# Copyright © 2024 Glenn Y. Rolland + +require "option_parser" + +module GX::Parsers::Options + class MappingMountOptions + # Add your options here + end +end diff --git a/src/parsers/options/mapping_umount_options.cr b/src/parsers/options/mapping_umount_options.cr new file mode 100644 index 0000000..df948e9 --- /dev/null +++ b/src/parsers/options/mapping_umount_options.cr @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2024 Glenn Y. Rolland +# Copyright © 2024 Glenn Y. Rolland + +require "option_parser" + +module GX::Parsers::Options + class MappingUmountOptions + # Add your options here + end +end