Class: WebistranoCli::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/webistrano_cli/config.rb

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



6
7
8
9
# File 'lib/webistrano_cli/config.rb', line 6

def initialize
  @file_path  = File.expand_path('~/.webistrano_cli.yml')
  @config     = YAML.load_file(@file_path) rescue nil
end

Instance Method Details

#defaultsObject



32
33
34
35
36
37
# File 'lib/webistrano_cli/config.rb', line 32

def defaults
  {
    :stage  => get_value('defaults/stage')  || 'staging',
    :task   => get_value('defaults/task')   || 'deploy:migrations'
  }
end

#get_value(path) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/webistrano_cli/config.rb', line 39

def get_value(path)
  nested = path.to_s.split('/')
  current = @config['webistrano_cli']
  nested.each do |key|
    current = current[key]
  end
  current
rescue
  nil
end

#load!(path) ⇒ Object



11
12
13
# File 'lib/webistrano_cli/config.rb', line 11

def load!(path)
  @config = YAML.load_file(path)
end

#save_yamlObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/webistrano_cli/config.rb', line 50

def save_yaml
  return @config if File.exists?(@file_path)
  File.open(@file_path, 'w') do |file|
    YAML.dump({
      'webistrano_cli' => {
        'url' => @config['url'].to_s,
        'user' => @config['user'].to_s,
        'password' => @config['password'].to_s,
        'defaults' => {
          'stage' => 'staging',
          'task'  => 'deploy:migrations'
        }
      }
    }, file)
  end
  @config
end

#setup_and_load!Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/webistrano_cli/config.rb', line 15

def setup_and_load!
  return @config if @config.presence
  config = {}
  # read config from ENV or ask for it
  ['url', 'user', 'password'].each do |field|
    config[field] = ENV["WCLI_#{field.upcase}"]
    next if config[field]
    config[field] = ask("webistrano #{field}: ", String) do |q|
      q.whitespace = :strip_and_collapse
      q.validate  = lambda { |p| p.length > 0 }
      q.responses[:not_valid] = "can't be blank!"
    end
  end
  @config = {'webistrano_cli' =>  config}
  save_yaml
end