2024-10-27 20:41:35 +01:00
|
|
|
# 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>
|
|
|
|
|
2024-01-14 20:31:38 +01:00
|
|
|
require "./abstract_command"
|
|
|
|
|
|
|
|
module GX::Commands
|
|
|
|
class MappingCreate < AbstractCommand
|
2024-08-04 22:58:09 +02:00
|
|
|
def initialize(@config : GX::Config) # FIXME
|
|
|
|
@config.load_from_env
|
|
|
|
@config.load_from_file
|
2024-01-14 20:31:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2024-10-25 13:36:08 +02:00
|
|
|
# FIXME: verify that filesystem is valid or return an error
|
|
|
|
|
2024-10-25 17:27:36 +02:00
|
|
|
# Assuming create_args is passed to this command with necessary details
|
|
|
|
create_args = @config.create_args
|
|
|
|
|
|
|
|
# Validate required arguments
|
|
|
|
if create_args[:name].empty? || create_args[:path].empty?
|
|
|
|
raise ArgumentError.new("Name and path are 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
|
|
|
|
|
|
|
|
# Append the new filesystem config to the root config
|
|
|
|
@config.root.try &.filesystems << filesystem_config
|
|
|
|
|
|
|
|
puts "Mapping '#{create_args[:name]}' created and added to configuration successfully."
|
2024-01-14 20:31:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.handles_mode
|
|
|
|
GX::Types::Mode::MappingCreate
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|