feat: use Log library instead of puts
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Glenn Y. Rolland 2023-04-25 12:28:16 +02:00
parent a921acc3f9
commit 3f985f2751
12 changed files with 82 additions and 54 deletions

View file

@ -4,4 +4,3 @@ all: build
build:
shards build --error-trace
read A

7
src/build/module.cr Normal file
View file

@ -0,0 +1,7 @@
require "../module"
require "log"
module DocMachine::Build
Log = DocMachine::Log.for("docmachine")
end

View file

@ -2,11 +2,12 @@
require "path"
require "file_utils"
require "./module"
require "./config"
module DocMachine::Build
class Run
Log = DB::Log.for("docmachine.build") # Log for db.pool source
Log = DocMachine::Build::Log.for("run")
def initialize(@config : DocMachine::Build::Config)
@basehash = Digest::SHA256.hexdigest(@config.data_dir)[0..6]
@ -21,9 +22,9 @@ module DocMachine::Build
# create directories
# setup permissions
def prepare()
puts "basedir = #{@config.data_dir}"
puts "docker_image = #{@docker_image}"
puts "action = #{@config.action}"
Log.info { "basedir = #{@config.data_dir}" }
Log.info { "docker_image = #{@docker_image}" }
Log.info { "action = #{@config.action}" }
self._avoid_duplicates()
self._pull_image()
@ -32,8 +33,8 @@ module DocMachine::Build
private def _avoid_duplicates
docker_cid = %x{docker ps -f "name=#{@docker_name}" -q}.strip
puts "docker_name: #{@docker_name}"
puts "docker_cid: #{docker_cid}"
Log.info { "docker_name: #{@docker_name}" }
Log.info { "docker_cid: #{docker_cid}" }
if !docker_cid.empty?
Process.run("docker", ["kill", @docker_name])
@ -41,33 +42,43 @@ module DocMachine::Build
end
def _pull_image
data_cache_dir = (
if ENV["XDG_CACHE_HOME"]?
# FIXME: add option to force update
data_cache_dir = if ENV["XDG_CACHE_HOME"]?
Path[ENV["XDG_CACHE_HOME"], "docmachine"]
else
Path[ENV["HOME"], ".cache", "docmachine"]
else Path[ENV["HOME"], ".cache", "docmachine"]
end
)
data_cache_file = data_cache_dir / "image.tar"
puts "Checking cache #{data_cache_file}..."
Log.info { "Checking cache #{data_cache_file}..." }
if ! File.exists? data_cache_file.to_s
puts "Downloading #{@docker_image} image..."
Log.info { "Downloading #{@docker_image} image..." }
Process.run("docker", ["pull", @docker_image], output: STDOUT)
puts "Building cache for image (#{data_cache_dir})"
Log.info { "Building cache for image (#{data_cache_dir})" }
FileUtils.mkdir_p(data_cache_dir)
Process.run("docker", ["save", @docker_image, "-o", data_cache_file.to_s], output: STDOUT)
puts "done"
Process.run(
"docker",
["image", "save", @docker_image, "-o", data_cache_file.to_s],
output: STDOUT
)
Log.info { "done" }
else
puts "Cache already exist. Skipping."
Log.info { "Cache already exist. Skipping." }
end
Log.info { "Loading #{@docker_image} image from cache..." }
docker_image_loaded = false
Process.run(
"docker",
["image", "load", @docker_image, "-i", data_cache_file.to_s],
output: STDOUT
)
end
def start()
uid = %x{id -u}.strip
gid = %x{id -g}.strip
puts "uid: #{uid}"
puts "cid: #{gid}"
Log.info { "uid: #{uid}" }
Log.info { "cid: #{gid}" }
docker_opts = [] of String
docker_opts << "run"
@ -89,14 +100,14 @@ module DocMachine::Build
if File.exists?("#{@config.data_dir}/.marp/theme.scss")
docker_opt_marp_theme = ["-v", "#{@config.data_dir}/.marp:/app/.marp"]
docker_opts.concat docker_opt_marp_theme
puts "Theme: detected Marp files. Adding option to command line (#{docker_opt_marp_theme})"
Log.info { "Theme: detected Marp files. Adding option to command line (#{docker_opt_marp_theme})" }
else
puts "Theme: no theme detected. Using default files"
Log.info { "Theme: no theme detected. Using default files" }
end
## Detect Mkdocs configuration - old format (full)
if File.exists?("#{@config.data_dir}/mkdocs.yml")
puts "Mkdocs: detected mkdocs.yml file. Please rename to mkdocs-patch.yml"
Log.info { "Mkdocs: detected mkdocs.yml file. Please rename to mkdocs-patch.yml" }
exit 1
end
@ -104,33 +115,33 @@ module DocMachine::Build
if File.exists?("#{@config.data_dir}/mkdocs-patch.yml")
docker_opt_mkdocs_config = ["-v", "#{@config.data_dir}/mkdocs-patch.yml:/app/mkdocs-patch.yml"]
docker_opts.concat docker_opt_mkdocs_config
puts "Mkdocs: detected mkdocs-patch.yml file. Adding option to command line (#{docker_opt_mkdocs_config})"
Log.info { "Mkdocs: detected mkdocs-patch.yml file. Adding option to command line (#{docker_opt_mkdocs_config})" }
else
puts "Mkdocs: no mkdocs-patch.yml detected. Using default files"
Log.info { "Mkdocs: no mkdocs-patch.yml detected. Using default files" }
end
## Detect slides
if Dir.exists?("#{@config.data_dir}/slides")
docker_opt_marp_port = ["-p", "5200:5200"]
docker_opts.concat docker_opt_marp_port
puts "Slides: detected slides directory. Adding option to command line (#{docker_opt_marp_port})"
Log.info { "Slides: detected slides directory. Adding option to command line (#{docker_opt_marp_port})" }
else
puts "Slides: no slides directory detected."
Log.info { "Slides: no slides directory detected." }
end
## Detect docs
if Dir.exists?("#{@config.data_dir}/docs")
docker_opt_marp_port = ["-p", "5100:5100"]
docker_opts.concat docker_opt_marp_port
puts "Slides: detected docs directory. Adding option to command line (#{docker_opt_marp_port})"
Log.info { "Slides: detected docs directory. Adding option to command line (#{docker_opt_marp_port})" }
else
puts "Slides: no slides docs detected."
Log.info { "Slides: no slides docs detected." }
end
docker_opts << @docker_image
docker_opts << @config.action
puts docker_opts.inspect.colorize(:yellow)
Log.info { docker_opts.inspect.colorize(:yellow) }
@process = Process.new("docker", docker_opts, output: STDOUT, error: STDERR)
end
@ -139,7 +150,7 @@ module DocMachine::Build
return if process.nil?
Signal::INT.trap do
STDERR.puts "Received CTRL-C"
Log.warn { "Received CTRL-C" }
process.signal(Signal::KILL)
Process.run("docker", ["kill", @docker_name])
end

