mirror of https://github.com/captn3m0/outliner.git
72 lines
2.0 KiB
Ruby
Executable File
72 lines
2.0 KiB
Ruby
Executable File
#!/usr/bin/env ruby
|
|
# frozen_string_literal: true
|
|
|
|
require 'bundler/setup'
|
|
require 'outliner'
|
|
|
|
def validate
|
|
raise 'Missing arguments' if ARGV.size != 2
|
|
raise 'Invalid directory' unless Dir.exist?(ARGV[0])
|
|
raise 'Invalid collection' unless ARGV[1].match(/\w+/)
|
|
raise 'OUTLINE_BASE_URI not set' unless ENV.key?('OUTLINE_BASE_URI')
|
|
raise 'OUTLINE_TOKEN not set' unless ENV.key?('OUTLINE_TOKEN')
|
|
end
|
|
|
|
def create_documents_recursively(directory, collection_id, parent_document_id = nil)
|
|
cwd = Dir.pwd
|
|
Dir.chdir directory
|
|
# Create all documents for this directory
|
|
Dir['*.md'].each do |file|
|
|
params = {
|
|
title: file[0...-3],
|
|
text: file[0...-3] +
|
|
"\n" +
|
|
File.read(file) +
|
|
"\n\n---\nImported at #{Time.now}",
|
|
collectionId: collection_id,
|
|
publish: true
|
|
}
|
|
|
|
params[:parentDocumentId] = parent_document_id if parent_document_id
|
|
CLIENT.documents_create(params)
|
|
puts "[-] #{file}"
|
|
end
|
|
|
|
# Create child documents for each sub-directory
|
|
Dir.glob('*').select { |f| File.directory? f }.each do |dir|
|
|
puts "[-] #{dir}"
|
|
params = {
|
|
title: dir,
|
|
text: dir + "\nImported at #{Time.now}",
|
|
collectionId: collection_id,
|
|
publish: true,
|
|
parentDocumentId: parent_document_id
|
|
}
|
|
response = CLIENT.documents_create(params)
|
|
create_documents_recursively(dir, collection_id, response['data']['id'])
|
|
end
|
|
Dir.chdir cwd
|
|
end
|
|
|
|
# Run validations
|
|
validate
|
|
|
|
# Setup variables
|
|
local_directory = ARGV[0]
|
|
remote_collection_name = ARGV[1]
|
|
|
|
# Create a root collection
|
|
CLIENT = Outliner::Client.new ENV['OUTLINE_BASE_URI']
|
|
root_collection_id = CLIENT.find_or_create_collection(remote_collection_name)
|
|
|
|
begin
|
|
create_documents_recursively(local_directory, root_collection_id)
|
|
puts '[S] Import successful'
|
|
rescue StandardError? => e
|
|
# If we fail, print an error, and delete the collection
|
|
puts "[E] Import failed with error: #{e.message}"
|
|
CLIENT.collections_delete(id: root_collection_id)
|
|
puts '[E] Deleted collection, please report the issue or retry'
|
|
exit 1
|
|
end
|