Compare commits
10 commits
b8d5493f1c
...
988e9324ea
| Author | SHA1 | Date | |
|---|---|---|---|
| 988e9324ea | |||
| 69add25ec2 | |||
| a5102c2db0 | |||
| 3119e872c2 | |||
| 9010a55eb6 | |||
| 75924a3412 | |||
| 2ced54cccc | |||
| 69a3326fea | |||
| d3ee43e0ef | |||
| 7c1ab2f3cc |
1 changed files with 105 additions and 13 deletions
|
|
@ -2,22 +2,29 @@ require "colorize"
|
|||
require "yaml"
|
||||
|
||||
class Baguette::Context
|
||||
class_property verbosity = 1
|
||||
# By default, just print everything.
|
||||
class_property verbosity = 4
|
||||
end
|
||||
|
||||
class Baguette::Configuration
|
||||
class_property project_directory : String? = nil
|
||||
|
||||
class Base
|
||||
include YAML::Serializable
|
||||
|
||||
# Check for provided file first,
|
||||
# then $XDG_CONFIG_HOME/<class>.yml,
|
||||
# then /etc/baguette/<class>.yml.
|
||||
# Check for $XDG_CONFIG_HOME/baguette/<project-name>/<class>.yml,
|
||||
# then /etc/baguette/<project-name>/<class>.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
|
||||
user_configuration = (ENV["XDG_CONFIG_HOME"]? || "#{ENV["HOME"]}/.config") + suffix
|
||||
system_configuration = "/etc#{suffix}"
|
||||
|
||||
if ! file.nil? && ! File.exists? file.not_nil!
|
||||
|
|
@ -30,15 +37,91 @@ class Baguette::Configuration
|
|||
elsif File.exists? user_configuration
|
||||
Baguette::Log.info "Using user configuration: #{user_configuration}"
|
||||
self.from_yaml File.read(user_configuration)
|
||||
elsif File.exists? user_configuration
|
||||
elsif File.exists? system_configuration
|
||||
Baguette::Log.info "Using system configuration: #{system_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
|
||||
end
|
||||
|
||||
class IPC < Base
|
||||
property ipc_timer : Int32 = 30_000 # 30 seconds.
|
||||
|
||||
property print_ipc_timer : Bool = false
|
||||
property print_ipc_connection : Bool = false
|
||||
property print_ipc_disconnection : Bool = false
|
||||
property print_ipc_extra_socket : Bool = false
|
||||
property print_ipc_message_received : Bool = false
|
||||
property print_ipc_message_sent : Bool = false
|
||||
property print_ipc_switch : Bool = false
|
||||
property print_ipc_error : Bool = true
|
||||
property print_ipc_exception : Bool = true
|
||||
property print_keepalive : Bool = false
|
||||
|
||||
property verbosity : Int32 = 4
|
||||
|
||||
def initialize
|
||||
end
|
||||
end
|
||||
|
||||
# Read options from the CLI.
|
||||
# We currently want to know:
|
||||
# - the program verbosity
|
||||
# - 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
|
||||
simulation = false
|
||||
no_configuration = false
|
||||
configuration_file = nil
|
||||
|
||||
help = false
|
||||
|
||||
OptionParser.parse do |parser|
|
||||
parser.banner = "usage: #{PROGRAM_NAME} [-ns][--project project-name]"
|
||||
|
||||
parser.on "-s", "--simulation",
|
||||
"Print configuration then quit." do
|
||||
simulation = true
|
||||
end
|
||||
|
||||
parser.on "-n", "--no-configuration",
|
||||
"No configuration file should be read." do
|
||||
no_configuration = true
|
||||
end
|
||||
|
||||
parser.on "-v verbosity", "--verbosity level",
|
||||
"Verbosity level. From 0 to 4. Default: 4" do |v|
|
||||
Baguette::Context.verbosity = v.to_i
|
||||
end
|
||||
|
||||
parser.on "--project project-name",
|
||||
"Project name. Will search in $XDG_CONFIG_HOME/baguette/<project-name>/<class>.yml then /etc/baguette/<project-name>/<class>.yml." do |dir|
|
||||
Baguette::Configuration.project_directory = dir
|
||||
end
|
||||
|
||||
parser.invalid_option do |arg|
|
||||
# Do not print anything: we only check for configuration file stuff.
|
||||
end
|
||||
|
||||
parser.on "-h", "--help", "Show this help" do
|
||||
Baguette::Log.warning "for the first option parsing!"
|
||||
puts parser
|
||||
help = true
|
||||
end
|
||||
end
|
||||
|
||||
# Options are removed once read from the ARGV array, but we want to propagate
|
||||
# this particular option to print the second set of options.
|
||||
ARGV.push "-h" if help
|
||||
|
||||
return simulation, no_configuration, configuration_file
|
||||
end
|
||||
end
|
||||
|
||||
class Baguette::Log
|
||||
|
|
@ -46,12 +129,15 @@ class Baguette::Log
|
|||
# FIXME: def log(), that puts stuff as-is in the logs.
|
||||
|
||||
def self.debug(text)
|
||||
return unless Baguette::Context.verbosity > 2
|
||||
STDERR.puts ":: #{text}".colorize(:cyan)
|
||||
return unless Baguette::Context.verbosity > 3
|
||||
STDERR
|
||||
.<<(":: ".colorize(:cyan))
|
||||
.<<(text.colorize(:cyan))
|
||||
.<<("\n")
|
||||
STDERR.flush
|
||||
end
|
||||
def self.info(text)
|
||||
return unless Baguette::Context.verbosity > 1
|
||||
return unless Baguette::Context.verbosity > 2
|
||||
STDOUT
|
||||
.<<(":: ".colorize(:blue))
|
||||
.<<(text.colorize(:white))
|
||||
|
|
@ -59,7 +145,7 @@ class Baguette::Log
|
|||
STDOUT.flush
|
||||
end
|
||||
def self.title(text)
|
||||
return unless Baguette::Context.verbosity > 1
|
||||
return unless Baguette::Context.verbosity > 2
|
||||
STDOUT
|
||||
.<<("|> ".colorize(:blue).bright)
|
||||
.<<(text.colorize(:white).bright)
|
||||
|
|
@ -67,13 +153,19 @@ class Baguette::Log
|
|||
STDOUT.flush
|
||||
end
|
||||
def self.warning(text)
|
||||
return unless Baguette::Context.verbosity > 0
|
||||
STDERR.puts ":: #{text}".colorize(:yellow)
|
||||
return unless Baguette::Context.verbosity > 1
|
||||
STDERR
|
||||
.<<(":: ".colorize(:yellow).bright)
|
||||
.<<(text.colorize(:yellow))
|
||||
.<<("\n")
|
||||
STDERR.flush
|
||||
end
|
||||
def self.error(text)
|
||||
return unless Baguette::Context.verbosity > 0
|
||||
STDERR.puts "!! #{text}".colorize(:red)
|
||||
STDERR
|
||||
.<<("!! ".colorize(:red).bright)
|
||||
.<<(text.colorize(:red))
|
||||
.<<("\n")
|
||||
STDERR.flush
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue