class Confswap::Command
Public Class Methods
new(*args)
click to toggle source
Calls superclass method
# File lib/confswap/command.rb, line 8 def initialize *args @env_variables = {} super *args end
Public Instance Methods
execute()
click to toggle source
# File lib/confswap/command.rb, line 24 def execute if version? puts Confswap::VERSION return 0 end if yaml? save_as_yaml() 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
gather_variables()
click to toggle source
# File lib/confswap/command.rb, line 49 def gather_variables @env_variables = Confswap::EnvironmentVariableReader.read_variables unless ignore_persisted_shell_variables? if !property_file.nil? and File.exists? property_file @env_variables = Confswap::PropertyFileVariableReader.read_variables_from_file property_file puts @env_variables end process_envvars() if envvars end
help(*args)
click to toggle source
Calls superclass method
# File lib/confswap/command.rb, line 13 def help *args return [ "This is confswap version #{Confswap::VERSION}", super ].join("\n") end
parse_envvars()
click to toggle source
# File lib/confswap/command.rb, line 109 def parse_envvars parsed_variables = Confswap::BashParser.parse_user_input @env_variables puts "Interpreted user variables: #{parsed_variables}" if verbose? parsed_variables end
process_envvars()
click to toggle source
# File lib/confswap/command.rb, line 76 def process_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
run(*args)
click to toggle source
Calls superclass method
# File lib/confswap/command.rb, line 20 def run *args super *args end
save_as_yaml()
click to toggle source
# File lib/confswap/command.rb, line 97 def save_as_yaml gather_variables() @env_variables = parse_envvars() output_filename_yaml = output_filename || 'conf.yaml' puts "Writing #{output_filename_yaml}" env_variables_yaml = @env_variables.to_yaml write_file env_variables_yaml, output_filename_yaml end
swap_config(configuration_filename)
click to toggle source
# File lib/confswap/command.rb, line 60 def swap_config configuration_filename output_filename_default = configuration_filename + '.out' if output_filename.nil? configuration_template = Confswap::ConfigurationFileReader.read configuration_filename gather_variables() 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)
click to toggle source
# File lib/confswap/command.rb, line 115 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