Skip to content

Instantly share code, notes, and snippets.

@csexton
Created April 27, 2016 14:09
Show Gist options
  • Select an option

  • Save csexton/a59e3c6b45ecd181c4ad3e7d21463258 to your computer and use it in GitHub Desktop.

Select an option

Save csexton/a59e3c6b45ecd181c4ad3e7d21463258 to your computer and use it in GitHub Desktop.

Revisions

  1. csexton created this gist Apr 27, 2016.
    64 changes: 64 additions & 0 deletions heroku-environments
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    #!/usr/bin/env ruby

    # Heroku enviroment helper script
    #
    # This is to streamline using differnt heroku enviroments from one repo.
    # Symlink this file to an envroment name (as set by the git-remote name), then
    # when running that symlink the script will look up the name in the git config
    # and use that remote as the heroku name.
    #
    # Setup:
    #
    # ln -s ~/bin/heroku-enviroments production
    #
    # And assuming git is configured:
    #
    # [remote "production"]
    # url = [email protected]:my-app-production.git
    # fetch = +refs/heads/*:refs/remotes/production/*
    #
    # Then running:
    #
    # production run rails console
    #
    # Actually runs:
    #
    # heroku run rails console --app my-app-production

    trap "SIGINT" do
    puts "Exiting"
    exit 130
    end

    class String
    def red; "\e[31m#{self}\e[0m" end
    def green; "\e[32m#{self}\e[0m" end
    def blue; "\e[34m#{self}\e[0m" end
    end

    def error(message)
    STDERR.puts "Error: #{message}".red
    exit 1
    end

    env_name = File.basename($0, File.extname($0))
    url = `git config --get remote.#{env_name}.url`.chomp

    if ARGV.empty?
    puts "Heroku CLI Wrapper for #{env_name}. Try `#{env_name} --help` for more information"
    exit 0
    end

    error "Unable to find git config file. Are you in a git repo?" if url.empty?

    app_name = /([^\/:]+)\.git$/.match(url)[1]

    error "Unable to find #{env_name} remote. Make sure you have `remote.production` set in your git config" if app_name.empty?

    cmd = "heroku #{ARGV.join " "} --app #{app_name}"

    puts cmd.blue
    puts

    system(cmd)
    exit $?.exitstatus