Class: Confmake::Command
- Inherits:
-
Clamp::Command
- Object
- Clamp::Command
- Confmake::Command
- Defined in:
- lib/confmake/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_json ⇒ 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.
9 10 11 12 |
# File 'lib/confmake/command.rb', line 9 def initialize *args @env_variables = {} super *args end |
Instance Method Details
#execute ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/confmake/command.rb', line 25 def execute if version? puts Confmake::VERSION return 0 end if yaml? save_as_yaml() return 0 end if json? save_as_json() 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
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/confmake/command.rb', line 55 def gather_variables @env_variables = Confmake::EnvironmentVariableReader.read_variables if include_persisted_shell_variables? if !property_file.nil? and File.exists? property_file @env_variables = Confmake::PropertyFileVariableReader.read_variables_from_file property_file puts @env_variables end process_envvars() if envvars end |
#help(*args) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/confmake/command.rb', line 14 def help *args return [ "This is confmake version #{Confmake::VERSION}", super ].join("\n") end |
#parse_envvars ⇒ Object
117 118 119 120 121 122 |
# File 'lib/confmake/command.rb', line 117 def parse_envvars gather_variables() parsed_variables = Confmake::BashParser.parse_user_input @env_variables puts "Interpreted user variables: #{parsed_variables}" if verbose? parsed_variables end |
#process_envvars ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/confmake/command.rb', line 82 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
21 22 23 |
# File 'lib/confmake/command.rb', line 21 def run *args super *args end |
#save_as_json ⇒ Object
110 111 112 113 114 115 |
# File 'lib/confmake/command.rb', line 110 def save_as_json @env_variables = parse_envvars() puts "Writing #{output_filename}" env_variables_json = @env_variables.to_json write_file env_variables_json, output_filename end |
#save_as_yaml ⇒ Object
103 104 105 106 107 108 |
# File 'lib/confmake/command.rb', line 103 def save_as_yaml @env_variables = parse_envvars() puts "Writing #{output_filename}" env_variables_yaml = @env_variables.to_yaml write_file env_variables_yaml, output_filename end |
#swap_config(configuration_filename) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/confmake/command.rb', line 66 def swap_config configuration_filename output_filename_default = configuration_filename + '.out' if output_filename.nil? configuration_template = Confmake::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
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/confmake/command.rb', line 124 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 |