#!/usr/bin/env ruby toolchain_image = 'bexio/toolchain:latest' toolchain_subcommand_args = ARGV require 'pathname' host_repo_root = `git rev-parse --show-toplevel 2>/dev/null`.chomp if host_repo_root.empty? $stderr.puts "docker toolchain only operates inside a git repository" Kernel.exit 1 end host_repo_root = Pathname.new(host_repo_root) host_cwd = Pathname.pwd rel_cwd = host_cwd.relative_path_from(host_repo_root) guest_repo_root = Pathname.new('/app') guest_cwd = guest_repo_root + rel_cwd toolchain_available = system 'docker', 'inspect', '-f', '{{.Id}}', toolchain_image, out: '/dev/null', err: '/dev/null' unless toolchain_available system 'docker', 'pull', toolchain_image end expunge_env_keys = [ 'TMPDIR' ] host_env_keys = ENV.keys container_env_keys = `docker inspect -f '{{.Config.Env}}' #{toolchain_image}`.chomp[1..-2].split(' ').map{ |pair| pair.split('=').first } share_env_keys = host_env_keys - container_env_keys - expunge_env_keys share_env_args = share_env_keys.map{ |k| ['-e', k] }.flatten interaction_args = ($stdin.tty? && $stdout.tty?) ? ['-i', '-t'] : [] docker_bin_path = `which docker`.chomp Kernel.exec docker_bin_path, 'run', '--rm', *interaction_args, '-v', "#{host_repo_root}:#{guest_repo_root}", '-w', guest_cwd.to_s, *share_env_args, toolchain_image, *toolchain_subcommand_args