summaryrefslogtreecommitdiff
path: root/internal/queue/queue.go
blob: 94ea1e07325bd3e9cf12029862e5bb41d12e96f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package queue

import (
	"fmt"
	"log"
	"os"
	"path/filepath"
	"slices"
	"strings"
	"time"

	"codeberg.org/snonux/gos/internal/config"
	"codeberg.org/snonux/gos/internal/format"
	"codeberg.org/snonux/gos/internal/oi"
)

func Run(args config.Args) error {
	if err := queueEntries(args); err != nil {
		return err
	}
	if err := queuePlatforms(args); err != nil {
		return err
	}
	return nil
}

// Queue all *.txt into ./db/*.txt.STAMP.queued
func queueEntries(args config.Args) error {
	// Strictly, we only operate on .txt files, but we also accept .md as Obsidian creates only .md files.
	var validExtensions = []string{".txt", ".md"}

	ch, err := oi.ReadDirFilter(args.GosDir, func(file os.DirEntry) (string, bool) {
		filePath := filepath.Join(args.GosDir, file.Name())
		return filePath, slices.Contains(validExtensions, filepath.Ext(file.Name())) &&
			file.Type().IsRegular()
	})
	if err != err {
		return err
	}

	now := time.Now()
	for filePath := range ch {
		destPath := fmt.Sprintf("%s/db/%s.%s.queued", args.GosDir,
			filepath.Base(filePath), now.Format(format.Time))
		if err := oi.Rename(filePath, destPath); err != nil {
			return err
		}
	}

	return nil
}

// Queue all ./db/queued/*.txt.STAMP.queued into ./db/platforms/PLATFORM/*.txt.STAMP.queued
// for each PLATFORM
func queuePlatforms(args config.Args) error {
	dbDir := filepath.Join(args.GosDir, "db")
	ch, err := oi.ReadDirFilter(dbDir, func(file os.DirEntry) (string, bool) {
		filePath := filepath.Join(dbDir, file.Name())
		return filePath, strings.HasSuffix(file.Name(), ".queued")
	})
	if err != err {
		return err
	}

	for filePath := range ch {
		for _, platform := range args.Platforms {
			if err := queuePlatform(filePath, args.GosDir, platform); err != nil {
				return err
			}
		}
		log.Println("Removing", filePath)
		if err := os.Remove(filePath); err != nil {
			return err
		}

	}

	return nil
}

// Queue ./db/queued/*.txt.STAMP.queued to ./db/platforms/PLATFORM/*.txt.STAMP.queued
func queuePlatform(entryPath, gosDir, platform string) error {
	destDir := filepath.Join(gosDir, "db/platform", strings.ToLower(platform))
	// destDir := fmt.Sprintf("%s/db/platforms/%s/", gosDir, strings.ToLower(platform))
	destPath := filepath.Join(destDir, filepath.Base(entryPath))
	// destPath := fmt.Sprintf("%s/%s", destDir, filepath.Base(entryPath))
	postedFile := fmt.Sprintf("%s.posted", strings.TrimSuffix(destPath, ".queued"))

	// Entry already posted platform?
	if oi.IsRegular(postedFile) {
		log.Println("Not re-queueing", destPath, "as", postedFile, "already exists")
		return nil
	}

	log.Println("Queuing", entryPath, "->", destPath)
	return oi.CopyFile(entryPath, destPath)
}