Sha256: 5792b29ccaf52590252a2affb0969b310edf28ac3cce7f4532f60b7caa483fd2

Contents?: true

Size: 1.75 KB

Versions: 4

Compression:

Stored size: 1.75 KB

Contents

# typed: strict
# frozen_string_literal: true

module Packwerk
  # A command-line interface to Packwerk.
  class Cli
    extend T::Sig

    sig do
      params(
        configuration: T.nilable(Configuration),
        out: T.any(StringIO, IO),
        err_out: T.any(StringIO, IO),
        environment: String,
        style: OutputStyle,
        offenses_formatter: T.nilable(OffensesFormatter)
      ).void
    end
    def initialize(
      configuration: nil,
      out: $stdout,
      err_out: $stderr,
      environment: "test",
      style: OutputStyles::Plain.new,
      offenses_formatter: nil
    )
      @out = out
      @err_out = err_out
      @environment = environment
      @style = style
      @configuration = T.let(configuration || Configuration.from_path, Configuration)
      @progress_formatter = T.let(Formatters::ProgressFormatter.new(@out, style: style), Formatters::ProgressFormatter)
      @offenses_formatter = T.let(
        offenses_formatter || @configuration.offenses_formatter,
        OffensesFormatter
      )
    end

    sig { params(args: T::Array[String]).returns(T.noreturn) }
    def run(args)
      success = execute_command(args)
      exit(success)
    end

    sig { params(args: T::Array[String]).returns(T::Boolean) }
    def execute_command(args)
      command = args.shift || "help"
      command_class = Commands.for(command)

      if command_class
        command_class.new(
          args,
          configuration: @configuration,
          out: @out,
          err_out: @err_out,
          progress_formatter: @progress_formatter,
          offenses_formatter: @offenses_formatter,
        ).run
      else
        @err_out.puts("'#{command}' is not a packwerk command. See `packwerk help`.",)

        false
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
packwerk-3.2.2 lib/packwerk/cli.rb
packwerk-3.2.1 lib/packwerk/cli.rb
packwerk-3.2.0 lib/packwerk/cli.rb
packwerk-3.1.0 lib/packwerk/cli.rb