Class: Capistrano::Redmine::Deployment::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano/redmine/deployment/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}, file: nil) ⇒ Config

Returns a new instance of Config.



79
80
81
82
83
# File 'lib/capistrano/redmine/deployment/config.rb', line 79

def initialize(config = {}, file: nil)
  @config = config
  @file = file
  load
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



118
119
120
# File 'lib/capistrano/redmine/deployment/config.rb', line 118

def method_missing(name)
  get(name)
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



10
11
12
# File 'lib/capistrano/redmine/deployment/config.rb', line 10

def file
  @file
end

Class Method Details

.config_from_capistrano(capistrano) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/capistrano/redmine/deployment/config.rb', line 31

def config_from_capistrano(capistrano)
  config = {
    api_key: capistrano.fetch(:redmine_api_key) || api_key_from_command(capistrano),
    host: capistrano.fetch(:redmine_host),
    project: capistrano.fetch(:redmine_project) || capistrano.fetch(:redmine_project_id),
    repository: capistrano.fetch(:redmine_repository),
    host_verification: capistrano.fetch(:redmine_host_verification),
    ca_file: capistrano.fetch(:redmine_ca_file)
  }

  new(config)
end

.config_from_envObject

Resolves config from ENV variables. Values are filtered by assign!, so unset (nil/empty) variables never overwrite an existing value.



46
47
48
49
50
51
52
53
54
# File 'lib/capistrano/redmine/deployment/config.rb', line 46

def config_from_env
  new({
    api_key:    ENV['REDMINE_API_KEY'],
    host:       ENV['REDMINE_HOST'],
    project:    ENV['REDMINE_PROJECT'],
    repository: ENV['REDMINE_REPOSITORY'],
    ca_file:    ENV['REDMINE_CA_FILE']
  })
end

.config_from_file(file) ⇒ Object



56
57
58
# File 'lib/capistrano/redmine/deployment/config.rb', line 56

def config_from_file(file)
  new(file: file)
end

.resolve(capistrano: nil, file: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/capistrano/redmine/deployment/config.rb', line 13

def resolve(capistrano: nil, file: nil)
  # build new empty config
  config = new

  # capistrano is the base - files and ENV win over it
  config.assign!(config_from_capistrano(capistrano)) if capistrano

  # try to resolve from current PWD
  config.assign!(config_from_file(File.join(Dir.pwd, '.redmine')))
  config.assign!(config_from_file(File.join(ENV['HOME'], '.redmine')))
  config.assign!(config_from_file(file)) if file

  # ENV wins over files and capistrano
  config.assign!(config_from_env)

  config
end

Instance Method Details

#assign!(other) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/capistrano/redmine/deployment/config.rb', line 101

def assign!(other)
  return unless other

  other.to_h.each do |key, value|
    next if value == nil || value == ''

    set(key, value)
  end
end

#get(key) ⇒ Object



93
94
95
# File 'lib/capistrano/redmine/deployment/config.rb', line 93

def get(key)
  @config[key.to_sym]
end

#loadObject



128
129
130
131
132
# File 'lib/capistrano/redmine/deployment/config.rb', line 128

def load
  if @file && File.exist?(@file)
    @config = YAML.load_file(@file)
  end
end

#saveObject



122
123
124
125
126
# File 'lib/capistrano/redmine/deployment/config.rb', line 122

def save
  return false unless @file

  File.open(@file, 'w') { |f| f.write(YAML.dump(@config)) } rescue false
end

#set(key, value) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/capistrano/redmine/deployment/config.rb', line 85

def set(key, value)
  if value != nil && value != ''
    @config[key.to_sym] = value
  else
    @config.delete(key.to_sym)
  end
end

#to_hObject



97
98
99
# File 'lib/capistrano/redmine/deployment/config.rb', line 97

def to_h
  @config
end

#valid?Boolean

Returns:

  • (Boolean)


111
112
113
114
115
116
# File 'lib/capistrano/redmine/deployment/config.rb', line 111

def valid?
  %i[host project repository api_key].all? { |key|
    val = get(key)
    val && val != ''
  }
end