Skip to content

Instantly share code, notes, and snippets.

@frank184
Created May 21, 2016 16:44
Show Gist options
  • Save frank184/19b0fb3413b9cc865a61b7698060675f to your computer and use it in GitHub Desktop.
Save frank184/19b0fb3413b9cc865a61b7698060675f to your computer and use it in GitHub Desktop.
Rake Task that controls the Ruby API for localtunnel
# lib/tasks/localtunnel.rake
namespace :localtunnel do
desc "Starts the LocalTunnel instace"
task start: :environment do
begin
if Rails.env.production?
raise "Trying to launch Localtunnel in production".red
end
default_subdomain = "whatever"
default_port = 3000
print "Enter an subdomain or blank (default: #{default_subdomain}) "
subdomain = $stdin.gets.chomp.downcase
subdomain = default_subdomain if subdomain.blank?
print "Enter an port or blank (default: #{default_port}) "
port = $stdin.gets.chomp.downcase
port = default_port if port.blank?
unless Localtunnel::Client.running?
puts "=> Started localtunnel at #{Localtunnel::Client.start(port: port, subdomain: subdomain)}"
else
puts "=> Localtunnel already running..."
end
rescue => e
puts e.message.red
end
end
desc "Stops the LocalTunnel instance"
task stop: :environment do
if Localtunnel::Client.running?
puts "=> Stopping Localtunnel at #{Localtunnel::Client.url}"
Localtunnel::Client.stop
else
puts "=> Localtunnel was not running..."
end
end
desc "Stops, then Stars the LocalTunnel instance"
task restart: [:start, :stop]
desc "Returns the URL that LocalTunnel is using"
task url: :environment do
puts "=> Running localtunnel at #{Localtunnel::Client.url}"
end
desc "Returns whether or not LocalTunnel is running successfully"
task running: :environment do
Localtunnel::Client.running?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment