Class: Cangallo::Config

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

Constant Summary collapse

CONFIG_DIR =
'.cangallo'
CONFIG_FILE =
'config.yaml'
DEFAULT_CONFIG =
<<EOT
default_repo: default
repos:
    default:
        type: local
        path: ~/#{CONFIG_DIR}/default
EOT

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



34
35
36
37
38
# File 'lib/cangallo/config.rb', line 34

def initialize
  create_config_dir
  create_default_config
  load_conf
end

Instance Method Details

#config_dirObject



83
84
85
# File 'lib/cangallo/config.rb', line 83

def config_dir
  File.join(ENV['HOME'], CONFIG_DIR)
end

#config_fileObject



87
88
89
# File 'lib/cangallo/config.rb', line 87

def config_file
  File.join(config_dir, CONFIG_FILE)
end

#create_config_dirObject



59
60
61
62
63
# File 'lib/cangallo/config.rb', line 59

def create_config_dir
  if !File.exist?(config_dir)
    FileUtils.mkdir_p(config_dir)
  end
end

#create_default_configObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cangallo/config.rb', line 65

def create_default_config
  if !File.exist?(config_file)
    open(config_file, 'w') do |f|
      f.write(DEFAULT_CONFIG)
    end

    load_conf
    path = File.expand_path(@conf["repos"]["default"]["path"])
    create_repo_dir(path)
  end
end

#create_repo_dir(path) ⇒ Object



77
78
79
80
81
# File 'lib/cangallo/config.rb', line 77

def create_repo_dir(path)
  if !File.exist?(path)
    FileUtils.mkdir_p(path)
  end
end

#load_confObject



55
56
57
# File 'lib/cangallo/config.rb', line 55

def load_conf
  @conf = YAML.load_file(config_file)
end

#repo(name = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cangallo/config.rb', line 40

def repo(name = nil)
  repo_name = name || @conf['default_repo'] || 'default'
  raise(%q{Configuration malformed, no 'repos'}) if !@conf['repos']

  repo_conf = @conf['repos'][repo_name]
  raise(%Q<No repo with name '#{repo_name}'>) if !repo_conf
  raise(%Q<Repo path no defined for '#{repo_name}'>) if !repo_conf['path']

  path = File.expand_path(repo_conf["path"])
  repo_conf["path"] = path
  repo_conf["name"] = repo_name
  create_repo_dir(path)
  Cangallo::Repo.new(repo_conf)
end

#reposObject



91
92
93
# File 'lib/cangallo/config.rb', line 91

def repos
  @conf["repos"].keys
end