Migrate migration

This commit is contained in:
Timur Ismagilov 2024-09-07 22:51:08 +03:00
parent 191c18fd9e
commit b3626ee546
3 changed files with 31 additions and 19 deletions

View File

@ -1,11 +1,13 @@
package migration package migration
import ( import (
"git.sr.ht/~bouncepaw/mycomarkup/v5/tools"
"github.com/bouncepaw/mycorrhiza/internal/files"
"io/ioutil" "io/ioutil"
"log" "log/slog"
"os" "os"
"github.com/bouncepaw/mycorrhiza/internal/files"
"git.sr.ht/~bouncepaw/mycomarkup/v5/tools"
) )
var headingMarkerPath string var headingMarkerPath string
@ -29,7 +31,8 @@ func shouldMigrateHeadings() bool {
return true return true
} }
if err != nil { if err != nil {
log.Fatalln("When checking if heading migration is needed:", err.Error()) slog.Error("Failed to check if heading migration is needed", "err", err)
os.Exit(1)
} }
_ = file.Close() _ = file.Close()
return false return false
@ -42,6 +45,7 @@ func createHeadingMarker() {
0766, 0766,
) )
if err != nil { if err != nil {
log.Fatalln(err) slog.Error("Failed to create heading migration marker", "err", err)
os.Exit(1)
} }
} }

View File

@ -8,14 +8,14 @@
package migration package migration
import ( import (
"github.com/bouncepaw/mycorrhiza/internal/user"
"io" "io"
"log" "log/slog"
"os" "os"
"strings" "strings"
"github.com/bouncepaw/mycorrhiza/history" "github.com/bouncepaw/mycorrhiza/history"
"github.com/bouncepaw/mycorrhiza/internal/hyphae" "github.com/bouncepaw/mycorrhiza/internal/hyphae"
"github.com/bouncepaw/mycorrhiza/internal/user"
) )
func genericLineMigrator( func genericLineMigrator(
@ -37,7 +37,8 @@ func genericLineMigrator(
file, err := os.OpenFile(hypha.TextFilePath(), os.O_RDWR, 0766) file, err := os.OpenFile(hypha.TextFilePath(), os.O_RDWR, 0766)
if err != nil { if err != nil {
hop.WithErrAbort(err) hop.WithErrAbort(err)
log.Fatal("Something went wrong when opening ", hypha.TextFilePath(), ": ", err.Error()) slog.Error("Failed to open text part file", "path", hypha.TextFilePath(), "err", err)
os.Exit(1)
} }
var buf strings.Builder var buf strings.Builder
@ -45,7 +46,7 @@ func genericLineMigrator(
if err != nil { if err != nil {
hop.WithErrAbort(err) hop.WithErrAbort(err)
_ = file.Close() _ = file.Close()
log.Fatal("Something went wrong when reading ", hypha.TextFilePath(), ": ", err.Error()) slog.Error("Failed to read text part file", "path", hypha.TextFilePath(), "err", err)
} }
var ( var (
@ -59,21 +60,24 @@ func genericLineMigrator(
if err != nil { if err != nil {
hop.WithErrAbort(err) hop.WithErrAbort(err)
_ = file.Close() _ = file.Close()
log.Fatal("Something went wrong when truncating ", hypha.TextFilePath(), ": ", err.Error()) slog.Error("Failed to truncate text part file", "path", hypha.TextFilePath(), "err", err)
os.Exit(1)
} }
_, err = file.Seek(0, 0) _, err = file.Seek(0, 0)
if err != nil { if err != nil {
hop.WithErrAbort(err) hop.WithErrAbort(err)
_ = file.Close() _ = file.Close()
log.Fatal("Something went wrong when seeking in ", hypha.TextFilePath(), ": ", err.Error()) slog.Error("Failed to seek in text part file", "path", hypha.TextFilePath(), "err", err)
os.Exit(1)
} }
_, err = file.WriteString(newText) _, err = file.WriteString(newText)
if err != nil { if err != nil {
hop.WithErrAbort(err) hop.WithErrAbort(err)
_ = file.Close() _ = file.Close()
log.Fatal("Something went wrong when writing to ", hypha.TextFilePath(), ": ", err.Error()) slog.Error("Failed to write to text part file", "path", hypha.TextFilePath(), "err", err)
os.Exit(1)
} }
} }
_ = file.Close() _ = file.Close()
@ -85,8 +89,8 @@ func genericLineMigrator(
} }
if hop.WithFiles(mycoFiles...).Apply().HasErrors() { if hop.WithFiles(mycoFiles...).Apply().HasErrors() {
log.Fatal(commitErrorMessage, hop.FirstErrorText()) slog.Error(commitErrorMessage + hop.FirstErrorText())
} }
log.Println("Migrated", len(mycoFiles), "Mycomarkup documents") slog.Info("Migrated Mycomarkup documents", "n", len(mycoFiles))
} }

View File

@ -1,11 +1,13 @@
package migration package migration
import ( import (
"git.sr.ht/~bouncepaw/mycomarkup/v5/tools"
"github.com/bouncepaw/mycorrhiza/internal/files"
"io/ioutil" "io/ioutil"
"log" "log/slog"
"os" "os"
"github.com/bouncepaw/mycorrhiza/internal/files"
"git.sr.ht/~bouncepaw/mycomarkup/v5/tools"
) )
var rocketMarkerPath string var rocketMarkerPath string
@ -33,7 +35,8 @@ func shouldMigrateRockets() bool {
return true return true
} }
if err != nil { if err != nil {
log.Fatalln("When checking if rocket migration is needed:", err.Error()) slog.Error("Failed to check if rocket migration is needed", "err", err)
os.Exit(1)
} }
_ = file.Close() _ = file.Close()
return false return false
@ -46,6 +49,7 @@ func createRocketLinkMarker() {
0766, 0766,
) )
if err != nil { if err != nil {
log.Fatalln(err) slog.Error("Failed to create rocket link migration marker")
os.Exit(1)
} }
} }