diff --git a/go.mod b/go.mod index 011f5dc..cf709a1 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/bouncepaw/mycorrhiza go 1.18 require ( - github.com/bouncepaw/mycomarkup/v4 v4.0.2 + github.com/bouncepaw/mycomarkup/v4 v4.1.0 github.com/go-ini/ini v1.63.2 github.com/gorilla/feeds v1.1.1 github.com/gorilla/mux v1.8.0 diff --git a/go.sum b/go.sum index 9a2ac90..a1148c5 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= github.com/andybalholm/brotli v1.0.3/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= -github.com/bouncepaw/mycomarkup/v4 v4.0.2 h1:pS2ZcQchkKimqHQjalqJu/alHesTkPYWZtAUJD87fMI= -github.com/bouncepaw/mycomarkup/v4 v4.0.2/go.mod h1:y0b8U6Xfnh3KfNUpG3QuAXRJwqFPPpmS2kYvLzaf688= +github.com/bouncepaw/mycomarkup/v4 v4.1.0 h1:HltmWtt5RREcSDpxVzgYhVB8XgaqgY/nbDiEr0t1dq0= +github.com/bouncepaw/mycomarkup/v4 v4.1.0/go.mod h1:y0b8U6Xfnh3KfNUpG3QuAXRJwqFPPpmS2kYvLzaf688= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-ini/ini v1.63.2 h1:kwN3umicd2HF3Tgvap4um1ZG52/WyKT9GGdPx0CJk6Y= diff --git a/main.go b/main.go index fa073cd..72d9a41 100644 --- a/main.go +++ b/main.go @@ -48,6 +48,7 @@ func main() { history.Start() history.InitGitRepo() migration.MigrateRocketsMaybe() + migration.MigrateHeadingsMaybe() shroom.SetHeaderLinks() categories.Init() diff --git a/migration/headings.go b/migration/headings.go new file mode 100644 index 0000000..1693ce6 --- /dev/null +++ b/migration/headings.go @@ -0,0 +1,47 @@ +package migration + +import ( + "github.com/bouncepaw/mycomarkup/v4/tools" + "github.com/bouncepaw/mycorrhiza/files" + "io/ioutil" + "log" + "os" +) + +var headingMarkerPath string + +func MigrateHeadingsMaybe() { + headingMarkerPath = files.FileInRoot(".mycomarkup-heading-migration-marker.txt") + if !shouldMigrateHeadings() { + return + } + + genericLineMigrator( + "Migrate headings to the new syntax", + tools.MigrateHeadings, + "Something went wrong when commiting heading migration: ") + createHeadingMarker() +} + +func shouldMigrateHeadings() bool { + file, err := os.Open(headingMarkerPath) + if os.IsNotExist(err) { + return true + } + if err != nil { + log.Fatalln("When checking if heading migration is needed:", err.Error()) + } + _ = file.Close() + return false +} + +func createHeadingMarker() { + err := ioutil.WriteFile( + headingMarkerPath, + []byte(`This file is used to mark that the heading migration was successful. If this file is deleted, the migration might happen again depending on the version. You should probably not touch this file at all and let it be.`), + 0766, + ) + if err != nil { + log.Fatalln(err) + } +} diff --git a/migration/migration.go b/migration/migration.go new file mode 100644 index 0000000..7f4a683 --- /dev/null +++ b/migration/migration.go @@ -0,0 +1,91 @@ +// Package migration holds the utilities for migrating from older incompatible Mycomarkup versions. +// +// Migrations are meant to be removed couple of versions after being introduced. +// +// Available migrations: +// * Rocket links +// * Headings +package migration + +import ( + "github.com/bouncepaw/mycorrhiza/history" + "github.com/bouncepaw/mycorrhiza/hyphae" + "github.com/bouncepaw/mycorrhiza/user" + "io" + "log" + "os" + "strings" +) + +func genericLineMigrator( + commitMessage string, + migrator func(string) string, + commitErrorMessage string, +) { + var ( + hop = history. + Operation(history.TypeMarkupMigration). + WithMsg(commitMessage). + WithUser(user.WikimindUser()) + mycoFiles = []string{} + ) + + for hypha := range hyphae.FilterHyphaeWithText(hyphae.YieldExistingHyphae()) { + /// Open file, read from file, modify file. If anything goes wrong, scream and shout. + + file, err := os.OpenFile(hypha.TextFilePath(), os.O_RDWR, 0766) + if err != nil { + hop.WithErrAbort(err) + log.Fatal("Something went wrong when opening ", hypha.TextFilePath(), ": ", err.Error()) + } + + var buf strings.Builder + _, err = io.Copy(&buf, file) + if err != nil { + hop.WithErrAbort(err) + _ = file.Close() + log.Fatal("Something went wrong when reading ", hypha.TextFilePath(), ": ", err.Error()) + } + + var ( + oldText = buf.String() + newText = migrator(oldText) + ) + if oldText != newText { // This file right here is being migrated for real. + mycoFiles = append(mycoFiles, hypha.TextFilePath()) + + err = file.Truncate(0) + if err != nil { + hop.WithErrAbort(err) + _ = file.Close() + log.Fatal("Something went wrong when truncating ", hypha.TextFilePath(), ": ", err.Error()) + } + + _, err = file.Seek(0, 0) + if err != nil { + hop.WithErrAbort(err) + _ = file.Close() + log.Fatal("Something went wrong when seeking in ", hypha.TextFilePath(), ": ", err.Error()) + } + + _, err = file.WriteString(newText) + if err != nil { + hop.WithErrAbort(err) + _ = file.Close() + log.Fatal("Something went wrong when writing to ", hypha.TextFilePath(), ": ", err.Error()) + } + } + _ = file.Close() + } + + if len(mycoFiles) == 0 { + hop.Abort() + return + } + + if hop.WithFiles(mycoFiles...).Apply().HasErrors() { + log.Fatal(commitErrorMessage, hop.FirstErrorText()) + } + + log.Println("Migrated", len(mycoFiles), "Mycomarkup documents") +} diff --git a/migration/rockets.go b/migration/rockets.go index 3895aec..1d0ea46 100644 --- a/migration/rockets.go +++ b/migration/rockets.go @@ -1,24 +1,13 @@ -// Package migration holds the utilities for migrating from older incompatible Mycomarkup versions. -// -// As of, there is rocket link migration only. Migrations are meant to be removed couple of versions after being introduced. package migration import ( "github.com/bouncepaw/mycomarkup/v4/tools" - "io" + "github.com/bouncepaw/mycorrhiza/files" "io/ioutil" "log" "os" - "strings" - - "github.com/bouncepaw/mycorrhiza/files" - "github.com/bouncepaw/mycorrhiza/history" - "github.com/bouncepaw/mycorrhiza/hyphae" - "github.com/bouncepaw/mycorrhiza/user" ) -// TODO: add heading migration too. - var rocketMarkerPath string // MigrateRocketsMaybe checks if the rocket link migration marker exists. If it exists, nothing is done. If it does not, the migration takes place. @@ -30,71 +19,11 @@ func MigrateRocketsMaybe() { return } - var ( - hop = history. - Operation(history.TypeMarkupMigration). - WithMsg("Migrate rocket links to the new syntax"). - WithUser(user.WikimindUser()) - mycoFiles = []string{} + genericLineMigrator( + "Migrate rocket links to the new syntax", + tools.MigrateRocketLinks, + "Something went wrong when commiting rocket link migration: ", ) - - for hypha := range hyphae.FilterHyphaeWithText(hyphae.YieldExistingHyphae()) { - /// Open file, read from file, modify file. If anything goes wrong, scream and shout. - - file, err := os.OpenFile(hypha.TextFilePath(), os.O_RDWR, 0766) - if err != nil { - hop.WithErrAbort(err) - log.Fatal("Something went wrong when opening ", hypha.TextFilePath(), ": ", err.Error()) - } - - var buf strings.Builder - _, err = io.Copy(&buf, file) - if err != nil { - hop.WithErrAbort(err) - _ = file.Close() - log.Fatal("Something went wrong when reading ", hypha.TextFilePath(), ": ", err.Error()) - } - - var ( - oldText = buf.String() - newText = tools.MigrateRocketLinks(oldText) - ) - if oldText != newText { // This file right here is being migrated for real. - mycoFiles = append(mycoFiles, hypha.TextFilePath()) - - err = file.Truncate(0) - if err != nil { - hop.WithErrAbort(err) - _ = file.Close() - log.Fatal("Something went wrong when truncating ", hypha.TextFilePath(), ": ", err.Error()) - } - - _, err = file.Seek(0, 0) - if err != nil { - hop.WithErrAbort(err) - _ = file.Close() - log.Fatal("Something went wrong when seeking in ", hypha.TextFilePath(), ": ", err.Error()) - } - - _, err = file.WriteString(newText) - if err != nil { - hop.WithErrAbort(err) - _ = file.Close() - log.Fatal("Something went wrong when writing to ", hypha.TextFilePath(), ": ", err.Error()) - } - } - _ = file.Close() - } - - if len(mycoFiles) == 0 { - hop.Abort() - return - } - - if hop.WithFiles(mycoFiles...).Apply().HasErrors() { - log.Fatal("Something went wrong when commiting rocket link migration: ", hop.FirstErrorText()) - } - log.Println("Migrated", len(mycoFiles), "Mycomarkup documents") createRocketLinkMarker() }