Sha256: 661c0d2fd35e5b2d9019aca73ac2d22b2da9c8fe947c1948e5de2eece5ae7e47

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 KB

Contents

require "open3"

module Utilities
  module Docker
    class Container
      SHOPIFY_BIN_PATH = "/usr/src/app/bin/shopify"

      Error = Class.new(StandardError)

      attr_reader :id, :env, :cwd, :xdg_config_home, :xdg_cache_home

      def initialize(id:, env:, cwd:)
        @id = id
        @cwd = cwd
        @xdg_config_home = File.join(cwd, ".config")
        @xdg_cache_home = File.join(cwd, ".cache")
        @env = env.merge({
          "XDG_CONFIG_HOME" => @xdg_config_home,
          "XDG_CACHE_HOME" => @xdg_cache_home,
        })
      end

      def remove
        _, stderr, stat = Open3.capture3(
          "docker", "rm", "-f", @id
        )
        raise Error, stderr unless stat.success?
      end

      def capture_shopify(*args)
        capture(*([SHOPIFY_BIN_PATH] + args))
      end

      def capture(*args, relative_dir: nil)
        command = ["docker", "exec"]
        cwd = if relative_dir.nil?
          @cwd
        else
          File.join(@cwd, relative_dir)
        end
        command += ["-w", cwd]
        @env.each do |env_name, env_value|
          command += ["--env", "#{env_name}=#{env_value}"]
        end
        command << @id
        command += args

        out, err, stat = Open3.capture3(*command)
        raise Error, err unless stat.success?
        out
      end

      def exec_shopify(*args, relative_dir: nil)
        exec(*([SHOPIFY_BIN_PATH] + args), relative_dir: relative_dir)
      end

      def exec(*args, relative_dir: nil)
        command = ["docker", "exec"]
        cwd = if relative_dir.nil?
          @cwd
        else
          File.join(@cwd, relative_dir)
        end
        command += ["-w", cwd]
        @env.each do |env_name, env_value|
          command += ["--env", "#{env_name}=#{env_value}"]
        end
        command << @id
        command += args

        out, stat = Open3.capture2e(*command)
        raise Error, out unless stat.success?
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shopify-cli-2.6.5 utilities/docker/container.rb
shopify-cli-2.6.4 utilities/docker/container.rb