Sha256: f9590656bd1cac25ce86649f3685ddd7034278ab2463ab4233feb51fd06ab856

Contents?: true

Size: 1.31 KB

Versions: 6

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require 'optparse'

module Buildkite
  module Builder
    module Commands
      class Abstract
        class << self
          attr_accessor :description

          def execute
            new.execute
          end
        end

        attr_reader :options

        def initialize
          @options = {}

          parser = OptionParser.new do |opts|
            opts.banner = "Usage: buildkite-builder #{command_name} [OPTIONS] [PIPELINE]"

            opts.on('-h', '--help', 'Prints this help') do
              options[:help] = opts
            end

            parse_options(opts)
          end
          parser.parse!
        end

        def execute
          if options[:help]
            puts options[:help]
            return
          end

          run
        end

        private

        def command_name
          Commands::COMMANDS.key(self.class.name.split('::').last.to_sym)
        end

        def parse_options(opts)
          # noop
          # Subclasses should override to parse options.
        end

        def available_pipelines
          @available_pipelines ||= pipelines_path.children.select(&:directory?).map { |dir| dir.basename.to_s }
        end

        def pipelines_path
          Buildkite::Builder.root.join(Runner::PIPELINES_PATH)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
buildkite-builder-1.0.0 lib/buildkite/builder/commands/abstract.rb
buildkite-builder-1.0.0.beta.5 lib/buildkite/builder/commands/abstract.rb
buildkite-builder-1.0.0.beta.4 lib/buildkite/builder/commands/abstract.rb
buildkite-builder-1.0.0.beta.3 lib/buildkite/builder/commands/abstract.rb
buildkite-builder-1.0.0.beta.2 lib/buildkite/builder/commands/abstract.rb
buildkite-builder-1.0.0.beta.1 lib/buildkite/builder/commands/abstract.rb