Method: OpenC3::CliGenerator.generate_microservice

Defined in:
lib/openc3/utilities/cli_generator.rb

.generate_microservice(args) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/openc3/utilities/cli_generator.rb', line 166

def self.generate_microservice(args)
  if args.length < 2 or args.length > 3
    abort("Usage: cli generate #{args[0]} <NAME> (--ruby or --python)")
  end

  # Create the local variables
  microservice_name = args[1].upcase.gsub(/_+|-+/, '_')
  microservice_path = "microservices/#{microservice_name}"
  if File.exist?(microservice_path)
    abort("Microservice #{microservice_path} already exists!")
  end
  microservice_filename = "#{microservice_name.downcase}.#{@@language}"
  microservice_class = microservice_filename.filename_to_class_name

  process_template("#{TEMPLATES_DIR}/microservice", binding) do |filename|
    # Rename the template MICROSERVICE to our actual microservice name
    filename.sub!("microservices/TEMPLATE", "microservices/#{microservice_name}")
    filename.sub!("microservice.#{@@language}", microservice_filename)
    false
  end

  cmd_line = "CMD ruby #{microservice_name.downcase}.rb"
  if @@language == 'py'
    cmd_line = "CMD python #{microservice_name.downcase}.py"
  end

  # Add this microservice to plugin.txt
  File.open("plugin.txt", 'a') do |file|
    file.puts <<~DOC

      MICROSERVICE #{microservice_name} #{microservice_name.downcase.gsub('_','-')}-microservice
        #{cmd_line}
    DOC
  end

  puts "Microservice #{microservice_name} successfully generated!"
  return microservice_name
end