Skip to content

Commit

Permalink
insert or update version in header.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
nce committed Sep 21, 2024
1 parent 4ddf43b commit fecc062
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 1 deletion.
23 changes: 22 additions & 1 deletion cmd/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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")
},
}
Expand Down
130 changes: 130 additions & 0 deletions pkg/migrate/updateversion.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit fecc062

Please sign in to comment.