./share/scripts/toolchain in docker-utils-0.1.7 vs ./share/scripts/toolchain in docker-utils-0.1.8

- old
+ new

@@ -1,47 +1,79 @@ #!/usr/bin/env ruby -toolchain_image = 'bexio/toolchain:latest' -toolchain_subcommand_args = ARGV +EXPUNGE_ENV_KEYS = [ + 'TMPDIR' +] +require 'optparse' 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 +options = {} +options[:toolchain_image] = 'bexio/toolchain:latest' + +toolchain_docker_args = [] +toolchain_subcommand_args = ARGV[0..-1] +ARGV.clear + +if subcommand_delim_pos = toolchain_subcommand_args.index('--') + toolchain_own_args, toolchain_subcommand_args = toolchain_subcommand_args.partition.each_with_index{ |x, i| i <= subcommand_delim_pos } + ARGV.concat(toolchain_own_args[0..-2]) end -host_repo_root = Pathname.new(host_repo_root) -host_cwd = Pathname.pwd +OptionParser.new do |opts| + opts.on("-i", "--image", "Run within the specified toolchain image") do |image_name| + options[:toolchain_image] = image_name + end -rel_cwd = host_cwd.relative_path_from(host_repo_root) + opts.on("-b", "--[no-]bind-net", "Run the command in the host's network namespace") do |v| + options[:bind_net] = v + end +end.parse! -guest_repo_root = Pathname.new('/app') -guest_cwd = guest_repo_root + rel_cwd +toolchain_image = (options[:toolchain_image].split(':') + ['latest'])[0, 2].join(':') toolchain_available = system 'docker', 'inspect', '-f', '{{.Id}}', toolchain_image, out: '/dev/null', err: '/dev/null' -unless toolchain_available - system 'docker', 'pull', toolchain_image +unless toolchain_available or system('docker', 'pull', toolchain_image) + $stderr.puts "could not locate toolchain image '#{toolchain_image}'; aborting" + Kernel.exit 1 end -expunge_env_keys = [ - 'TMPDIR' -] -host_env_keys = ENV.keys + +# mount options +host_cwd = Pathname.pwd +host_repo_root = `git rev-parse --show-toplevel 2>/dev/null`.chomp +host_repo_root = nil if host_repo_root.empty? +if host_repo_root + host_repo_root = Pathname.new(host_repo_root) + rel_cwd = host_cwd.relative_path_from(host_repo_root) + guest_repo_root = Pathname.new('/src') + guest_cwd = guest_repo_root + rel_cwd + + toolchain_docker_args.concat([ + '-v', "#{host_repo_root}:#{guest_repo_root}", + '-w', guest_cwd.to_s + ]) +end + +# interactivity options +if $stdin.tty? and $stdout.tty? + toolchain_docker_args.concat(['-i', '-t']) +end + +# environment options +exposed_env_keys = ENV.keys - EXPUNGE_ENV_KEYS container_env_keys = `docker inspect -f '{{.Config.Env}}' #{toolchain_image}`.chomp[1..-2].split(' ').map{ |pair| pair.split('=').first } +toolchain_docker_args.concat((exposed_env_keys - container_env_keys).map{ |k| ['-e', k] }.flatten) -share_env_keys = host_env_keys - container_env_keys - expunge_env_keys -share_env_args = share_env_keys.map{ |k| ['-e', k] }.flatten +# network options +if options[:bind_net] + toolchain_docker_args.push '--net=host' +end -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_docker_args, toolchain_image, *toolchain_subcommand_args