diff --git a/cmd/migrate/migrate.go b/cmd/migrate/migrate.go index bf849c3..3533f63 100644 --- a/cmd/migrate/migrate.go +++ b/cmd/migrate/migrate.go @@ -12,7 +12,8 @@ func NewMigrateCommand() *cobra.Command { cmd := &cobra.Command{ Use: "migrate", Short: "migrate the old activity format to the newest one", - Long: "parses the current state of the activity and migrates it to the current format", + Long: "parses the current state of the activity and migrates it to the current format." + + " It should be run from an activity directory.", //nolint: revive Run: func(cmd *cobra.Command, args []string) { path, err := os.Getwd() @@ -40,6 +41,26 @@ func NewMigrateCommand() *cobra.Command { Msg("Removed obsolete files from activity") } + migrated, err = migrate.SplitImagesIncludeInOwnFile(path + "/") + if err != nil { + log.Fatal().Err(err).Str("filename", path).Msg("Migration error") + } + + if migrated { + log.Info().Str("filename", path). + Msg("Moved latex image includes in own file") + } + + migrated, err = migrate.InsertOrUpdateVersion(path+"/", "v2") + if err != nil { + log.Fatal().Err(err).Str("filename", path).Msg("Migration error") + } + + if migrated { + log.Info().Str("filename", path). + Msg("Updated or Inserted Version") + } + log.Info().Msg("Activity migrated") }, } diff --git a/pkg/migrate/updateversion.go b/pkg/migrate/updateversion.go new file mode 100644 index 0000000..6d5d44a --- /dev/null +++ b/pkg/migrate/updateversion.go @@ -0,0 +1,130 @@ +package migrate + +import ( + "bufio" + "fmt" + "os" + "slices" + "strings" +) + +func normalizeHeaderFile(fileLocation string) error { + if err := removeLinesFromFile(fileLocation + "header.yaml"); err != nil { + return fmt.Errorf("error removing lines from header.yaml: %w", err) + } + + return nil +} + +func updateVersion(filename, newVersion string) error { + originalContent, err := os.ReadFile(filename) + if err != nil { + return fmt.Errorf("error reading file: %w", err) + } + + lines := strings.Split(string(originalContent), "\n") + + for i, line := range lines { + if strings.Contains(line, " version:") { + lines[i] = " version: " + newVersion + } + } + + updatedContent := strings.Join(lines, "\n") + + // Write the updated content back to the file + //nolint: gosec + err = os.WriteFile(filename, []byte(updatedContent), 0o644) + if err != nil { + return fmt.Errorf("error writing file: %w", err) + } + + return nil +} + +func removeLinesFromFile(filePath string) error { + linesToRemove := []string{ + "# vim: set filetype=yaml:", + "---", + "# default", + "# adjust if excess of space", + } + + inputFile, err := os.Open(filePath) + if err != nil { + return fmt.Errorf("error opening file: %w", err) + } + defer inputFile.Close() + + tempFile, err := os.CreateTemp("", "tempfile") + if err != nil { + return fmt.Errorf("error creating temp file: %w", err) + } + defer tempFile.Close() + + // Read the input file line by line and write to the temp file + scanner := bufio.NewScanner(inputFile) + writer := bufio.NewWriter(tempFile) + + for scanner.Scan() { + line := scanner.Text() + if !slices.Contains(linesToRemove, strings.TrimSpace(line)) { + _, err := writer.WriteString(line + "\n") + if err != nil { + return fmt.Errorf("error writing to temp file: %w", err) + } + } + } + + if err := scanner.Err(); err != nil { + return fmt.Errorf("error reading file: %w", err) + } + + writer.Flush() + + // Close both files + inputFile.Close() + tempFile.Close() + + // Replace the original file with the temp file + err = os.Rename(tempFile.Name(), filePath) + if err != nil { + return fmt.Errorf("error replacing file: %w", err) + } + + return nil +} + +func InsertOrUpdateVersion(filePath string, version string) (bool, error) { + versionLine := "meta:\n version:" + + if err := normalizeHeaderFile(filePath); err != nil { + return false, fmt.Errorf("error normalizingHeader: %w", err) + } + + originalFile, err := os.ReadFile(filePath + "header.yaml") + if err != nil { + return false, fmt.Errorf("error reading file: %w", err) + } + + if strings.HasPrefix(string(originalFile), versionLine) { + err := updateVersion(filePath+"header.yaml", version) + if err != nil { + return false, fmt.Errorf("error setting new version on header.yaml: %w", err) + } + + return true, nil + } + + // Prepend the new content to the original content + newContent := []byte(versionLine + " " + version + "\n" + string(originalFile)) + + // Write the updated content back to the file + //nolint: gosec + err = os.WriteFile(filePath+"header.yaml", newContent, 0o644) + if err != nil { + return false, fmt.Errorf("error writing file: %w", err) + } + + return true, nil +}