Sha256: 3cadf2e83a09746f31f05e8434b056806151360512f727c43e7f4b1e3f7ee32c

Contents?: true

Size: 1.56 KB

Versions: 11

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

module Buildkite
  module Pipelines
    class Command
      BIN_PATH = 'buildkite-agent'
      COMMANDS = %w(
        pipeline
        artifact
        annotate
        meta_data
      )

      class << self
        def pipeline(subcommand, *args)
          new(:pipeline, subcommand, *args).run
        end

        def artifact(subcommand, *args)
          new(:artifact, subcommand, *args).run
        end

        def annotate(body, *args)
          new(:annotate, body, *args).run
        end

        def meta_data(subcommand, *args)
          new(:'meta-data', subcommand, *args).run
        end
      end

      COMMANDS.each do |command|
        define_singleton_method("#{command}!") do |*args|
          abort unless public_send(command, *args)
        end
      end

      def initialize(command, subcommand, *args)
        @command = command.to_s
        @subcommand = subcommand.to_s
        @options = extract_options(args)
        @args = transform_args(args)
      end

      def run
        system(*to_a)
      end

      private

      def to_a
        command = [BIN_PATH, @command, @subcommand]
        command.concat(@options.to_a.flatten)
        command.concat(@args)
      end

      def extract_options(args)
        return {} unless args.first.is_a?(Hash)

        args.shift.tap do |options|
          options.transform_keys! do |key|
            "--#{key.to_s.tr('_', '-')}"
          end
          options.transform_values!(&:to_s)
        end
      end

      def transform_args(args)
        args.map!(&:to_s)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
buildkite-builder-3.5.0 lib/buildkite/pipelines/command.rb
buildkite-builder-3.4.0 lib/buildkite/pipelines/command.rb
buildkite-builder-3.3.2 lib/buildkite/pipelines/command.rb
buildkite-builder-3.3.1 lib/buildkite/pipelines/command.rb
buildkite-builder-3.3.0 lib/buildkite/pipelines/command.rb
buildkite-builder-3.2.0 lib/buildkite/pipelines/command.rb
buildkite-builder-3.1.0 lib/buildkite/pipelines/command.rb
buildkite-builder-3.0.0 lib/buildkite/pipelines/command.rb
buildkite-builder-2.4.1 lib/buildkite/pipelines/command.rb
buildkite-builder-2.4.0 lib/buildkite/pipelines/command.rb
buildkite-builder-2.3.0 lib/buildkite/pipelines/command.rb