Started adding the "actual" migration

This commit is contained in:
kolaente 2019-10-20 14:20:01 +02:00
parent 754f66c9c8
commit 5eb5407ca4
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 108 additions and 3 deletions

111
main.go
View File

@ -1,14 +1,19 @@
package main
import (
"bufio"
sdk "code.vikunja.io/go-sdk"
"context"
"fmt"
"log"
"os"
"strings"
)
func main() {
const listID = 19
client := sdk.NewAPIClient(&sdk.Configuration{
BasePath: "http://localhost:8080/api/v1",
DefaultHeader: make(map[string]string),
@ -29,9 +34,109 @@ func main() {
Prefix: "Bearer",
})
lists, _, err := client.ListApi.ListsGet(auth, nil)
// Read the md file
lines, err := scanLines("../api/Featurecreep.md")
if err != nil {
log.Fatal("Error getting lists: ", err)
log.Fatal("Error reading file: ", err)
}
fmt.Println(lists)
tasks := make([]*sdk.ModelsTask, 0, len(lines))
mapByHeadline := make(map[string][]string)
var lastHeadline string
var notApplicable []string
var lastTask *sdk.ModelsTask
var taskRelations []*sdk.ModelsTaskRelation
for in, l := range lines {
if l == "" {
continue
}
if strings.HasPrefix(l, "#") {
lastHeadline = strings.TrimSpace(strings.ReplaceAll(l, "#", ""))
mapByHeadline[lastHeadline] = []string{}
continue
}
// Only add tasks to the list
if strings.HasPrefix(l, "* [") {
t := &sdk.ModelsTask{
Text: l[5:],
Done: strings.HasPrefix(l, "* [x]"),
Labels: []sdk.ModelsLabel{
{
Title: lastHeadline,
},
},
}
*t, _, err = client.TaskApi.ListsIdPut(auth, listID, *t)
if err != nil {
log.Fatal("Error creating task: ", err)
}
log.Printf("Created task %d of %d", in, len(lines))
// Subtask handling
lastTask = t
tasks = append(tasks, t)
continue
}
// Subtask handling
// If we have a line starting with two spaces and then task, we have a subtask of the last task
if strings.HasPrefix(l, " * [") {
subtask := &sdk.ModelsTask{
Text: l[7:],
Done: strings.HasPrefix(l, " * [x]"),
Labels: []sdk.ModelsLabel{
{
Title: lastHeadline,
},
},
}
*subtask, _, err = client.TaskApi.ListsIdPut(auth, listID, *subtask)
if err != nil {
log.Fatal("Error creating task: ", err)
}
log.Printf("Created task %d of %d", in, len(lines))
tasks = append(tasks, subtask)
taskRelations = append(taskRelations, &sdk.ModelsTaskRelation{
TaskId: lastTask.Id,
OtherTaskId: subtask.Id,
RelationKind: "subtask",
})
continue
}
notApplicable = append(notApplicable, l)
}
// TODO: update all labels afterwards
// TODO: create all relations afterwards
fmt.Println(mapByHeadline)
}
func scanLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, nil
}