From 9010a55eb6fda5a09e0e1af8f14a8c0feb48a7b9 Mon Sep 17 00:00:00 2001 From: Karchnu Date: Sun, 13 Dec 2020 02:03:04 +0100 Subject: [PATCH] Configuration parameter replaced by --project. --- src/baguette-crystal-base.cr | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/baguette-crystal-base.cr b/src/baguette-crystal-base.cr index bb14a46..4c82ee7 100644 --- a/src/baguette-crystal-base.cr +++ b/src/baguette-crystal-base.cr @@ -7,16 +7,22 @@ class Baguette::Context end class Baguette::Configuration + class_property project_directory : String? = nil + class Base include YAML::Serializable - # Check for provided file first, - # then $XDG_CONFIG_HOME/.yml, - # then /etc/baguette/.yml. + # Check for $XDG_CONFIG_HOME/baguette//.yml, + # then /etc/baguette//.yml. + # (project name is not mandatory) def self.get(file : String? = nil) filename = "/" + (self.name.downcase.gsub /baguette::configuration::/, "") + ".yml" directory = "/baguette" + if pdir = Baguette::Configuration.project_directory + directory += "/" + pdir + end + suffix = "#{directory}#{filename}" user_configuration = (ENV["XDG_CONFIG_HOME"]? || "~/.config") + suffix system_configuration = "/etc#{suffix}" @@ -36,6 +42,8 @@ class Baguette::Configuration self.from_yaml File.read(system_configuration) else Baguette::Log.warning "No configuration found" + Baguette::Log.warning "Searched in: #{user_configuration}" + Baguette::Log.warning "Searched in: #{system_configuration}" nil end end @@ -63,7 +71,7 @@ class Baguette::Configuration # Read options from the CLI. # We currently want to know: # - the program verbosity - # - the configuration file to use + # - the configuration directory to use (project name) # - if the configuration files should be ignored # - if this is just a simulation (used to print configuration then quit) def self.option_parser @@ -74,7 +82,7 @@ class Baguette::Configuration help = false OptionParser.parse do |parser| - parser.banner = "usage: #{PROGRAM_NAME} [-ns][-c configuration-file]" + parser.banner = "usage: #{PROGRAM_NAME} [-ns][--project project-name]" parser.on "-s", "--simulation", "Print configuration then quit." do @@ -91,9 +99,9 @@ class Baguette::Configuration Baguette::Context.verbosity = v.to_i end - parser.on "-c file", "--configuration file", - "Configuration file." do |file| - configuration_file = file + parser.on "--project project-name", + "Project name. Will search in $XDG_CONFIG_HOME/baguette//.yml then /etc/baguette//.yml." do |dir| + Baguette::Configuration.project_directory = dir end parser.invalid_option do |arg|