Sha256: 6e711c7b0590e12ebdaeed99a14d600542c6baafcad8a1c20f973b9983fd5e41

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true
require "shellwords"

class Pups::Docker
  class << self
    def generate_env_arguments(config)
      output = []
      config&.each do |k, v|
        if !v.to_s.empty?
          output << "--env #{k}=#{escape_user_string_literal(v)}"
        end
      end
      normalize_output(output)
    end

    def generate_link_arguments(config)
      output = []
      config&.each do |c|
        output << "--link #{c["link"]["name"]}:#{c["link"]["alias"]}"
      end
      normalize_output(output)
    end

    def generate_expose_arguments(config)
      output = []
      config&.each do |c|
        if c.to_s.include?(":")
          output << "--publish #{c}"
        else
          output << "--expose #{c}"
        end
      end
      normalize_output(output)
    end

    def generate_volume_arguments(config)
      output = []
      config&.each do |c|
        output << "--volume #{c["volume"]["host"]}:#{c["volume"]["guest"]}"
      end
      normalize_output(output)
    end

    def generate_label_arguments(config)
      output = []
      config&.each do |k, v|
        output << "--label #{k}=#{escape_user_string_literal(v)}"
      end
      normalize_output(output)
    end

    private

    def escape_user_string_literal(str)
      # We need to escape the following strings as they are more likely to contain
      # special characters than any of the other config variables on a Linux system:
      # - the value side of an environment variable
      # - the value side of a label.
      Shellwords.escape(str)
    end

    def normalize_output(output)
      output.empty? ? "" : output.join(" ")
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pups-1.2.1 lib/pups/docker.rb
pups-1.2.0 lib/pups/docker.rb