Sha256: e85e91d57c9aaf2dde9f69894aeb2dd173a9ebd57a4ba4d13a3b601338b1af9c

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

require 'shellwords'
require 'yaml'

module Travis
  class Cli
    class Config
      include Helper

      attr_reader :shell, :remote, :options

      def initialize(shell, remote, options)
        @remote = remote
        @options = options
        @shell = shell
      end

      def invoke
        store
        push
        restart if restart?
      end

      protected

        def app
          @app ||= begin
            app = File.basename(Dir.pwd).gsub('travis-', '')
            app = 'web' if app == 'ci'
            app
          end
        end

        def config
          @config ||= keychain.fetch
        end

        def keychain
          @keychain ||= Keychain.new(app, shell)
        end

        def store
          backup if backup?
          File.open(filename, 'w+') { |f| f.write(config) }
        end

        def push
          say 'Configuring the app ...'
          config = Shellwords.escape(YAML.dump(YAML.load(self.config)[remote]))
          run "heroku config:add travis_config=#{config} -r #{remote}", :echo => "heroku config:add travis_config=... -r #{app}"
        end

        def restart
          say 'Restarting the app ...'
          run "heroku restart -r #{remote}"
        end

        def backup
          say 'Backing up the old config file ...'
          run "cp #{filename} #{filename}.backup"
        end

        def restart?
          !!options['restart']
        end

        def backup?
          !!options['backup']
        end

        def filename
          "config/travis.yml"
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
travis-cli-0.0.1 lib/travis/cli/config.rb