Module: Negroku::Modes::Env

Extended by:
Env
Included in:
Env
Defined in:
lib/negroku/modes/env.rb

Constant Summary collapse

ENV_FILE =
".rbenv-vars.example"

Instance Method Summary collapse

Instance Method Details

#bulk(selected_stage = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/negroku/modes/env.rb', line 5

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

#current_stagesObject



53
54
55
# File 'lib/negroku/modes/env.rb', line 53

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

#get_variablesObject

build a list of variables from ENV_FILE and yeilds it



25
26
27
28
29
30
31
32
# File 'lib/negroku/modes/env.rb', line 25

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

#select_stageObject



44
45
46
47
48
49
50
51
# File 'lib/negroku/modes/env.rb', line 44

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

#select_variablesObject

Returns a hash of selected variables and values



35
36
37
38
39
40
41
42
# File 'lib/negroku/modes/env.rb', line 35

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

#set_vars_to_stage(stage, variables) ⇒ Object

Sets the variables to the selected stage using cap rbenv set



17
18
19
20
21
22
# File 'lib/negroku/modes/env.rb', line 17

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