Sha256: c929e7d277c381456795e0d7b5832259640d66906530a17b4f270054b84f6c53

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

require 'open3'

module HerokuConfig
  class Config
    def initialize(app)
      @app = app
      check_heroku_cli_installed!
    end

    def get(name)
      return "fakevalue" if ENV['HEROKU_CONFIG_TEST']
      out = sh "heroku config:get #{name} -a #{@app}"
      return out if !out&.empty?
    end

    def set(*params)
      case params.size
      when 1 # Hash
        set_many(params.first)
      when 2 # 2 Strings
        set_one(name, value)
      else
        raise "ERROR: #{params.class} is a class that is not supported"
      end
    end

    def set_one(name, value)
      sh "heroku config:set #{name} #{value} -a #{@app}"
    end

    # Example:
    #
    #      set(a: 1, b: 2)
    #      =>
    #      heroku config:set a=1 b=2 -a APP
    #
    def set_many(hash)
      args = hash.map { |k,v| "#{k}=#{v}" }.join(' ')
      sh "heroku config:set #{args} -a #{@app}", include_stderr: true
    end

  private
    def sh(command, include_stderr: false)
      puts "=> #{command}"
      stdout, stderr, status = Open3.capture3(command)

      out = stdout.strip
      unless status.success?
        puts "ERROR: #{stderr}".color(:red)
        if stderr.empty?
          puts "STDOUT: #{stdout}"
        end
        exit 1
      end
      if include_stderr
        stderr + "\n" + out
      else
        out
      end
    end

    def check_heroku_cli_installed!
      return if heroku_cli_installed?

      puts "The heroku cli is not installed. Please install the heroku cli: https://devcenter.heroku.com/articles/heroku-cli"
      exit 1
    end

    def heroku_cli_installed?
      system("type heroku > /dev/null 2>&1")
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
heroku-config-0.4.0 lib/heroku_config/config.rb
heroku-config-0.3.0 lib/heroku_config/config.rb