forked from Seltcana/rootfs-wip
147 lines
2.7 KiB
Crystal
147 lines
2.7 KiB
Crystal
|
require "colorize"
|
||
|
require "option_parser"
|
||
|
require "file_utils"
|
||
|
|
||
|
# Not currently used. Waiting for API updates.
|
||
|
#require "baguette-crystal-base"
|
||
|
|
||
|
# Prototyping for the next generation of baguette-crystal-base.
|
||
|
|
||
|
class Baguette::Configuration
|
||
|
property application_name : String
|
||
|
|
||
|
def initialize(@application_name)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
module Baguette::Base
|
||
|
getter configuration : Configuration
|
||
|
|
||
|
def info(text)
|
||
|
STDOUT << (":: ".colorize :blue) << (text.colorize :white) << "\n"
|
||
|
end
|
||
|
|
||
|
def warn(text)
|
||
|
STDERR << (":: ".colorize :yellow) << (text.colorize :yellow) << "\n"
|
||
|
end
|
||
|
|
||
|
def error(text)
|
||
|
STDERR << (":: ".colorize :red) << (text.colorize :white) << "\n"
|
||
|
end
|
||
|
end
|
||
|
|
||
|
Colorize.on_tty_only!
|
||
|
|
||
|
# CAUTION
|
||
|
# backends: storage, ???
|
||
|
# rootfs templates
|
||
|
|
||
|
class RootFS::Context
|
||
|
include Baguette::Base
|
||
|
|
||
|
def initialize
|
||
|
@configuration = Baguette::Configuration.new "rootfs"
|
||
|
end
|
||
|
|
||
|
def run
|
||
|
options_parser = uninitialized OptionParser
|
||
|
|
||
|
commands = {
|
||
|
"new" => ->(args : Array(String)){
|
||
|
}
|
||
|
"help" => ->(args : Array(String)){
|
||
|
STDOUT << options_parser << "\n"
|
||
|
}
|
||
|
}
|
||
|
|
||
|
command = uninitialized String
|
||
|
arguments = uninitialized Array(String)
|
||
|
|
||
|
options_parser = OptionParser.parse do |parser|
|
||
|
parser.banner = "usage: #{@configuration.application_name} <command> [options]"
|
||
|
|
||
|
parser.on "-c", "--color", "Forces the use of colors." do
|
||
|
Colorize.enabled = true
|
||
|
end
|
||
|
|
||
|
# FIXME: This leads to unusual behaviors when using the CLI.
|
||
|
# Replace by dedicated executables.
|
||
|
parser.unknown_args do |args|
|
||
|
# args => Array(String)
|
||
|
|
||
|
raise "no command provided" if args.size < 1
|
||
|
|
||
|
command = args.pop
|
||
|
arguments = args
|
||
|
end
|
||
|
end
|
||
|
|
||
|
if proc = commands[command]?
|
||
|
proc.call(arguments)
|
||
|
else
|
||
|
raise "unrecognized command: #{command}"
|
||
|
end
|
||
|
rescue e
|
||
|
if message = e.message
|
||
|
error message
|
||
|
else
|
||
|
error e.to_s
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
class RootFS::RootFS
|
||
|
getter directory : String
|
||
|
|
||
|
def initialize(@directory)
|
||
|
end
|
||
|
|
||
|
def self.new(directory, template : String)
|
||
|
instance = RootFS.allocate
|
||
|
instance.initialize directory
|
||
|
instance.create! template
|
||
|
instance
|
||
|
end
|
||
|
|
||
|
protected def create!(template : String)
|
||
|
show!
|
||
|
puts "UNIMPLEMENTED: creating rootfs here"
|
||
|
end
|
||
|
|
||
|
def self.load(directory)
|
||
|
new directory, nil
|
||
|
end
|
||
|
|
||
|
def delete
|
||
|
show!
|
||
|
puts "UNIMPLEMENTED: removing rootfs directory here"
|
||
|
end
|
||
|
|
||
|
def save
|
||
|
show!
|
||
|
puts "UNIMPLEMENTED: updating snapshot here"
|
||
|
end
|
||
|
|
||
|
def restore
|
||
|
show!
|
||
|
puts "UNIMPLEMENTED: restoring from snapshot here"
|
||
|
end
|
||
|
|
||
|
def mount
|
||
|
show!
|
||
|
puts "UNIMPLEMENTED: binding file systems here"
|
||
|
end
|
||
|
|
||
|
def unmount
|
||
|
show!
|
||
|
puts "UNIMPLEMENTED: unbinding file systems here"
|
||
|
end
|
||
|
|
||
|
def show!
|
||
|
puts "#{@template}, #{@directory}"
|
||
|
end
|
||
|
end
|
||
|
|
||
|
RootFS::Context.new.run
|
||
|
|