summaryrefslogtreecommitdiff
path: root/internal/entry
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-09-30 10:51:49 +0300
committerPaul Buetow <paul@buetow.org>2024-09-30 10:51:49 +0300
commitcbdce79d25206573d1f79c9c647dcaa251b69463 (patch)
tree20f3bcbf76b28e8577a0dba3eb586b33e73a7699 /internal/entry
parent4d904e44a18acec779ef1d09de5d30e4890d1918 (diff)
add entry "class"
Diffstat (limited to 'internal/entry')
-rw-r--r--internal/entry/entry.go75
-rw-r--r--internal/entry/entry_test.go39
2 files changed, 114 insertions, 0 deletions
diff --git a/internal/entry/entry.go b/internal/entry/entry.go
new file mode 100644
index 0000000..4e837a0
--- /dev/null
+++ b/internal/entry/entry.go
@@ -0,0 +1,75 @@
+package entry
+
+import (
+ "fmt"
+ "strings"
+ "time"
+
+ "codeberg.org/snonux/gos/internal/format"
+)
+
+type State int
+
+const (
+ Unknown State = iota
+ Queued
+ Posted
+)
+
+func (s State) String() string {
+ switch s {
+ case Unknown:
+ return "unknown"
+ case Queued:
+ return "queued"
+ case Posted:
+ return "posted"
+ default:
+ panic(fmt.Sprintf("unknown state: %d", int(s)))
+ }
+}
+
+// The time this code was written a:round, actually.
+const oldestValidTime = "20240922-102800"
+
+type Entry struct {
+ Path string
+ Time time.Time
+ State State
+}
+
+// filePath format: /foo/foobarbaz.something.here.txt.STAMP.{posted,queued}
+func New(filePath string) (Entry, error) {
+ ent := Entry{Path: filePath}
+
+ // We want to get the STAMP!
+ parts := strings.Split(filePath, ".")
+ if len(parts) < 4 {
+ return ent, fmt.Errorf("not a valid entry path: %s", filePath)
+ }
+
+ switch parts[len(parts)-1] {
+ case "queued":
+ ent.State = Queued
+ case "posted":
+ ent.State = Posted
+ default:
+ return ent, fmt.Errorf("can't parse state from path: %s", filePath)
+ }
+
+ var err error
+ if ent.Time, err = time.Parse(format.Time, parts[len(parts)-2]); err != nil {
+ return ent, err
+ }
+
+ oldestValid, err := time.Parse(format.Time, oldestValidTime)
+ if err != nil {
+ panic(err)
+ }
+
+ if ent.Time.Before(oldestValid) {
+ return ent, fmt.Errorf("entry time does not seem legic, it is too old: %v", ent.Time)
+ }
+
+ return ent, nil
+}
diff --git a/internal/entry/entry_test.go b/internal/entry/entry_test.go
new file mode 100644
index 0000000..5e3a5d3
--- /dev/null
+++ b/internal/entry/entry_test.go
@@ -0,0 +1,39 @@
+package entry
+
+import (
+ "fmt"
+ "testing"
+ "time"
+
+ "codeberg.org/snonux/gos/internal/format"
+)
+
+func TestEntry(t *testing.T) {
+ states := []State{Queued, Posted}
+ stamps := []string{"20240928-111835", "20241028-120135"}
+
+ for _, state := range states {
+ for _, stamp := range stamps {
+ queuedPath := fmt.Sprintf("gosdir/db/platforms/linkedin/helloworld.txt.%s.%s", stamp, state)
+
+ ent, err := New(queuedPath)
+ if err != nil {
+ t.Error(err)
+ }
+ if ent.Path != queuedPath {
+ t.Errorf("expected path %s but got %s", queuedPath, ent.Path)
+ }
+ if ent.State != state {
+ t.Errorf("expected state %s but got %s", state, ent.State)
+ }
+
+ expectedTime, err := time.Parse(format.Time, stamp)
+ if err != nil {
+ t.Error(err)
+ }
+ if ent.Time != expectedTime {
+ t.Errorf("expected time to be %v but got %v", expectedTime, ent.Time)
+ }
+ }
+ }
+}