View file

@ -33,7 +33,7 @@ module DocMachine
end
opts.on("-h", "--help", "Show this help") do
puts opts
Log.info { opts }
exit
end
@ -47,15 +47,15 @@ module DocMachine
end
parser.parse(args)
Log.info { "commands = #{commands}" }
Log.debug { "commands = #{commands}" }
if commands.size < 1
puts parser.to_s
Log.error { parser.to_s }
Log.error { "ERROR: no command defined" }
end
commands.each do |command|
# puts "== Running #{command}"
# Log.info { "== Running #{command}" }
command.call()
end
end

View file

@ -1,8 +1,8 @@
require "./module"
module DocMachine
Log = ::Log.for("doc_machine")
class Config
property verbose : Bool = false

View file

@ -1,7 +1,10 @@
require "./cli"
Log.setup(:debug)
Log.define_formatter BaseFormat, "#{message}"
::Log.setup(:notice, Log::IOBackend.new(formatter: BaseFormat))
app = DocMachine::Cli.new
app.start(ARGV)

6
src/module.cr Normal file
View file

@ -0,0 +1,6 @@
require "log"
module DocMachine
Log = ::Log.for("docmachine")
end

View file

@ -8,7 +8,9 @@ module DocMachine::Plan
opts.on("plan", "Generate content structure (beta)") do
opts.banner = "Usage: #{PROGRAM_NAME} plan [options]"
opts.on("-t", "--test", "Test") { puts "Test" }
opts.on("-t", "--test", "Test") do
Log.info { "Test" }
end
end
end
end

