forked from Seltcana/rootfs-wip
Commands implemented through OptionsParser.
This commit is contained in:
parent
9d4b2adf7b
commit
db8639b893
82
src/main.cr
82
src/main.cr
@ -37,6 +37,19 @@ module Baguette::Base
|
||||
include Baguette::Log
|
||||
end
|
||||
|
||||
class OptionParser
|
||||
def to_s(io : IO) : Nil
|
||||
if banner = @banner
|
||||
io << banner << '\n'
|
||||
end
|
||||
|
||||
@flags.select(&.starts_with? " -").join io, '\n'
|
||||
|
||||
io << "\ncommands:\n"
|
||||
@flags.select{ |x| !x.starts_with? " -" }.join io, '\n'
|
||||
end
|
||||
end
|
||||
|
||||
Colorize.on_tty_only!
|
||||
|
||||
# CAUTION
|
||||
@ -50,71 +63,86 @@ class RootFS::Context
|
||||
@configuration = Baguette::Configuration.new "rootfs"
|
||||
end
|
||||
|
||||
def print_commands_help
|
||||
STDOUT << "commands:" << "\n"
|
||||
STDOUT << " new <template> <rootfs-directory>\n"
|
||||
STDOUT << " save <rootfs-directory> <snapshot-directory>\n"
|
||||
STDOUT << " restore <rootfs-directory> <snapshot-directory>\n"
|
||||
# Other commands are not implemented at the moment.
|
||||
end
|
||||
|
||||
def run
|
||||
options_parser = uninitialized OptionParser
|
||||
|
||||
commands = {
|
||||
"new" => ->(args : Array(String)){
|
||||
:new => ->(args : Array(String)){
|
||||
RootFS.new args[1], args[0]
|
||||
},
|
||||
"save" => ->(args : Array(String)){
|
||||
:save => ->(args : Array(String)){
|
||||
RootFS.load(args[0]).save(args[1])
|
||||
},
|
||||
"restore" => ->(args : Array(String)){
|
||||
:restore => ->(args : Array(String)){
|
||||
RootFS.load(args[0]).restore(args[1])
|
||||
},
|
||||
"mount" => ->(args : Array(String)){
|
||||
:mount => ->(args : Array(String)){
|
||||
RootFS.load(args[0]).mount(args[1]?)
|
||||
},
|
||||
"unmount" => ->(args : Array(String)){
|
||||
:unmount => ->(args : Array(String)){
|
||||
RootFS.load(args[0]).unmount(args[1]?)
|
||||
},
|
||||
# FIXME: This needs access to the list of commands.
|
||||
"help" => ->(args : Array(String)){
|
||||
STDOUT << options_parser << "\n\n"
|
||||
print_commands_help
|
||||
}
|
||||
}
|
||||
|
||||
command = uninitialized String
|
||||
command = nil
|
||||
arguments = uninitialized Array(String)
|
||||
|
||||
options_parser = OptionParser.parse do |parser|
|
||||
parser.banner = "usage: #{@configuration.application_name} <command> [options]"
|
||||
|
||||
parser.on "new", "Creates a new rootfs." do
|
||||
parser.banner = "usage: #{@configuration.application_name} new <template> <rootfs-directory>"
|
||||
|
||||
command = :new
|
||||
end
|
||||
|
||||
parser.on "save", "Creates a snapshot of a rootfs." do
|
||||
parser.banner = "usage: #{@configuration.application_name} save <rootfs-directory> <snapshot-directory>"
|
||||
command = :save
|
||||
end
|
||||
|
||||
parser.on "restore", "Restores a rootfs from a snapshot." do
|
||||
parser.banner = "usage: #{@configuration.application_name} restore <rootfs-directory> <snapshot-directory>"
|
||||
command = :restore
|
||||
end
|
||||
|
||||
parser.on "mount", "Bind-mount a directory in a rootfs." do
|
||||
parser.banner = "usage: #{@configuration.application_name} mount <rootfs-directory> [mount-point]"
|
||||
command = :mount
|
||||
end
|
||||
|
||||
parser.on "unmount", "Unmount a directory from a rootfs." do
|
||||
parser.banner = "usage: #{@configuration.application_name} unmount <rootfs-directory> [mount-point]"
|
||||
command = :unmount
|
||||
end
|
||||
|
||||
parser.on "-c", "--color", "Forces the use of colors." do
|
||||
Colorize.enabled = true
|
||||
end
|
||||
|
||||
parser.on "-h", "--help", "Displays this help message." do
|
||||
STDOUT << parser << "\n\n"
|
||||
print_commands_help
|
||||
puts parser
|
||||
exit 0
|
||||
end
|
||||
|
||||
# FIXME: This leads to unusual behaviors when using the CLI.
|
||||
# Replace by dedicated executables.
|
||||
parser.unknown_args do |args|
|
||||
# args => Array(String)
|
||||
if command.nil?
|
||||
raise "no command provided" if args.size < 1
|
||||
end
|
||||
|
||||
raise "no command provided" if args.size < 1
|
||||
|
||||
command = args.shift
|
||||
arguments = args
|
||||
end
|
||||
end
|
||||
|
||||
if command.nil?
|
||||
puts options_parser
|
||||
exit 1
|
||||
end
|
||||
|
||||
if proc = commands[command]?
|
||||
proc.call(arguments)
|
||||
else
|
||||
# FIXME: This is an internal error. It shouldn’t trigger.
|
||||
raise "unrecognized command: #{command}"
|
||||
end
|
||||
rescue e
|
||||
|
Loading…
Reference in New Issue
Block a user