Sha256: 54ad414e31845aba214bc7dea1124e9ff32feeefce625596ae5fbbb9513df74d

Contents?: true

Size: 1.73 KB

Versions: 101

Compression:

Stored size: 1.73 KB

Contents

require 'cli/kit'

module CLI
  module Kit
    class Resolver
      def initialize(tool_name:, command_registry:)
        @tool_name = tool_name
        @command_registry = command_registry
      end

      def call(args)
        args = args.dup
        command_name = args.shift

        command, resolved_name = @command_registry.lookup_command(command_name)

        if command.nil?
          command_not_found(command_name)
          raise CLI::Kit::AbortSilent # Already output message
        end

        [command, resolved_name, args]
      end

      private

      def command_not_found(name)
        CLI::UI::Frame.open("Command not found", color: :red, timing: false) do
          $stderr.puts(CLI::UI.fmt("{{command:#{@tool_name} #{name}}} was not found"))
        end

        cmds = commands_and_aliases
        if cmds.all? { |cmd| cmd.is_a?(String) }
          possible_matches = cmds.min_by(2) do |cmd|
            CLI::Kit::Levenshtein.distance(cmd, name)
          end

          # We don't want to match against any possible command
          # so reject anything that is too far away
          possible_matches.reject! do |possible_match|
            CLI::Kit::Levenshtein.distance(possible_match, name) > 3
          end

          # If we have any matches left, tell the user
          if possible_matches.any?
            CLI::UI::Frame.open("{{bold:Did you mean?}}", timing: false, color: :blue) do
              possible_matches.each do |possible_match|
                $stderr.puts CLI::UI.fmt("{{command:#{@tool_name} #{possible_match}}}")
              end
            end
          end
        end
      end

      def commands_and_aliases
        @command_registry.command_names + @command_registry.aliases.keys
      end
    end
  end
end

Version data entries

101 entries across 101 versions & 2 rubygems

Version Path
shopify-cli-1.4.0 vendor/deps/cli-kit/lib/cli/kit/resolver.rb
shopify-cli-1.3.1 vendor/deps/cli-kit/lib/cli/kit/resolver.rb
shopify-cli-1.3.0 vendor/deps/cli-kit/lib/cli/kit/resolver.rb
shopify-cli-1.2.0 vendor/deps/cli-kit/lib/cli/kit/resolver.rb
shopify-cli-1.1.2 vendor/deps/cli-kit/lib/cli/kit/resolver.rb
shopify-cli-1.1.1 vendor/deps/cli-kit/lib/cli/kit/resolver.rb
shopify-cli-1.1.0 vendor/deps/cli-kit/lib/cli/kit/resolver.rb
shopify-cli-1.0.5 vendor/deps/cli-kit/lib/cli/kit/resolver.rb
shopify-cli-1.0.4 vendor/deps/cli-kit/lib/cli/kit/resolver.rb
shopify-cli-1.0.3 vendor/deps/cli-kit/lib/cli/kit/resolver.rb
shopify-cli-1.0.2 vendor/deps/cli-kit/lib/cli/kit/resolver.rb
shopify-cli-1.0.1 vendor/deps/cli-kit/lib/cli/kit/resolver.rb
shopify-cli-1.0.0 vendor/deps/cli-kit/lib/cli/kit/resolver.rb
shopify-cli-0.9.3 vendor/deps/cli-kit/lib/cli/kit/resolver.rb
shopify-cli-0.9.2 vendor/deps/cli-kit/lib/cli/kit/resolver.rb
shopify-cli-0.9.1 vendor/deps/cli-kit/lib/cli/kit/resolver.rb
shopify-cli-0.9.0 vendor/deps/cli-kit/lib/cli/kit/resolver.rb
cli-kit-3.3.0 lib/cli/kit/resolver.rb
cli-kit-3.1.0 lib/cli/kit/resolver.rb
cli-kit-3.0.1 lib/cli/kit/resolver.rb