View file

@ -15,7 +15,7 @@ module DocMachine::Scaffold
commands << ->() : Nil do
if args.size < 1
STDERR.puts "ERROR: No target given!"
Log.error { "ERROR: No target given!" }
exit 1
end
config.target_directory = args[0]

0
src/scaffold/module.cr Normal file
View file

View file

@ -18,11 +18,11 @@ module DocMachine::Scaffold
# Verify parameters
def prepare()
if ! File.directory? @config.target_directory
STDERR.puts "ERROR: target must be a directory"
Log.error { "ERROR: target must be a directory" }
exit 1
end
puts "Target directory: #{@config.target_directory}"
Log.info { "Target directory: #{@config.target_directory}" }
if !@config.force
prompt = Term::Prompt.new
@ -32,25 +32,25 @@ module DocMachine::Scaffold
end
def start()
puts "== Scaffolding #{@config.target_directory}"
Log.info { "== Scaffolding #{@config.target_directory}" }
p = Path.new(@config.target_directory)
cwd = Dir.current
["docs", "slides", "images"].each do |dir|
p_sub = p.join(dir)
puts "-- creating #{p_sub}"
Log.info { "-- creating #{p_sub}" }
FileUtils.mkdir_p(p_sub)
end
["docs", "slides"].each do |dir|
p_sub = p.join(dir)
FileUtils.cd(p_sub)
puts "-- creating link to images in #{p_sub}"
Log.info { "-- creating link to images in #{p_sub}" }
if File.symlink? "images"
FileUtils.rm "images"
end
FileUtils.ln_sf(Path.new("..","images"), Path.new("images"))
FileUtils.cd(cwd)
end
puts "-- creating README.md"
Log.info { "-- creating README.md" }
FileUtils.touch("README.md")
end

View file

@ -18,11 +18,11 @@ module DocMachine::Write
# Verify parameters
def prepare()
if ! File.directory? @config.target_directory
STDERR.puts "ERROR: target must be a directory"
Log.error { "ERROR: target must be a directory" }
exit 1
end
puts "Target directory: #{@config.target_directory}"
Log.info { "Target directory: #{@config.target_directory}" }
if !@config.force
prompt = Term::Prompt.new
@ -32,25 +32,25 @@ module DocMachine::Write
end
def start()
puts "== Writeing #{@config.target_directory}"
Log.info { "== Writeing #{@config.target_directory}" }
p = Path.new(@config.target_directory)
cwd = Dir.current
["docs", "slides", "images"].each do |dir|
p_sub = p.join(dir)
puts "-- creating #{p_sub}"
Log.info { "-- creating #{p_sub}" }
FileUtils.mkdir_p(p_sub)
end
["docs", "slides"].each do |dir|
p_sub = p.join(dir)
FileUtils.cd(p_sub)
puts "-- creating link to images in #{p_sub}"
Log.info { "-- creating link to images in #{p_sub}" }
if File.symlink? "images"
FileUtils.rm "images"
end
FileUtils.ln_sf(Path.new("..","images"), Path.new("images"))
FileUtils.cd(cwd)
end
puts "-- creating README.md"
Log.info { "-- creating README.md" }
FileUtils.touch("README.md")
end