Configuration parameter replaced by --project.

This commit is contained in:
Karchnu 2020-12-13 02:03:04 +01:00
parent 75924a3412
commit 9010a55eb6

View File

@ -7,16 +7,22 @@ class Baguette::Context
end end
class Baguette::Configuration class Baguette::Configuration
class_property project_directory : String? = nil
class Base class Base
include YAML::Serializable include YAML::Serializable
# Check for provided file first, # Check for $XDG_CONFIG_HOME/baguette/<project-name>/<class>.yml,
# then $XDG_CONFIG_HOME/<class>.yml, # then /etc/baguette/<project-name>/<class>.yml.
# then /etc/baguette/<class>.yml. # (project name is not mandatory)
def self.get(file : String? = nil) def self.get(file : String? = nil)
filename = "/" + (self.name.downcase.gsub /baguette::configuration::/, "") + ".yml" filename = "/" + (self.name.downcase.gsub /baguette::configuration::/, "") + ".yml"
directory = "/baguette" directory = "/baguette"
if pdir = Baguette::Configuration.project_directory
directory += "/" + pdir
end
suffix = "#{directory}#{filename}" suffix = "#{directory}#{filename}"
user_configuration = (ENV["XDG_CONFIG_HOME"]? || "~/.config") + suffix user_configuration = (ENV["XDG_CONFIG_HOME"]? || "~/.config") + suffix
system_configuration = "/etc#{suffix}" system_configuration = "/etc#{suffix}"
@ -36,6 +42,8 @@ class Baguette::Configuration
self.from_yaml File.read(system_configuration) self.from_yaml File.read(system_configuration)
else else
Baguette::Log.warning "No configuration found" Baguette::Log.warning "No configuration found"
Baguette::Log.warning "Searched in: #{user_configuration}"
Baguette::Log.warning "Searched in: #{system_configuration}"
nil nil
end end
end end
@ -63,7 +71,7 @@ class Baguette::Configuration
# Read options from the CLI. # Read options from the CLI.
# We currently want to know: # We currently want to know:
# - the program verbosity # - the program verbosity
# - the configuration file to use # - the configuration directory to use (project name)
# - if the configuration files should be ignored # - if the configuration files should be ignored
# - if this is just a simulation (used to print configuration then quit) # - if this is just a simulation (used to print configuration then quit)
def self.option_parser def self.option_parser
@ -74,7 +82,7 @@ class Baguette::Configuration
help = false help = false
OptionParser.parse do |parser| 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", parser.on "-s", "--simulation",
"Print configuration then quit." do "Print configuration then quit." do
@ -91,9 +99,9 @@ class Baguette::Configuration
Baguette::Context.verbosity = v.to_i Baguette::Context.verbosity = v.to_i
end end
parser.on "-c file", "--configuration file", parser.on "--project project-name",
"Configuration file." do |file| "Project name. Will search in $XDG_CONFIG_HOME/baguette/<project-name>/<class>.yml then /etc/baguette/<project-name>/<class>.yml." do |dir|
configuration_file = file Baguette::Configuration.project_directory = dir
end end
parser.invalid_option do |arg| parser.invalid_option do |arg|