Sha256: 872f90b7828bc1b2b26fd9f32aec021164977f1458c1510a334d26fdd72a149c

Contents?: true

Size: 1.8 KB

Versions: 17

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true
require 'shopify_cli'

module Extension
  module Commands
    class Tunnel < ExtensionCommand
      options do |parser, flags|
        parser.on('--port=PORT') { |port| flags[:port] = port }
      end

      AUTH_SUBCOMMAND = 'auth'
      START_SUBCOMMAND = 'start'
      STOP_SUBCOMMAND = 'stop'
      STATUS_SUBCOMMAND = 'status'
      DEFAULT_PORT = 39351

      def call(args, _name)
        subcommand = args.shift

        case subcommand
        when AUTH_SUBCOMMAND then authorize(args)
        when START_SUBCOMMAND then ShopifyCli::Tunnel.start(@ctx, port: port)
        when STOP_SUBCOMMAND then ShopifyCli::Tunnel.stop(@ctx)
        when STATUS_SUBCOMMAND then status
        else @ctx.puts(self.class.help)
        end
      end

      def self.help
        ShopifyCli::Context.message('tunnel.help', ShopifyCli::TOOL_NAME)
      end

      def self.extended_help
        ShopifyCli::Context.message('tunnel.extended_help', ShopifyCli::TOOL_NAME, DEFAULT_PORT)
      end

      private

      def status
        tunnel_url = ShopifyCli::Tunnel.urls.first

        if tunnel_url.nil?
          @ctx.puts(@ctx.message('tunnel.no_tunnel_running'))
        else
          @ctx.puts(@ctx.message('tunnel.tunnel_running_at', tunnel_url))
        end
      end

      def port
        return DEFAULT_PORT unless options.flags.key?(:port)

        port = options.flags[:port].to_i
        @ctx.abort(@ctx.message('tunnel.invalid_port', options.flags[:port])) unless port > 0
        port
      end

      def authorize(args)
        token = args.shift

        if token.nil?
          @ctx.puts(@ctx.message('tunnel.missing_token'))
          @ctx.puts("#{self.class.help}\n#{self.class.extended_help}")
        else
          ShopifyCli::Tunnel.auth(@ctx, token)
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
shopify-cli-1.6.0 lib/project_types/extension/commands/tunnel.rb
shopify-cli-1.5.0 lib/project_types/extension/commands/tunnel.rb
shopify-cli-1.4.1 lib/project_types/extension/commands/tunnel.rb
shopify-cli-1.4.0 lib/project_types/extension/commands/tunnel.rb
shopify-cli-1.3.1 lib/project_types/extension/commands/tunnel.rb
shopify-cli-1.3.0 lib/project_types/extension/commands/tunnel.rb
shopify-cli-1.2.0 lib/project_types/extension/commands/tunnel.rb
shopify-cli-1.1.2 lib/project_types/extension/commands/tunnel.rb
shopify-cli-1.1.1 lib/project_types/extension/commands/tunnel.rb
shopify-cli-1.1.0 lib/project_types/extension/commands/tunnel.rb
shopify-cli-1.0.5 lib/project_types/extension/commands/tunnel.rb
shopify-cli-1.0.4 lib/project_types/extension/commands/tunnel.rb
shopify-cli-1.0.3 lib/project_types/extension/commands/tunnel.rb
shopify-cli-1.0.2 lib/project_types/extension/commands/tunnel.rb
shopify-cli-1.0.1 lib/project_types/extension/commands/tunnel.rb
shopify-cli-1.0.0 lib/project_types/extension/commands/tunnel.rb
shopify-cli-0.9.3 lib/project_types/extension/commands/tunnel.rb