From a921acc3f9d55ba649bd4de7d1128558b575184d Mon Sep 17 00:00:00 2001 From: "Glenn Y. Rolland" Date: Mon, 24 Apr 2023 18:43:04 +0200 Subject: [PATCH] ci: add missing libreadline-dev --- .drone.yml | 2 +- src/build/run.cr | 33 +++++++++++++++++++++++++++++++++ src/cli.cr | 17 ++++++++--------- src/common/docker.cr | 14 ++++++++++++++ src/config.cr | 3 +++ src/main.cr | 1 + src/write/cli.cr | 6 +++++- src/write/config.cr | 2 ++ 8 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 src/common/docker.cr diff --git a/.drone.yml b/.drone.yml index 087a097..db1e400 100644 --- a/.drone.yml +++ b/.drone.yml @@ -11,7 +11,7 @@ steps: path: /_cache commands: - pwd - - apt-get update && apt-get install -y cmake g++ + - apt-get update && apt-get install -y cmake g++ libreadline-dev - shards install - shards build --production --static - strip bin/docmachine diff --git a/src/build/run.cr b/src/build/run.cr index a7591a0..a2951c2 100644 --- a/src/build/run.cr +++ b/src/build/run.cr @@ -1,8 +1,13 @@ +require "path" +require "file_utils" + require "./config" module DocMachine::Build class Run + Log = DB::Log.for("docmachine.build") # Log for db.pool source + def initialize(@config : DocMachine::Build::Config) @basehash = Digest::SHA256.hexdigest(@config.data_dir)[0..6] @docker_name = "docmachine-#{@basehash}" @@ -20,6 +25,11 @@ module DocMachine::Build puts "docker_image = #{@docker_image}" puts "action = #{@config.action}" + self._avoid_duplicates() + self._pull_image() + end + + private def _avoid_duplicates docker_cid = %x{docker ps -f "name=#{@docker_name}" -q}.strip puts "docker_name: #{@docker_name}" @@ -30,6 +40,29 @@ module DocMachine::Build end end + def _pull_image + data_cache_dir = ( + if ENV["XDG_CACHE_HOME"]? + Path[ENV["XDG_CACHE_HOME"], "docmachine"] + else + Path[ENV["HOME"], ".cache", "docmachine"] + end + ) + + data_cache_file = data_cache_dir / "image.tar" + puts "Checking cache #{data_cache_file}..." + if ! File.exists? data_cache_file.to_s + puts "Downloading #{@docker_image} image..." + Process.run("docker", ["pull", @docker_image], output: STDOUT) + puts "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" + else + puts "Cache already exist. Skipping." + end + end + def start() uid = %x{id -u}.strip gid = %x{id -g}.strip diff --git a/src/cli.cr b/src/cli.cr index 36bfed7..584a64c 100644 --- a/src/cli.cr +++ b/src/cli.cr @@ -47,17 +47,16 @@ module DocMachine end parser.parse(args) - puts commands + Log.info { "commands = #{commands}" } - if commands.size > 0 - commands.each do |command| - # puts "== Running #{command}" - command.call() - end - else + if commands.size < 1 puts parser.to_s - STDOUT.puts "" - STDERR.puts "ERROR: no command defined" + Log.error { "ERROR: no command defined" } + end + + commands.each do |command| + # puts "== Running #{command}" + command.call() end end end diff --git a/src/common/docker.cr b/src/common/docker.cr new file mode 100644 index 0000000..f49d699 --- /dev/null +++ b/src/common/docker.cr @@ -0,0 +1,14 @@ + +class Docker + property image : String + + def initialize(@image) + end + + + def store_image + end + + def image_load + end +end diff --git a/src/config.cr b/src/config.cr index 3de0e98..084ef21 100644 --- a/src/config.cr +++ b/src/config.cr @@ -1,5 +1,8 @@ + module DocMachine + Log = ::Log.for("doc_machine") + class Config property verbose : Bool = false diff --git a/src/main.cr b/src/main.cr index c426b18..9bc515b 100644 --- a/src/main.cr +++ b/src/main.cr @@ -1,6 +1,7 @@ require "./cli" +Log.setup(:debug) app = DocMachine::Cli.new app.start(ARGV) diff --git a/src/write/cli.cr b/src/write/cli.cr index 49cd3b2..d6bc1d7 100644 --- a/src/write/cli.cr +++ b/src/write/cli.cr @@ -2,7 +2,11 @@ require "./config" require "./run" module DocMachine::Write + Log = DocMachine::Log.for("write") + class Cli + Log = DocMachine::Write::Log.for("cli") + def self.add_options(opts, args, parent_config, commands) config = Config.new(parent_config) @@ -15,7 +19,7 @@ module DocMachine::Write 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] diff --git a/src/write/config.cr b/src/write/config.cr index 132f6ef..9adc918 100644 --- a/src/write/config.cr +++ b/src/write/config.cr @@ -2,6 +2,8 @@ module DocMachine::Write class Config + Log = DocMachine::Write.for("config") + property target_directory : String = "." property force : Bool = false