From f94d0f1f39ac7ae498d76c670e677b0539240728 Mon Sep 17 00:00:00 2001 From: Glenn Date: Sat, 18 Nov 2023 19:32:12 +0100 Subject: [PATCH] fix: add support for global config file Refs: #14 --- src/cli.cr | 3 +++ src/config.cr | 32 ++++++++++++++++++++++++++++---- src/main.cr | 22 ++++++++++++++++++++++ 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/cli.cr b/src/cli.cr index a222201..3c0bcf0 100644 --- a/src/cli.cr +++ b/src/cli.cr @@ -11,6 +11,7 @@ module GX VERSION="v0.1.9" class Cli + Log = ::Log.for("cli") @config : Config @@ -29,10 +30,12 @@ module GX parser.banner = "Usage: #{PROGRAM_NAME} [options]\n\nGlobal options" parser.on("-c", "--config FILE", "Set configuration file") do |path| + Log.info { "Configuration set to #{path}" } @config.path = path end parser.on("-v", "--verbose", "Set more verbosity") do |flag| + Log.info { "Verbosity enabled" } @config.verbose = true end diff --git a/src/config.cr b/src/config.cr index 34df337..f5f6ba0 100644 --- a/src/config.cr +++ b/src/config.cr @@ -7,6 +7,8 @@ require "./filesystems" module GX class Config + Log = ::Log.for("config") + enum Mode ConfigAdd ConfigDelete @@ -26,8 +28,6 @@ module GX property path : String property args : AddArgs.class | DelArgs.class | NoArgs.class - DEFAULT_CONFIG_PATH = "mfm.yml" - def initialize() if !ENV["HOME"]? raise "Home directory not found" @@ -37,15 +37,39 @@ module GX @verbose = false @mode = Mode::Mount @filesystems = [] of Filesystem - @path = File.join(@home_dir, ".config", DEFAULT_CONFIG_PATH) + @path = detect_config_file() + @args = NoArgs end + def detect_config_file() + possible_files = [ + File.join(@home_dir, ".config", "mfm", "config.yaml"), + File.join(@home_dir, ".config", "mfm", "config.yml"), + File.join(@home_dir, ".config", "mfm.yaml"), + File.join(@home_dir, ".config", "mfm.yml"), + File.join("/etc", "mfm", "config.yaml"), + File.join("/etc", "mfm", "config.yml"), + ] + + possible_files.each do |file_path| + if File.exists?(file_path) + Log.info { "Configuration file found: #{file_path}" } + return file_path if File.exists?(file_path) + else + Log.debug { "Configuration file not found: #{file_path}" } + end + end + + Log.error { "No configuration file found in any of the standard locations" } + raise "Configuration file not found" + end + def load_from_file @filesystems = [] of Filesystem if !File.exists? @path - STDERR.puts "Error: file #{@path} does not exist!".colorize(:red) + Log.error { "File #{@path} does not exist!".colorize(:red) } exit(1) end load_filesystems(@path) diff --git a/src/main.cr b/src/main.cr index 43192b0..f32183c 100644 --- a/src/main.cr +++ b/src/main.cr @@ -6,11 +6,33 @@ require "yaml" require "colorize" require "json" +require "log" require "./filesystems/gocryptfs" require "./config" require "./cli" +struct BaseFormat < Log::StaticFormatter + def run + string @entry.severity.label.downcase + string "(" + source + string "): " + message + end +end + +Log.setup do |config| + backend = Log::IOBackend.new(formatter: BaseFormat) + config.bind "*", Log::Severity::Info, backend + + if ENV["LOG_LEVEL"]? + level = Log::Severity.parse(ENV["LOG_LEVEL"]) || Log::Severity::Info + config.bind "*", level, backend + end +end + + app = GX::Cli.new app.parse_command_line(ARGV) app.run