Sha256: c04288a36282c31b19a1c45ccfb90ea29d7f0926e3f302e50938b25d8d418fe0

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

require "milestoner"

module Rubysmith
  module CLI
    # The main Command Line Interface (CLI) object.
    class Shell
      ACTIONS = {
        config: Actions::Config.new,
        build: Actions::Build.new,
        publish: Actions::Publish.new
      }.freeze

      def initialize parser: Parser.new, actions: ACTIONS, container: Container
        @parser = parser
        @actions = actions
        @container = container
      end

      def call arguments = []
        perform parser.call(arguments)
      rescue OptionParser::ParseError, Milestoner::Error => error
        logger.error { error.message }
      end

      private

      attr_reader :parser, :actions, :container

      def perform configuration
        case configuration
          in action_config: Symbol => action then config action
          in action_build: true then build configuration
          in action_publish: true then publish configuration
          in action_version: true then logger.info { "Rubysmith #{specification.version}" }
          else usage
        end
      end

      def config(action) = actions.fetch(__method__).call(action)

      def build(configuration) = actions.fetch(__method__).call(configuration)

      def publish(configuration) = actions.fetch(__method__).call(configuration)

      def usage = logger.unknown(parser.to_s)

      def specification = container[__method__]

      def logger = container[__method__]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubysmith-1.2.0 lib/rubysmith/cli/shell.rb