refactor: remove hardcoded filesystem testing

This commit is contained in:
Glenn Y. Rolland 2024-10-27 22:08:58 +01:00
parent ef4ca70eed
commit ad3af05032
11 changed files with 163 additions and 49 deletions

View file

@ -13,51 +13,44 @@ module GX::Commands
end end
def execute def execute
# FIXME: verify that filesystem is valid or return an error # Assuming mapping_create_options is passed to this command with necessary details
create_options = @config.mapping_create_options
# Assuming create_args is passed to this command with necessary details
create_args = @config.create_args
# Validate required arguments # Validate required arguments
if create_args[:name].empty? || create_args[:path].empty? if create_options.nil?
raise ArgumentError.new("Name and path are required to create a mapping.") 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 end
# Create the appropriate filesystem config based on the type # Create the appropriate filesystem config based on the type
filesystem_config = case create_args[:type] filesystem_config = GX::Models::FilesystemFactory.build(create_options)
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
# Append the new filesystem config to the root config # 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 end
def self.handles_mode def self.handles_mode
GX::Types::Mode::MappingCreate GX::Types::Mode::MappingCreate
end 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
end end

View file

@ -37,10 +37,11 @@ module GX
property args : AddArgs.class | DelArgs.class | NoArgs.class property args : AddArgs.class | DelArgs.class | NoArgs.class
property? auto_open : Bool property? auto_open : Bool
# FIXME: refactor and remove these parts from here # TODO: refactor and remove these parts from here
property help_options : Parsers::Options::HelpOptions?
property config_init_options : Parsers::Options::ConfigInitOptions? property config_init_options : Parsers::Options::ConfigInitOptions?
property config_options : Parsers::Options::ConfigOptions? 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? property mapping_create_options : Parsers::Options::MappingCreateOptions?
def initialize def initialize

View file

@ -0,0 +1,21 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#
# SPDX-FileCopyrightText: 2024 Glenn Y. Rolland <glenux@glenux.net>
# Copyright © 2024 Glenn Y. Rolland <glenux@glenux.net>
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

View file

@ -13,6 +13,11 @@ module GX::Models
include Concerns::Base 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 def _mounted_prefix
"#{encrypted_path}" "#{encrypted_path}"
end end
@ -34,5 +39,6 @@ module GX::Models
) )
process.wait process.wait
end end
def self.name ; "gocryptfs" ; end
end end
end end

View file

@ -13,6 +13,11 @@ module GX::Models
include Concerns::Base include Concerns::Base
def initialize(create_options)
@name = create_options.name.as(String)
@url = create_options.url.as(String)
end
def _mounted_prefix def _mounted_prefix
"httpdirfs" "httpdirfs"
end end
@ -34,5 +39,6 @@ module GX::Models
) )
process.wait process.wait
end end
def self.name ; "httpdirfs" ; end
end end
end end

View file

@ -16,6 +16,14 @@ module GX::Models
include Concerns::Base 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 def _mounted_prefix
"#{@remote_user}@#{@remote_host}:#{@remote_path}" "#{@remote_user}@#{@remote_host}:#{@remote_path}"
end end
@ -41,5 +49,6 @@ module GX::Models
) )
process.wait process.wait
end end
def self.name ; "sshfs" ; end
end end
end end

View file

@ -10,7 +10,6 @@ module GX::Parsers
class MappingParser < AbstractParser class MappingParser < AbstractParser
def build(parser, ancestors, config) def build(parser, ancestors, config)
breadcrumbs = ancestors + "mapping" breadcrumbs = ancestors + "mapping"
create_args = {name: "", path: ""}
delete_args = {name: ""} delete_args = {name: ""}
mount_args = {name: ""} mount_args = {name: ""}
umount_args = {name: ""} umount_args = {name: ""}
@ -29,35 +28,53 @@ module GX::Parsers
parser.on("create", "Create mapping") do parser.on("create", "Create mapping") do
config.mode = Types::Mode::MappingCreate 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.banner = Utils.usage_line(breadcrumbs + "create", "Create mapping", true)
parser.separator("\nCreate options") parser.separator("\nCreate options")
parser.on("-t", "--type TYPE", "Set filesystem type") do |type| 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 NAME", "Set mapping name") do |name|
config.mapping_create_options.try do |opts|
opts.name = name
end end
parser.on("-n", "--name", "Set mapping name") do |name|
create_args = create_args.merge({name: name})
end end
# Filesystem specific # Filesystem specific
parser.on("--encrypted-path PATH", "Set encrypted path (for gocryptfs)") do |path| 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 end
parser.on("--remote-user USER", "Set SSH user (for sshfs)") do |user| 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 end
parser.on("--remote-host HOST", "Set SSH host (for sshfs)") do |host| 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 end
parser.on("--source-path PATH", "Set remote path (for sshfs)") do |path| 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 end
parser.on("--remote-port PORT", "Set SSH port (for sshfs)") do |port| 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 end
parser.on("--url URL", "Set URL (for httpdirfs)") do |url| 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 end
parser.separator(Utils.help_line(breadcrumbs + "create")) parser.separator(Utils.help_line(breadcrumbs + "create"))
@ -67,13 +84,19 @@ module GX::Parsers
config.mode = Types::Mode::MappingEdit config.mode = Types::Mode::MappingEdit
parser.on("--remote-user USER", "Set SSH user") do |user| 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 end
parser.on("--remote-host HOST", "Set SSH host") do |host| 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 end
parser.on("--source-path PATH", "Set remote path") do |path| 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 end
parser.separator(Utils.help_line(breadcrumbs + "edit")) parser.separator(Utils.help_line(breadcrumbs + "edit"))

View file

@ -0,0 +1,19 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#
# SPDX-FileCopyrightText: 2024 Glenn Y. Rolland <glenux@glenux.net>
# Copyright © 2024 Glenn Y. Rolland <glenux@glenux.net>
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

View file

@ -0,0 +1,12 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#
# SPDX-FileCopyrightText: 2024 Glenn Y. Rolland <glenux@glenux.net>
# Copyright © 2024 Glenn Y. Rolland <glenux@glenux.net>
require "option_parser"
module GX::Parsers::Options
class MappingDeleteOptions
# Add your options here
end
end

View file

@ -0,0 +1,12 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#
# SPDX-FileCopyrightText: 2024 Glenn Y. Rolland <glenux@glenux.net>
# Copyright © 2024 Glenn Y. Rolland <glenux@glenux.net>
require "option_parser"
module GX::Parsers::Options
class MappingMountOptions
# Add your options here
end
end

View file

@ -0,0 +1,12 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#
# SPDX-FileCopyrightText: 2024 Glenn Y. Rolland <glenux@glenux.net>
# Copyright © 2024 Glenn Y. Rolland <glenux@glenux.net>
require "option_parser"
module GX::Parsers::Options
class MappingUmountOptions
# Add your options here
end
end