Class: CICI::Config

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

Instance Method Summary collapse

Constructor Details

#initialize(ui) ⇒ Config

Returns a new instance of Config.



8
9
10
# File 'lib/cici/config.rb', line 8

def initialize(ui)
  @ui = ui
end

Instance Method Details

#all_secretsObject

Get array of all secrets, including their base paths. So, a collection of files in the secrets directory to compress.



91
92
93
94
95
96
97
# File 'lib/cici/config.rb', line 91

def all_secrets
  secrets = Set[]
  secrets.merge(default_secrets)
  sets.keys.each { |set_key| secrets.merge(secrets_for_set(set_key)) }

  secrets.to_a
end

#all_secrets_original_pathsObject

Same as all_secrets(), but without base paths. So, a collection of files in their original source locations.



100
101
102
103
104
105
106
# File 'lib/cici/config.rb', line 100

def all_secrets_original_paths
  secrets = Set[]
  secrets.merge(default_secrets_without_base_path)
  sets.keys.each { |set_key| secrets.merge(secrets_for_set_without_base_path(set_key)) }

  secrets.to_a
end

#base_pathObject

Get “path”, or default value



27
28
29
# File 'lib/cici/config.rb', line 27

def base_path
  @config['path'] || 'secrets'
end

#default_secretsObject

Gets default array of secrets. Each secrets includes ‘base_path’ so they each look like: “secrets/path_to_file/file.txt”



33
34
35
# File 'lib/cici/config.rb', line 33

def default_secrets
  default_secrets_without_base_path.map { |secret_path| Pathname.new(base_path).join(secret_path).to_s }
end

#default_secrets_without_base_pathObject

Same as default_secrets(), but omit “base_path” inclusion. So you get raw entires from config file.



38
39
40
41
42
43
# File 'lib/cici/config.rb', line 38

def default_secrets_without_base_path
  secrets = []
  return secrets unless @config.key? 'default'

  return @config['default']['secrets'] if @config['default'].key? 'secrets'
end

#loadObject



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

def load
  config_file_name = '.cici.yml'

  @ui.fail("Cannot find config file, #{config_file_name} in current directory.") unless File.file?(config_file_name)

  config_file_contents = File.read(config_file_name)
  @config = YAML.safe_load(config_file_contents)

  @ui.verbose("Loaded config from file, #{config_file_name}")
  @ui.debug("Config: #{@config}")
end

#output_fileObject

outout file name. Includes file extension



116
117
118
119
120
121
# File 'lib/cici/config.rb', line 116

def output_file
  output_file = 'secrets.tar'
  output_file = "#{@config['output']}.tar" unless @config['output'].nil?

  output_file
end

#output_file_encryptedObject

output file name plus encryption file extension



124
125
126
# File 'lib/cici/config.rb', line 124

def output_file_encrypted
  "#{output_file}.enc"
end

#path_for_set(set_name) ⇒ Object

Gets the “base_path” for where all secrets will be stored for a set. If set name is ‘production` and base path is `secrets/`, this function could return: “secrets/production/”



58
59
60
61
62
63
64
65
66
# File 'lib/cici/config.rb', line 58

def path_for_set(set_name)
  set = set(set_name)
  path = Pathname.new(base_path)

  directory = set.key?('path') ? set['path'] : set_name
  path = path.join(directory)

  path.to_s
end

#secrets_for_set(set_name) ⇒ Object

Gets array of secrets for a set. Each secrets includes ‘base_path’ so they each look like: “secrets/name-of-set/path_to_file/file.txt”



78
79
80
# File 'lib/cici/config.rb', line 78

def secrets_for_set(set_name)
  secrets_for_set_without_base_path(set_name).map { |secret_path| Pathname.new(path_for_set(set_name)).join(secret_path).to_s }
end

#secrets_for_set_without_base_path(set_name) ⇒ Object

Same as secrets_for_set(), but omit “base_path” inclusion. So you get raw entires from config file.



69
70
71
72
73
74
# File 'lib/cici/config.rb', line 69

def secrets_for_set_without_base_path(set_name)
  set = set(set_name)
  return set['secrets'] if set.key? 'secrets'

  default_secrets_without_base_path
end

#set(name) ⇒ Object

Get a Hash for the set from the config file



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

def set(name)
  @ui.fail("Set, #{name}, does not exist in config file.") unless @config['sets'].key? name

  set = @config['sets'][name]

  set = {} if set.nil?

  set
end

#setsObject

Hash of all sets



109
110
111
112
113
# File 'lib/cici/config.rb', line 109

def sets
  sets = {}
  sets = @config['sets'] if @config.key? 'sets'
  sets
end

#skip_gitignore?Boolean

Should skip gitignore operation?

Returns:

  • (Boolean)


83
84
85
86
87
88
# File 'lib/cici/config.rb', line 83

def skip_gitignore?
  skip = false
  return @config['skip_gitignore'] if @config.key? 'skip_gitignore'

  skip
end