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
# 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 (!property_file.nil?) || (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 specifies this variable, but it was not found as an environment variable."
    exit(1)
  end

  write_file output, output_filename || output_filename_default
end

#write_file(output_file_contents, output_filename) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/confswap/command.rb', line 64

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