feat: add base structure for commands
This commit is contained in:
parent
4730c77992
commit
fb168d5308
29 changed files with 286 additions and 151 deletions
|
@ -150,8 +150,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?
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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
|
15
src/models/exceptions.cr
Normal file
15
src/models/exceptions.cr
Normal file
|
@ -0,0 +1,15 @@
|
|||
|
||||
# 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
|
||||
class InvalidFilesystemError < Exception
|
||||
end
|
||||
|
||||
class InvalidMountpointError < Exception
|
||||
end
|
||||
end
|
|
@ -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
|
||||
|
14
src/models/filesystems/concerns/mount.cr
Normal file
14
src/models/filesystems/concerns/mount.cr
Normal file
|
@ -0,0 +1,14 @@
|
|||
# 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>
|
||||
|
||||
module GX::Models::Concerns
|
||||
module Mount
|
||||
def mount()
|
||||
_mount_wrapper() do
|
||||
_mount_action
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
14
src/models/filesystems/concerns/mount_point.cr
Normal file
14
src/models/filesystems/concerns/mount_point.cr
Normal file
|
@ -0,0 +1,14 @@
|
|||
# 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>
|
||||
|
||||
module GX::Models::Concerns
|
||||
module MountPoint
|
||||
def mount_point?()
|
||||
!mount_point.nil?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
29
src/models/filesystems/concerns/mount_wrapper.cr
Normal file
29
src/models/filesystems/concerns/mount_wrapper.cr
Normal file
|
@ -0,0 +1,29 @@
|
|||
# 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>
|
||||
|
||||
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
|
||||
|
17
src/models/filesystems/concerns/mounted.cr
Normal file
17
src/models/filesystems/concerns/mounted.cr
Normal file
|
@ -0,0 +1,17 @@
|
|||
# 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>
|
||||
|
||||
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
|
||||
|
||||
|
23
src/models/filesystems/concerns/umount.cr
Normal file
23
src/models/filesystems/concerns/umount.cr
Normal file
|
@ -0,0 +1,23 @@
|
|||
# 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>
|
||||
|
||||
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
|
||||
|
|
@ -4,14 +4,22 @@
|
|||
# Copyright © 2023 Glenn Y. Rolland <glenux@glenux.net>
|
||||
|
||||
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}"
|
|
@ -4,14 +4,23 @@
|
|||
# Copyright © 2023 Glenn Y. Rolland <glenux@glenux.net>
|
||||
|
||||
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"
|
|
@ -4,17 +4,25 @@
|
|||
# Copyright © 2023 Glenn Y. Rolland <glenux@glenux.net>
|
||||
|
||||
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}"
|
|
@ -4,7 +4,7 @@
|
|||
# Copyright © 2023 Glenn Y. Rolland <glenux@glenux.net>
|
||||
|
||||
require "yaml"
|
||||
require "./abstract_filesystem_config"
|
||||
require "./filesystems/abstract_config"
|
||||
|
||||
module GX::Models
|
||||
class InvalidEnvironmentError < Exception
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
# Copyright © 2023 Glenn Y. Rolland <glenux@glenux.net>
|
||||
|
||||
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
|
||||
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
|
||||
require "./abstract_command"
|
||||
|
||||
module GX
|
||||
module Operations
|
||||
class FilesystemCreateCommand < AbstractCommand
|
||||
def execute()
|
||||
raise "Not Implemented"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,12 +0,0 @@
|
|||
|
||||
require "./abstract_command"
|
||||
|
||||
module GX
|
||||
module Operations
|
||||
class FilesystemDeleteCommand < AbstractCommand
|
||||
def execute()
|
||||
raise "Not Implemented"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,12 +0,0 @@
|
|||
|
||||
require "./abstract_command"
|
||||
|
||||
module GX
|
||||
module Operations
|
||||
class FilesystemEditCommand < AbstractCommand
|
||||
def execute()
|
||||
raise "Not Implemented"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,12 +0,0 @@
|
|||
|
||||
require "./abstract_command"
|
||||
|
||||
module GX
|
||||
module Operations
|
||||
class FilesystemMountCommand < AbstractCommand
|
||||
def execute()
|
||||
raise "Not Implemented"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,12 +0,0 @@
|
|||
|
||||
require "./abstract_command"
|
||||
|
||||
module GX
|
||||
module Operations
|
||||
class FilesystemUmountCommand < AbstractCommand
|
||||
def execute()
|
||||
raise "Not Implemented"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
14
src/operations/filesystems/create_command.cr
Normal file
14
src/operations/filesystems/create_command.cr
Normal file
|
@ -0,0 +1,14 @@
|
|||
# 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 "../abstract_command"
|
||||
|
||||
module GX::Operations::Filesystems
|
||||
class CreateCommand < AbstractCommand
|
||||
def execute()
|
||||
raise NotImplementedError.new("CreateCommand is not Implemented")
|
||||
end
|
||||
end
|
||||
end
|
14
src/operations/filesystems/delete_command.cr
Normal file
14
src/operations/filesystems/delete_command.cr
Normal file
|
@ -0,0 +1,14 @@
|
|||
# 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 "../abstract_command"
|
||||
|
||||
module GX::Operations::Filesystems
|
||||
class DeleteCommand < AbstractCommand
|
||||
def execute()
|
||||
raise NotImplementedError.new("DeleteCommand is not Implemented")
|
||||
end
|
||||
end
|
||||
end
|
14
src/operations/filesystems/edit_command.cr
Normal file
14
src/operations/filesystems/edit_command.cr
Normal file
|
@ -0,0 +1,14 @@
|
|||
# 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 "../abstract_command"
|
||||
|
||||
module GX::Operations::Filesystems
|
||||
class EditCommand < AbstractCommand
|
||||
def execute()
|
||||
raise NotImplementedError.new("EditCommand is not Implemented")
|
||||
end
|
||||
end
|
||||
end
|
15
src/operations/filesystems/list_command.cr
Normal file
15
src/operations/filesystems/list_command.cr
Normal file
|
@ -0,0 +1,15 @@
|
|||
# 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 "../abstract_command"
|
||||
|
||||
module GX::Operations::Filesystems
|
||||
class ListCommand < AbstractCommand
|
||||
def execute()
|
||||
raise NotImplementedError.new("ListCommand is not Implemented")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
14
src/operations/filesystems/mount_command.cr
Normal file
14
src/operations/filesystems/mount_command.cr
Normal file
|
@ -0,0 +1,14 @@
|
|||
# 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 "../abstract_command"
|
||||
|
||||
module GX::Operations::Filesystems
|
||||
class MountCommand < AbstractCommand
|
||||
def execute()
|
||||
raise NotImplementedError.new("MountCommand is not Implemented")
|
||||
end
|
||||
end
|
||||
end
|
15
src/operations/filesystems/select_command.cr
Normal file
15
src/operations/filesystems/select_command.cr
Normal file
|
@ -0,0 +1,15 @@
|
|||
# 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 "../abstract_command"
|
||||
|
||||
module GX::Operations::Filesystems
|
||||
class SelectCommand < AbstractCommand
|
||||
def execute()
|
||||
raise NotImplementedError.new("SelectCommand is not Implemented")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
14
src/operations/filesystems/umount_command.cr
Normal file
14
src/operations/filesystems/umount_command.cr
Normal file
|
@ -0,0 +1,14 @@
|
|||
# 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 "../abstract_command"
|
||||
|
||||
module GX::Operations::Filesystems
|
||||
class UmountCommand < AbstractCommand
|
||||
def execute()
|
||||
raise NotImplementedError.new("UmountCommand is not Implemented")
|
||||
end
|
||||
end
|
||||
end
|
15
src/operations/global/edit_command.cr
Normal file
15
src/operations/global/edit_command.cr
Normal file
|
@ -0,0 +1,15 @@
|
|||
# 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 "../abstract_command"
|
||||
|
||||
module GX::Operations::Global
|
||||
class EditCommand < AbstractCommand
|
||||
def execute()
|
||||
raise NotImplementedError.new("EditCommand is not Implemented")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in a new issue