Class: Confswap::Command

Inherits:
Clamp::Command
  • Object
show all
Defined in:
lib/confswap/command.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Command

Returns a new instance of Command.



7
8
9
# File 'lib/confswap/command.rb', line 7

def initialize *args
  super *args
end

Instance Method Details

#executeObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/confswap/command.rb', line 22

def execute
  if version?
    puts Confswap::VERSION
    return 0 
  end

  if configuration_filename.nil?
    puts 'Specify a template file or use --help for usage information'
    return 0
  end

  if File.exists? configuration_filename
    swap_config configuration_filename
    return 0
  else
    puts "Error: Configuration template file with name #{configuration_filename} does not exist"
    return 1
  end
end

#help(*args) ⇒ Object



11
12
13
14
15
16
# File 'lib/confswap/command.rb', line 11

def help(*args)
  return [
    "This is confswap version #{Confswap::VERSION}",
    super
  ].join("\n")
end

#run(*args) ⇒ Object



18
19
20
# File 'lib/confswap/command.rb', line 18

def run *args
  super *args
end

#swap_config(configuration_filename) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/confswap/command.rb', line 42

def swap_config configuration_filename
  output_filename_default = configuration_filename + '.out' if output_filename.nil?

  configuration_template = Confswap::ConfigurationFileReader.read configuration_filename
  env_variables = Confswap::EnvironmentVariableReader.read_variables

  if envvars
    envvars.each {|envvar|
      key_and_value = envvar.split(/=/, 2)
      key = key_and_value.first
      value = key_and_value.last
      if key_and_value.count != 2
        puts "Invalid envvar specified #{key} and #{value}"
        return 1 
      end

      env_variables[key.to_sym]=value
    }   

    if verbose?
      puts "Environment variables:"
      env_variables.each { |var|
        puts "#{var.first} = #{var.last}"
      }
    end
  end

  if !property_file.nil? and File.exists? property_file
    puts 'pfile specified'
    env_variables = Confswap::PropertyFileVariableReader.read_variables_from_file property_file
    p env_variables
  end
  
  begin
    output = configuration_template % env_variables
  rescue KeyError => error
    puts "#{error.message}.  Your configuration requires this variable, but no environment variable was found or specified."
    exit(1)
  end

  write_file output, output_filename || output_filename_default
end

#write_file(output_file_contents, output_filename) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/confswap/command.rb', line 85

def write_file output_file_contents, output_filename
  return File.write output_filename, output_file_contents unless File.exists? output_filename
  
  if File.exists? output_filename and force?
    puts "Overwriting #{output_filename}..."
    File.write output_filename, output_file_contents
  else
    puts "#{output_filename} already exists, use the --force flag to overwrite"
  end
end