Class: VagrantProperties::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-properties/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#properties_path=(value) ⇒ Object (writeonly)

Sets the attribute properties_path

Parameters:

  • value

    the value to set the attribute properties_path to.



6
7
8
# File 'lib/vagrant-properties/config.rb', line 6

def properties_path=(value)
  @properties_path = value
end

Class Method Details

.build_propertiesObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vagrant-properties/config.rb', line 37

def build_properties
  load_properties.each_with_object({}) do |(name, property), memo|
    if repo_valide?(property['repo'])
      property['path'] = pull_project(property['repo'])
    end

    if !domains_valid?(property['domains']) && hostname_valid?(property['hostname'])
      property['domains'] = [property['hostname']]
    end

    if ip_valid?(property['ip']) && domains_valid?(property['domains'])
      write_to_hosts(property['ip'], property['domains'])
    end

    if ip_valid?(property['vip']) && domains_valid?([property['vip_domain']])
      write_to_hosts(property['vip'], [property['vip_domain']])
    end

    keys = property.keys.inject([]) { |m, k| m << k.to_sym }
    memo[name.to_sym] = Struct.new(*keys).new(*property.values)
  end
end

.domains_valid?(domains) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/vagrant-properties/config.rb', line 25

def domains_valid?(domains)
  domains && domains.is_a?(Array) && !domains.empty?
end

.ghq?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/vagrant-properties/config.rb', line 105

def ghq?
  @ghq ||= `which ghq 1>/dev/null ; echo $?`.to_i == 0
end

.ghq_get(path, repo) ⇒ Object



97
98
99
# File 'lib/vagrant-properties/config.rb', line 97

def ghq_get(path, repo)
  `test ! -d #{path} && ghq get #{repo}`
end

.git_clone(path, repo) ⇒ Object



101
102
103
# File 'lib/vagrant-properties/config.rb', line 101

def git_clone(path, repo)
  `test ! -d #{path} && git clone --recursive #{repo} #{path}`
end

.hostname_valid?(hostname) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/vagrant-properties/config.rb', line 29

def hostname_valid?(hostname)
  hostname && hostname.is_a?(String) && !hostname.empty?
end

.ip_valid?(ip) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/vagrant-properties/config.rb', line 33

def ip_valid?(ip)
  ip && ip.is_a?(String) && !ip.empty?
end

.load_propertiesObject



64
65
66
# File 'lib/vagrant-properties/config.rb', line 64

def load_properties
  YAML.load_file properties_path
end

.path_matcherObject



93
94
95
# File 'lib/vagrant-properties/config.rb', line 93

def path_matcher
  %r|([\w\-\.]*)[:/]([\w\-]*)/([\w\-\.]*)\.git|
end

.propertiesObject



17
18
19
# File 'lib/vagrant-properties/config.rb', line 17

def properties
  @properties ||= build_properties
end

.properties_pathObject



60
61
62
# File 'lib/vagrant-properties/config.rb', line 60

def properties_path
  @properties_path ||= 'vagrant_properties.yml'
end

.pull_project(repo) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/vagrant-properties/config.rb', line 68

def pull_project(repo)
  matched = repo.match(path_matcher)

  if ghq?
    ghq_root = `ghq root`.chomp

    if ghq_root == "No help topic for 'root'"
      raise StandardError.new '"ghq root" not found. please update ghq'
    end

    path = "#{ghq_root}/#{matched[1..3].join('/')}"
    ghq_get path, repo
  else
    path = "../#{matched[3]}"
    git_clone path, repo
  end

  path
end

.repo_valide?(repo) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/vagrant-properties/config.rb', line 21

def repo_valide?(repo)
  repo && repo.is_a?(String) && !repo.empty?
end

.write_to_hosts(ip, domains) ⇒ Object



88
89
90
91
# File 'lib/vagrant-properties/config.rb', line 88

def write_to_hosts(ip, domains)
  `test 0 -ne $(cat /etc/hosts | grep -q #{ip} ; echo $?) && \
    echo "#{ip} #{domains.join(' ')}" | sudo tee -a /etc/hosts`
end

Instance Method Details

#named(key) ⇒ Object



12
13
14
# File 'lib/vagrant-properties/config.rb', line 12

def named(key)
  self.class.properties[key.to_sym]
end

#to_hashObject



8
9
10
# File 'lib/vagrant-properties/config.rb', line 8

def to_hash
  self.class.properties
end