Class: RepoMgr::Config

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

Overview

handles repo-mgr configuration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/repo_mgr/config.rb', line 13

def initialize
  @cfg_dir = "#{ENV['HOME']}/.repo-mgr"
  FileUtils.mkdir_p @cfg_dir

  @cfg_file = "#{@cfg_dir}/repo-mgr.yml"
  unless File.exist? @cfg_file
    File.write @cfg_file, { repos: {}, packages: {} }.to_yaml
  end

  @cfg = YAML.load_file @cfg_file
end

Instance Attribute Details

#cfgObject (readonly)

Returns the value of attribute cfg.



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

def cfg
  @cfg
end

#cfg_dirObject (readonly)

Returns the value of attribute cfg_dir.



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

def cfg_dir
  @cfg_dir
end

Instance Method Details

#add_pkg(repo, path) ⇒ Object



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

def add_pkg(repo, path)
  if @cfg[:repos][repo].nil?
    Tools.error "unable to add packages to #{repo} - repo does not exist"
  end

  @cfg[:packages] ||= {}
  @cfg[:packages][repo] ||= []
  pkg = File.basename path

  if @cfg[:packages][repo].include?(pkg)
    Tools.error "you already have #{pkg} in your #{repo} repo"
  end

  @cfg[:packages][repo] << pkg

  save
end

#remove_pkg(repo, path) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/repo_mgr/config.rb', line 68

def remove_pkg(repo, path)
  if @cfg[:repos][repo].nil?
    Tools.error "unable to remove packages from #{repo} "\
                '- repo does not exist'
  end

  @cfg[:packages] ||= {}
  @cfg[:packages][repo] ||= []
  pkg = File.basename path
  @cfg[:packages][repo].delete pkg

  save
end

#saveObject



25
26
27
# File 'lib/repo_mgr/config.rb', line 25

def save
  File.write @cfg_file, @cfg.to_yaml
end

#upsert_repo(options) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/repo_mgr/config.rb', line 29

def upsert_repo(options)
  name = options[:name]
  type = options[:type]

  if @cfg[:repos][name] && @cfg[:repos][name][:type] != type
    Tools.error "unable to change type for #{name} repository"
  end

  @cfg[:repos][name] = {
    type: type,
    path: options[:path],
    keyid: options[:keyid]
  }

  if options[:publisher]
    @cfg[:repos][name][:publisher] = options[:publisher]
  end

  save
end