From 4d4c248ec2d3a4e06fd881338b3225de976d660f Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Wed, 25 Nov 2020 22:01:45 +0100 Subject: [PATCH] Initial commit. --- project.zsh | 9 ++++ shard.yml | 14 +++++ src/main.cr | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 project.zsh create mode 100644 shard.yml create mode 100644 src/main.cr diff --git a/project.zsh b/project.zsh new file mode 100644 index 0000000..c7bc89b --- /dev/null +++ b/project.zsh @@ -0,0 +1,9 @@ + +package=(rootfs) +version=0.1 + +targets=(rootfs) + +type[rootfs]=crystal +sources[rootfs]=src/main.cr + diff --git a/shard.yml b/shard.yml new file mode 100644 index 0000000..7af0b96 --- /dev/null +++ b/shard.yml @@ -0,0 +1,14 @@ +name: rootfs +version: 0.1.0 + +authors: + - Luka Vandervelden + +description: | + RootFS management and snapshotting helper. + +dependencies: + baguette-crystal-base: + git: https://git.baguette.netlib.re/Baguette/baguette-crystal-base + +# license: MIT diff --git a/src/main.cr b/src/main.cr new file mode 100644 index 0000000..68f73a0 --- /dev/null +++ b/src/main.cr @@ -0,0 +1,146 @@ +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} [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 +