Sha256: 92bbedd67e22e9c5dbb8e35fb629094fd9bb05d687e78515b9666b1ada592465

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module SmtpMock
  module Cli
    module Resolver
      require 'optparse'

      USE_CASE = 'Usage: smtp_mock [options], example: `bundle exec smtp_mock -s -i ~/existent_dir`'
      DOWNLOAD_SCRIPT = 'https://raw.githubusercontent.com/mocktools/go-smtp-mock/master/script/download.sh'

      def resolve(command_line_args) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
        opt_parser = ::OptionParser.new do |parser|
          parser.banner = SmtpMock::Cli::Resolver::USE_CASE

          parser.on('-s', '--sudo', 'Run command as sudo') do
            self.sudo = true
          end

          parser.on('-iPATH', '--install=PATH', 'Install smtpmock to the existing path') do |argument|
            self.install_path = argument
            return self.message = 'smtpmock is already installed' if ::File.exist?(binary_path)

            ::Kernel.system("cd #{install_path} && curl -sL #{SmtpMock::Cli::Resolver::DOWNLOAD_SCRIPT} | bash")
            ::Kernel.system("#{as_sudo}ln -s #{binary_path} #{SmtpMock::Dependency::SYMLINK}")

            self.message = 'smtpmock was installed successfully'
          end

          parser.on('-u', '--uninstall', 'Uninstall smtpmock') do
            current_smtpmock_path = SmtpMock::Dependency.smtpmock_path_by_symlink
            return self.message = 'smtpmock not installed yet' if current_smtpmock_path.empty?

            ::Kernel.system("#{as_sudo}unlink #{SmtpMock::Dependency::SYMLINK}")
            ::Kernel.system("rm #{current_smtpmock_path}")

            self.message = 'smtpmock was uninstalled successfully'
          end

          parser.on('-h', '--help', 'Prints help') do
            self.message = parser.to_s
          end

          self.success = true
        end

        opt_parser.parse(command_line_args) # TODO: add error handler
      end

      private

      def binary_path
        "#{install_path}/smtpmock"
      end

      def as_sudo
        return 'sudo ' if sudo
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
smtp_mock-0.1.2 lib/smtp_mock/cli/resolver.rb