Sha256: 796d9e99fa8d6f0e8a0dab73927f01ec57c4027da35dbd8b2a4bec6e58675a08

Contents?: true

Size: 1.62 KB

Versions: 16

Compression:

Stored size: 1.62 KB

Contents

module Negroku::Modes
  module Env
    ENV_FILE = ".rbenv-vars.example"

    def bulk(selected_stage = nil)
      if stage = selected_stage || select_stage
        variables = select_variables
        if variables.empty?
          puts I18n.t(:no_variables_setted, scope: :negroku)
        else
          set_vars_to_stage(stage, variables)
        end
      end
    end

    # Sets the variables to the selected stage using cap rbenv set
    def set_vars_to_stage(stage, variables)
      # convert to array using VAR=value
      vars_array = variables.map{|k,v| "#{k}=#{v}" }
      Capistrano::Application.invoke(stage)
      Capistrano::Application.invoke("rbenv:vars:set", *vars_array)
    end

    # build a list of variables from ENV_FILE and yeilds it
    def get_variables
      return unless File.exists?(ENV_FILE)

      File.open(ENV_FILE).each do |line|
        var_name = line.split("=").first
        yield var_name unless line =~ /^\#/
      end
    end

    # Returns a hash of selected variables and values
    def select_variables
      selection = {}
      puts I18n.t(:ask_variables_message, scope: :negroku)
      get_variables do |var_name|
        selection[var_name] = Ask.input(var_name)
      end
      selection.reject {|key, value| value.empty? }
    end

    def select_stage
      if current_stages.empty?
        puts I18n.t(:no_stages_found, scope: :negroku)
      else
        selection = Ask.list I18n.t(:select_stages_question, scope: :negroku), current_stages
        current_stages[selection]
      end
    end

    def current_stages
      @current_stages ||= Capistrano::Application.stages
    end

    extend self
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
negroku-2.8.6 lib/negroku/modes/env.rb
negroku-2.8.5 lib/negroku/modes/env.rb
negroku-2.8.4 lib/negroku/modes/env.rb
negroku-2.8.3 lib/negroku/modes/env.rb
negroku-2.8.2 lib/negroku/modes/env.rb
negroku-2.8.1 lib/negroku/modes/env.rb
negroku-2.8.0 lib/negroku/modes/env.rb
negroku-2.7.1 lib/negroku/modes/env.rb
negroku-2.7.0 lib/negroku/modes/env.rb
negroku-2.6.0 lib/negroku/modes/env.rb
negroku-2.5.5 lib/negroku/modes/env.rb
negroku-2.5.4 lib/negroku/modes/env.rb
negroku-2.5.3 lib/negroku/modes/env.rb
negroku-2.5.2 lib/negroku/modes/env.rb
negroku-2.5.1 lib/negroku/modes/env.rb
negroku-2.5.0 lib/negroku/modes/env.rb