Module: Awsam::Utils

Defined in:
lib/awsam/utils.rb

Class Method Summary collapse

Class Method Details

.bash_environ(envs, set_export = true) ⇒ Object

Print the appropriate environment variables set commands for bash



20
21
22
23
24
# File 'lib/awsam/utils.rb', line 20

def self::bash_environ(envs, set_export = true)
  envs.each_pair do |k, v|
    puts "%s#{k}=\"#{v}\"" % [set_export ? "export " : ""]
  end
end

.bash_unset_environ(envs) ⇒ Object

Unset each of the environ settings to clear the environ



13
14
15
16
17
# File 'lib/awsam/utils.rb', line 13

def self.bash_unset_environ(envs)
  envs.each_pair do |k, v|
    puts "unset #{k}"
  end
end

.confdir_scan(dir) ⇒ Object

Scan a directory yielding for each file



5
6
7
8
9
10
# File 'lib/awsam/utils.rb', line 5

def self.confdir_scan(dir)
  Dir.entries(dir).each do |name|
    next if name == '.' || name == '..' || name == Awsam::DEFAULT_LINK_NAME
    yield(name)
  end
end

.get_default(basedir) ⇒ Object

Get the target of the default link



47
48
49
50
# File 'lib/awsam/utils.rb', line 47

def self.get_default(basedir)
  link = File.join(basedir, Awsam::DEFAULT_LINK_NAME)
  File.exist?(link) ? File.readlink(link) : nil
end

.remove_default(basedir) ⇒ Object

Remove the default link



53
54
55
# File 'lib/awsam/utils.rb', line 53

def self.remove_default(basedir)
  FileUtils.rm File.join(basedir, Awsam::DEFAULT_LINK_NAME)
end

.set_default(basedir, target) ⇒ Object

Set the default resource with link directory and target



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/awsam/utils.rb', line 27

def self.set_default(basedir, target)
  link = File.join(basedir, Awsam::DEFAULT_LINK_NAME)
  if File.exist?(link)
    begin
      FileUtils.rm(link)
    rescue => err
      $stderr.puts "Failed to remove link #{link}: #{err.message}"
      return false
    end
  end
  begin
    FileUtils.ln_s(target, link)
  rescue => err
    $stderr.puts "Failed to create symlink: #{err.message}"
    return false
  end
  true
end