Class: Confswap::Command
- Inherits:
-
Clamp::Command
- Object
- Clamp::Command
- Confswap::Command
- Defined in:
- lib/confswap/command.rb
Instance Method Summary collapse
- #execute ⇒ Object
- #gather_variables ⇒ Object
- #help(*args) ⇒ Object
-
#initialize(*args) ⇒ Command
constructor
A new instance of Command.
- #parse_envvars ⇒ Object
- #process_envvars ⇒ Object
- #run(*args) ⇒ Object
- #save_as_yaml ⇒ Object
- #swap_config(configuration_filename) ⇒ Object
- #write_file(output_file_contents, output_filename) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Command
Returns a new instance of Command.
8 9 10 11 |
# File 'lib/confswap/command.rb', line 8 def initialize *args @env_variables = {} super *args end |
Instance Method Details
#execute ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# 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 ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# 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) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/confswap/command.rb', line 13 def help *args return [ "This is confswap version #{Confswap::VERSION}", super ].join("\n") end |
#parse_envvars ⇒ Object
109 110 111 112 113 |
# 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 ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# 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) ⇒ Object
20 21 22 |
# File 'lib/confswap/command.rb', line 20 def run *args super *args end |
#save_as_yaml ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 |
# 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) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# 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.}. 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
115 116 117 118 119 120 121 122 123 124 |
# 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 |