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

View file

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

View file

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

View file

@ -1,7 +1,10 @@
require "./cli" require "./cli"
Log.setup(:debug) Log.define_formatter BaseFormat, "#{message}"
::Log.setup(:notice, Log::IOBackend.new(formatter: BaseFormat))
app = DocMachine::Cli.new app = DocMachine::Cli.new
app.start(ARGV) 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.on("plan", "Generate content structure (beta)") do
opts.banner = "Usage: #{PROGRAM_NAME} plan [options]" 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 end
end end

View file

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

View file

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