From 6ec7ae0ec7c8be99c0a7631b723a8ec12ea0ab87 Mon Sep 17 00:00:00 2001 From: Glenn Date: Wed, 24 Jan 2024 14:13:29 +0100 Subject: [PATCH] fix: implement config init --- shard.lock | 4 ++++ shard.yml | 3 +++ src/commands/config_init.cr | 30 ++++++++++++++++++++++++++---- src/file_storage.cr | 8 ++++++++ 4 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 src/file_storage.cr diff --git a/shard.lock b/shard.lock index e19d81c..216e1db 100644 --- a/shard.lock +++ b/shard.lock @@ -4,6 +4,10 @@ shards: git: https://github.com/crystal-ameba/ameba.git version: 1.6.1 + baked_file_system: + git: https://github.com/schovi/baked_file_system.git + version: 0.10.0 + crinja: git: https://github.com/straight-shoota/crinja.git version: 0.8.1 diff --git a/shard.yml b/shard.yml index 3b29118..d65fad5 100644 --- a/shard.yml +++ b/shard.yml @@ -26,6 +26,9 @@ dependencies: github: hugopl/version_from_shard tablo: github: hutou/tablo + baked_file_system: + github: schovi/baked_file_system + version: 0.10.0 development_dependencies: ameba: diff --git a/src/commands/config_init.cr b/src/commands/config_init.cr index 2171d39..07e5eb8 100644 --- a/src/commands/config_init.cr +++ b/src/commands/config_init.cr @@ -1,14 +1,36 @@ require "./abstract_command" +require "../file_storage" module GX::Commands class ConfigInit < AbstractCommand - def initialize(config : GX::Config) # FIXME + def initialize(@config : GX::Config) end def execute - puts "FIXME: detect if config is present" - puts "FIXME: compute config path (either default, or from command line)" - puts "FIXME: create config file from default if needed" + config_dir = File.join(@config.home_dir, ".config", "mfm") + config_file_path = File.join(config_dir, "config.yml") + + # Guard condition to exit if the configuration file already exists + if File.exists?(config_file_path) + puts "Configuration file already exists at #{config_file_path}. No action taken." + return + end + + puts "Creating initial configuration file at #{config_file_path}" + + # Ensure the configuration directory exists + FileUtils.mkdir_p(config_dir) + + # Read the default configuration content from the baked file storage + default_config_content = FileStorage.get("sample.mfm.yaml") + + # Write the default configuration to the target path + File.write(config_file_path, default_config_content) + + puts "Configuration file created successfully." + rescue ex + STDERR.puts "Error creating the configuration file: #{ex.message}" + exit(1) end def self.handles_mode diff --git a/src/file_storage.cr b/src/file_storage.cr new file mode 100644 index 0000000..dd01b59 --- /dev/null +++ b/src/file_storage.cr @@ -0,0 +1,8 @@ +require "baked_file_system" + +class FileStorage + extend BakedFileSystem + + bake_folder "../static" +end +