Commands implemented through OptionsParser.

remotes/1729829410320275842/tmp_refs/heads/master
Luka Vandervelden 2020-12-15 10:06:13 +01:00
parent 9d4b2adf7b
commit db8639b893
1 changed files with 55 additions and 27 deletions

View File

@ -37,6 +37,19 @@ module Baguette::Base
include Baguette::Log include Baguette::Log
end 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! Colorize.on_tty_only!
# CAUTION # CAUTION
@ -50,71 +63,86 @@ class RootFS::Context
@configuration = Baguette::Configuration.new "rootfs" @configuration = Baguette::Configuration.new "rootfs"
end 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 def run
options_parser = uninitialized OptionParser options_parser = uninitialized OptionParser
commands = { commands = {
"new" => ->(args : Array(String)){ :new => ->(args : Array(String)){
RootFS.new args[1], args[0] RootFS.new args[1], args[0]
}, },
"save" => ->(args : Array(String)){ :save => ->(args : Array(String)){
RootFS.load(args[0]).save(args[1]) RootFS.load(args[0]).save(args[1])
}, },
"restore" => ->(args : Array(String)){ :restore => ->(args : Array(String)){
RootFS.load(args[0]).restore(args[1]) RootFS.load(args[0]).restore(args[1])
}, },
"mount" => ->(args : Array(String)){ :mount => ->(args : Array(String)){
RootFS.load(args[0]).mount(args[1]?) RootFS.load(args[0]).mount(args[1]?)
}, },
"unmount" => ->(args : Array(String)){ :unmount => ->(args : Array(String)){
RootFS.load(args[0]).unmount(args[1]?) 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) arguments = uninitialized Array(String)
options_parser = OptionParser.parse do |parser| options_parser = OptionParser.parse do |parser|
parser.banner = "usage: #{@configuration.application_name} <command> [options]" 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 parser.on "-c", "--color", "Forces the use of colors." do
Colorize.enabled = true Colorize.enabled = true
end end
parser.on "-h", "--help", "Displays this help message." do parser.on "-h", "--help", "Displays this help message." do
STDOUT << parser << "\n\n" puts parser
print_commands_help
exit 0 exit 0
end end
# FIXME: This leads to unusual behaviors when using the CLI.
# Replace by dedicated executables.
parser.unknown_args do |args| 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 arguments = args
end end
end end
if command.nil?
puts options_parser
exit 1
end
if proc = commands[command]? if proc = commands[command]?
proc.call(arguments) proc.call(arguments)
else else
# FIXME: This is an internal error. It shouldnt trigger.
raise "unrecognized command: #{command}" raise "unrecognized command: #{command}"
end end
rescue e rescue e