From 0487a796f336d3a02cb6be3ac2c4e5d1be3a986e Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Thu, 16 Jan 2020 20:30:43 +0100 Subject: [PATCH] conversion script compatible with the new DODB API --- altsrc/common.cr | 13 +------------ altsrc/main.cr | 25 +++++++++++++++---------- conversion.cr | 34 ++++++++++++++++++++++++++++++---- 3 files changed, 46 insertions(+), 26 deletions(-) diff --git a/altsrc/common.cr b/altsrc/common.cr index 4001649..facd385 100644 --- a/altsrc/common.cr +++ b/altsrc/common.cr @@ -5,17 +5,7 @@ class TodoD end class TodoD::List - enum PermissionLevel - None = -1 - Read - Post - Edit - Admin - - def to_json(o) - to_s.downcase.to_json(o) - end - end + alias PermissionLevel = ::AuthD::User::PermissionLevel JSON.mapping({ id: String, @@ -33,7 +23,6 @@ class TodoD::List @permissions = { "admin" => [user.uid], "edit" => [] of Int32, - "post" => [] of Int32, "read" => [] of Int32 } @tasks = [] of String diff --git a/altsrc/main.cr b/altsrc/main.cr index fd79f24..9a104fe 100644 --- a/altsrc/main.cr +++ b/altsrc/main.cr @@ -2,27 +2,32 @@ require "option_parser" require "uuid" require "uuid/json" - - +require "ipc" +require "authd" require "dodb" - +require "./todod.cr" class TodoD::Storage getter root : String - getter lists : DODB::DataBase(String, List) - getter tasks : DODB::DataBase(String, Task) + getter lists : DODB::DataBase(List) + getter lists_by_id : DODB::Index(List) getter lists_per_user : DODB::Tags(List) - def initialize(@root) - @lists = DODB::DataBase(String, List).new("#{@root}/lists") - @lists_per_user = @lists.new_tags "user", &.users_with_read_permissions.map(&.to_s) + getter tasks : DODB::DataBase(Task) + getter tasks_by_id : DODB::Index(Task) - @tasks = DODB::DataBase(String, Task).new("#{@root}/tasks") + def initialize(@root) + @lists = DODB::DataBase(List).new("#{@root}/lists") + @lists_per_user = @lists.new_tags "user", &.users_with_read_permissions.map(&.to_s) + @lists_by_id = @lists.new_index "id", &.id + + @tasks = DODB::DataBase(Task).new("#{@root}/tasks") + @tasks_by_id = @tasks.new_index "id", &.id end def new_list(list) - @lists[list.id] = list + @lists << list end end diff --git a/conversion.cr b/conversion.cr index 2443c9c..26f6e73 100644 --- a/conversion.cr +++ b/conversion.cr @@ -2,12 +2,38 @@ require "json" require "./src/project.cr" +require "option_parser" + require "./altsrc/common.cr" require "./altsrc/main.cr" +new_storage_dir = "./new-storage" +old_storage_dir = "./old-storage" + +OptionParser.parse do |parser| + parser.on "-n new-storage-directory", + "--new-directory storage-directory", + "The new directory where to put converted files." do |opt| + new_storage_dir = opt + end + + parser.on "-o old-storage-directory", + "--old-storage-directory directory", + "Old storage directory, to convert." do |opt| + old_storage_dir = opt + end + + parser.on "-h", "--help", "Show this help" do + puts parser + puts "Default values are:" + pp! new_storage_dir + pp! old_storage_dir + exit 0 + end +end # 1. get the old list of projects -old_storage = Project.all "../storage/" +old_storage = Project.all old_storage_dir # PROJECT: JSON.mapping({ # PROJECT: id: String, @@ -30,7 +56,7 @@ old_storage = Project.all "../storage/" # TASKS: }) # 2. create the new storage -new_storage = TodoD::Storage.new "../new-storage/" +new_storage = TodoD::Storage.new new_storage_dir # 3. for each project (old name for "list") @@ -91,7 +117,7 @@ old_storage.each do |old_project| # 3. store the new task tasks << newtask - new_storage.tasks[newtask.id] = newtask + new_storage.tasks << newtask end # finally, create the list with all the parameters @@ -105,5 +131,5 @@ old_storage.each do |old_project| pp! list # finally, store the new list - new_storage.lists[list.id] = list + new_storage.lists << list end