-
Notifications
You must be signed in to change notification settings - Fork 200
class ShopifyCli::Tunnel
Wraps around ngrok functionality to allow you to spawn a ngrok proccess in the background and stop the process when you need to. It also allows control over the ngrok process between application runs.
PORT
DOWNLOAD_URLS
NGROK_TUNNELS_URI
TUNNELS_FIELD
TUNNEL_ADDRESS_KEY_PATH
PUBLIC_URL_FIELD
stop(ctx)
will find and stop a running tunnel process. It will also output if the
operation was successful or not
-
ctx
- running context from your command
see source
# File lib/shopify-cli/tunnel.rb, line 41
def stop(ctx)
if ShopifyCli::ProcessSupervision.running?(:ngrok)
if ShopifyCli::ProcessSupervision.stop(:ngrok)
ctx.puts(ctx.message("core.tunnel.stopped"))
else
ctx.abort(ctx.message("core.tunnel.error.stop"))
end
else
ctx.puts(ctx.message("core.tunnel.not_running"))
end
end
start(ctx, port: PORT)
start will start a running ngrok process running in the background. It will
also output the success of this operation
-
ctx
- running context from your command -
port
- port to use to open the ngrok tunnel
-
url
- the url that the tunnel is now bound to and available to the public
see source
# File lib/shopify-cli/tunnel.rb, line 66
def start(ctx, port: PORT)
install(ctx)
url, account, seconds_remaining = start_ngrok(ctx, port)
if account
ctx.puts(ctx.message("core.tunnel.start_with_account", url, account))
else
if seconds_remaining <= 0
ctx.puts(ctx.message("core.tunnel.timed_out"))
url, _account, seconds_remaining = restart_ngrok(ctx, port)
end
ctx.puts(ctx.message("core.tunnel.start", url))
ctx.puts(ctx.message("core.tunnel.will_timeout", seconds_to_hm(seconds_remaining)))
ctx.puts(ctx.message("core.tunnel.signup_suggestion", ShopifyCli::TOOL_NAME))
end
url
end
auth(ctx, token)
will add the users authentication token to our version of ngrok to unlock the
extended ngrok features
-
ctx
- running context from your command -
token
- authentication token provided by ngrok for extended features
see source
# File lib/shopify-cli/tunnel.rb, line 92
def auth(ctx, token)
install(ctx)
ctx.system(File.join(ShopifyCli.cache_dir, "ngrok"), "authtoken", token)
end
stats()
will return the statistics of the current running tunnels
-
stats
- the hash of running statistics returning from the ngrok api
see source
# File lib/shopify-cli/tunnel.rb, line 104
def stats
response = Net::HTTP.get_response(NGROK_TUNNELS_URI)
JSON.parse(response.body)
rescue
{}
end
urls()
will return the urls of the current running tunnels
-
stats
- the array of urls
see source
# File lib/shopify-cli/tunnel.rb, line 118
def urls
tunnels = stats.dig(TUNNELS_FIELD)
tunnels.map { |tunnel| tunnel.dig(PUBLIC_URL_FIELD) }
rescue
[]
end
running_on?(port)
Returns Boolean if a tunnel is running on a given port
-
port
- port to check
- true / false
see source
# File lib/shopify-cli/tunnel.rb, line 136
def running_on?(port)
extract_port = ->(tunnel) { URI(tunnel.dig(*TUNNEL_ADDRESS_KEY_PATH)).port }
matches_port = ->(occupied_port) { occupied_port == port }
stats.fetch(TUNNELS_FIELD, []).map(&extract_port).any?(&matches_port)
rescue
false
end
- SingleForwardable