1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-10-31 07:33:00 +00:00

Write heading migrator

This commit is contained in:
Timur Ismagilov
2022-05-20 13:17:53 +03:00
parent 4035d4253a
commit 33c8192bb3
6 changed files with 147 additions and 79 deletions

47
migration/headings.go Normal file
View File

@@ -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)
}
}