diff options
Diffstat (limited to 'lib/gitlab/import_export/json/ndjson_writer.rb')
-rw-r--r-- | lib/gitlab/import_export/json/ndjson_writer.rb | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/lib/gitlab/import_export/json/ndjson_writer.rb b/lib/gitlab/import_export/json/ndjson_writer.rb new file mode 100644 index 00000000000..e74fdd74049 --- /dev/null +++ b/lib/gitlab/import_export/json/ndjson_writer.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +module Gitlab + module ImportExport + module JSON + class NdjsonWriter + include Gitlab::ImportExport::CommandLineUtil + + def initialize(dir_path) + @dir_path = dir_path + end + + def close + end + + def write_attributes(exportable_path, hash) + # It will create: + # tree/project.json + with_file("#{exportable_path}.json") do |file| + file.write(hash.to_json) + end + end + + def write_relation(exportable_path, relation, value) + # It will create: + # tree/project/ci_cd_setting.ndjson + with_file(exportable_path, "#{relation}.ndjson") do |file| + file.write(value.to_json) + end + end + + def write_relation_array(exportable_path, relation, items) + # It will create: + # tree/project/merge_requests.ndjson + with_file(exportable_path, "#{relation}.ndjson") do |file| + items.each do |item| + file.write(item.to_json) + file.write("\n") + end + end + end + + private + + def with_file(*path) + file_path = File.join(@dir_path, *path) + raise ArgumentError, "The #{file_path} already exist" if File.exist?(file_path) + + # ensure that path is created + mkdir_p(File.dirname(file_path)) + + File.open(file_path, "wb") do |file| + yield(file) + end + end + end + end + end +end |