lib/renuo/cli/app/secrets_fetcher.rb in renuo-cli-4.3.0 vs lib/renuo/cli/app/secrets_fetcher.rb in renuo-cli-4.5.0
- old
+ new
@@ -18,23 +18,63 @@
end
end
private
- def elaborate_item(item)
+ def elaborate_item(item) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
private_link = item[:private_link]
- files = item[:files]
- default_folder = item[:default_folder]
+ if private_link.nil?
+ warn 'Private link not found in your config file.'
+ return
+ elsif private_link.includes?('share.1password.com')
+ warn 'Please use a private link, not a share link.'
+ return
+ end
+
item_id = extract_item_id(private_link)
- item = get_item(item_id)
- return if item.nil?
+ item_json = get_item(item_id)
+ return if item_json.nil?
- files.each do |file|
- process_files(file[:folder] || default_folder, file[:name], item, item_id)
+ item[:env_variables].each do |env_variable|
+ process_env_variable(env_variable[:name], item_id, item_json)
end
+
+ item[:files].each do |file|
+ process_file(file[:folder] || item[:default_folder], file[:name], item_json, item_id)
+ end
end
+ def process_env_variable(name, item_id, item_json)
+ env_json = item_json['fields'].find { |field| field['label'] == name }
+ if env_json.nil?
+ warn "Field `#{name}` not found in item #{item_id}. Check the field name in your #{CONFIG_FILE} file."
+ else
+ value = env_json['value']
+ update_or_add_variable('.env', name, /^#{name}=/, "#{name}=#{value}")
+ update_or_add_variable('config/application.yml', name, /^#{name}:.*$/, "#{name}: \"#{value}\"")
+
+ end
+ end
+
+ def update_or_add_variable(file_path, name, pattern, replacement) # rubocop:disable Metrics/MethodLength
+ return unless File.exist?(file_path)
+
+ file_contents = File.read(file_path)
+ updated_contents = file_contents
+
+ if file_contents.match(pattern)
+ if ask("Do you want to update the value of #{name} in #{file_path}? (y/n)", 'y') == 'y'
+ updated_contents = file_contents.gsub(pattern, replacement)
+ end
+ else
+ puts "Added environment variable #{name} to #{file_path}"
+ updated_contents = "#{file_contents}#{replacement}\n"
+ end
+
+ File.write(file_path, updated_contents)
+ end
+
def get_item(item_id)
output = execute_command(command: "op item get #{item_id} --format json",
success_message: '',
error_message: "Error fetching item #{item_id}." \
"Check `private_link` in your #{CONFIG_FILE} file.")
@@ -53,10 +93,10 @@
def extract_item_id(private_link)
/&i=([^&]+)/.match(private_link)[1]
end
- def process_files(output_folder, filename, item, item_id)
+ def process_file(output_folder, filename, item, item_id)
FileUtils.mkdir_p(output_folder)
output_path = File.join(output_folder, filename)
vault_id = item.dig('vault', 'id')
download_file_from_item(item['files'], vault_id, item_id, filename, output_path)