Module: DockerSyncConfig

Defined in:
lib/docker-sync/config.rb

Overview

this has basically completely reused from Thor::runner.rb - thank you!

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.escape_globs(path) ⇒ Object



112
113
114
# File 'lib/docker-sync/config.rb', line 112

def self.escape_globs(path)
  path.to_s.gsub(/[*?{}\[\]]/, '\\\\\\&')
end

.global_configObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/docker-sync/config.rb', line 23

def self.global_config
  global_config_path = global_config_location
  date = DateTime.new(2001, 1, 1) #paste
  # noinspection RubyStringKeysInHashInspection
  defaults = {'update_check'=>true, 'update_last_check' => date.iso8601(9), 'update_enforce' => true}
  if File.exist?(global_config_path)
    config = ConfigTemplate::interpolate_config_file(global_config_path)
    config = defaults.merge(config)
    return config
  else
    return defaults
  end
end

.global_config_locationObject



14
15
16
# File 'lib/docker-sync/config.rb', line 14

def self.global_config_location
  return File.expand_path('~/.docker-sync-global.yml')
end

.global_config_save(config) ⇒ Object



37
38
39
40
# File 'lib/docker-sync/config.rb', line 37

def self.global_config_save(config)
  global_config_path = global_config_location
  File.open(global_config_path, 'w') {|f| f.write config.to_yaml }
end

.globs_for_project_config(path) ⇒ Object

Where to look for docker-sync.yml files.



107
108
109
110
# File 'lib/docker-sync/config.rb', line 107

def self.globs_for_project_config(path)
  path = escape_globs(path)
  ["#{path}/docker-sync.yml"]
end

.is_first_runObject



18
19
20
21
# File 'lib/docker-sync/config.rb', line 18

def self.is_first_run
  global_config_path = global_config_location
  return !File.exist?(global_config_path)
end

.project_config_find(skip_lookup = false) ⇒ Object

this has been ruthlessly stolen from Thor/runner.rb - please do not hunt me for that :)



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/docker-sync/config.rb', line 71

def self.project_config_find(skip_lookup = false)
  # Finds docker-sync.yml by traversing from your current directory down to the root
  # directory of your system. If at any time we find a docker-sync.yml file, we stop.
  #
  #
  # ==== Example
  #
  # If we start at /Users/wycats/dev/thor ...
  #
  # 1. /Users/wycats/dev/thor
  # 2. /Users/wycats/dev
  # 3. /Users/wycats <-- we find a docker-sync.yml here, so we stop
  #
  # Suppose we start at c:\Documents and Settings\james\dev\docker-sync ...
  #
  # 1. c:\Documents and Settings\james\dev\docker-sync.yml
  # 2. c:\Documents and Settings\james\dev
  # 3. c:\Documents and Settings\james
  # 4. c:\Documents and Settings
  # 5. c:\ <-- no docker-sync.yml found!
  #
  docker_sync_files = []

  unless skip_lookup
    Pathname.pwd.ascend do |path|
      docker_sync_files = globs_for_project_config(path).map { |g| Dir[g] }.flatten
      break unless docker_sync_files.empty?
    end
  end

  files = []
  files += docker_sync_files
end

.project_config_pathObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/docker-sync/config.rb', line 53

def self.project_config_path
  files = project_config_find
  if files.length > 0
    path =  files.pop
  else
    raise('No docker-sync.yml configuration found in your path ( traversing up ) Did you define it for your project?')
  end

  begin
    config = YAML.load_file(path)
    raise "Version of docker-sync.yml does not match the reqiured one" unless project_ensure_configuration_version_compatibility(config)
  rescue Exception => e
    raise "You docker-sync.yml file does either not include a version: \"#{project_required_config_version}\" setting or your setting (#{config['version']}) does not match the required version (#{project_required_config_version}). (Add this if you migrated from docker-sync 0.1.x)"
  end
  return path
end

.project_ensure_configuration_version_compatibility(config) ⇒ Object



47
48
49
50
51
# File 'lib/docker-sync/config.rb', line 47

def self.project_ensure_configuration_version_compatibility(config)
  return false unless config.key?('version')
  return false if config['version'].to_s != project_required_config_version.to_s
  return true
end

.project_required_config_versionObject



43
44
45
# File 'lib/docker-sync/config.rb', line 43

def self.project_required_config_version
  return '2'
end

Instance Method Details

#initialize(options) ⇒ Object



10
11
12
# File 'lib/docker-sync/config.rb', line 10

def initialize(options)
  Dotenv.load
end