summaryrefslogtreecommitdiff
path: root/internal/queue/queue.go
blob: 4a1b266dae9e54ae12376273eb46b23a86e23bad (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package queue

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

	"codeberg.org/snonux/gos/internal/colour"
	"codeberg.org/snonux/gos/internal/config"
	"codeberg.org/snonux/gos/internal/entry"
	"codeberg.org/snonux/gos/internal/oi"
	"codeberg.org/snonux/gos/internal/platforms"
	"codeberg.org/snonux/gos/internal/tags"
	"codeberg.org/snonux/gos/internal/timestamp"
)

// Strictly, we only operate on .txt files, but we also accept .md as Obsidian creates only .md files.
var validExtensions = []string{".txt", ".md"}

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

// Queue all *.txt into ./db/*.txt.STAMP.queued
func queueEntries(args config.Args) error {
	ch, err := oi.ReadDirCh(args.GosDir, find(args.GosDir, validExtensions...))
	if err != nil {
		return err
	}

	for filePath := range ch {
		if filePath, err = tags.InlineExtract(filePath); err != nil {
			return err
		}
		en, err := entry.New(filePath)
		if err != nil {
			return err
		}

		hasHashtags, err := en.HasHashtags()
		if err != nil {
			return err
		}
		if !hasHashtags {
			colour.Warnln("The following entry has got no hashtags:")
		}
		if !hasHashtags || en.HasTag("ask") {
			if err := en.FileAction("Do you want to queue this"); err != nil {
				return err
			}
		}

		destPath := fmt.Sprintf("%s/db/%s.%s.queued", args.GosDir, filepath.Base(en.Path), timestamp.Now())
		if args.DryRun {
			colour.Infoln("Not queueing entry", en.Path, "to", destPath, "as dry-run mode enabled")
			continue
		}
		if err := oi.Rename(en.Path, 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 queueEntriesToPlatforms(args config.Args) error {
	dbDir := filepath.Join(args.GosDir, "db")
	ch, err := oi.ReadDirCh(dbDir, find(dbDir, ".queued"))
	if err != nil {
		return err
	}

	trashDir := filepath.Join(args.GosDir, "db", "trashbin")
	for filePath := range ch {
		en, err := entry.New(filePath)
		if err != nil {
			return err
		}
		for platformStr := range args.Platforms {
			platform, err := platforms.New(platformStr)
			if err != nil {
				return err
			}
			// func NewShare(args config.Args, tags map[string]struct{}) (Share, error) {
			share, err := tags.NewShare(args, en.Tags)
			if err != nil {
				return err
			}
			if share.Excluded(platform.String()) {
				colour.Infoln("Not queueing entry", en, "to platform", platform, "as it is excluded")
				continue
			}
			if err := queuePlatform(en, args.GosDir, platform); err != nil {
				return err
			}
		}
		if args.DryRun {
			continue
		}

		// Keep queued items in trash for a while.
		trashPath := filepath.Join(trashDir, strings.TrimSuffix(filepath.Base(en.Path), ".queued")+".trash")
		colour.Infofln("Trashing %s -> %s", en.Path, trashPath)
		if err := oi.EnsureParentDir(trashPath); err != nil {
			return err
		}
		if err := os.Rename(en.Path, trashPath); err != nil {
			return err
		}
	}

	sixMonthsAgo := time.Now().AddDate(0, -6, 0)
	return deleteFiles(trashDir, ".trash", sixMonthsAgo)
}

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

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

	colour.Infoln("Queuing", en.Path, "->", destPath)
	return oi.CopyFile(en.Path, destPath)
}

func deleteFiles(path, suffix string, olderThan time.Time) error {
	ch, err := oi.ReadDirCh(path, find(path, suffix))
	if err != nil {
		return err
	}
	for filePath := range ch {
		fileInfo, err := os.Stat(filePath)
		if err != nil {
			return err
		}
		if fileInfo.ModTime().Before(olderThan) {
			colour.Infoln("Cleaning up", filePath)
			err := os.Remove(filePath)
			if err != nil {
				return err
			}
		}
	}
	return nil
}

func find(path string, suffixes ...string) func(os.DirEntry) (string, bool) {
	return func(file os.DirEntry) (string, bool) {
		filePath := filepath.Join(path, file.Name())
		for _, suffix := range suffixes {
			if strings.HasSuffix(file.Name(), suffix) {
				return filePath, true
			}
		}
		return filePath, false
	}
}