diff options
| author | Paul Buetow <paul@buetow.org> | 2024-07-28 22:44:03 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-07-28 22:44:03 +0300 |
| commit | 8072daab7a5221842cf823d9b314deb49b9ba6ff (patch) | |
| tree | df82e769da205811d4347ff8e05527df58fdea74 /internal | |
| parent | f36f21e1f1610b76caecbfab844f0462a9ba040c (diff) | |
rename entry to ent
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/client/tui/compose.go | 1 | ||||
| -rw-r--r-- | internal/client/tui/tui.go | 22 | ||||
| -rw-r--r-- | internal/easyhttp/easyhttp.go | 9 | ||||
| -rw-r--r-- | internal/server/handler/handler.go | 20 | ||||
| -rw-r--r-- | internal/server/repository/repository.go | 42 | ||||
| -rw-r--r-- | internal/server/repository/repository_test.go | 82 | ||||
| -rw-r--r-- | internal/types/entry_test.go | 42 |
7 files changed, 117 insertions, 101 deletions
diff --git a/internal/client/tui/compose.go b/internal/client/tui/compose.go index 20f74f1..3148b2a 100644 --- a/internal/client/tui/compose.go +++ b/internal/client/tui/compose.go @@ -11,7 +11,6 @@ import ( ) func composeAction(conf config.ClientConfig, queue bool) tea.Cmd { - err := ensureDirectoryExists(conf.DataDir) composeFile := fmt.Sprintf("%s/%s", conf.DataDir, conf.ComposeFile) diff --git a/internal/client/tui/tui.go b/internal/client/tui/tui.go index 298b890..8d4c176 100644 --- a/internal/client/tui/tui.go +++ b/internal/client/tui/tui.go @@ -43,13 +43,14 @@ type model struct { } const ( - composeNewPostCursor = iota - submitPostCursor + cursorCompose = iota + cursorSubmit + cursorComposeAndSubmit ) func initModel(conf config.ClientConfig) model { return model{ - choices: []string{"Compose post", "Submit post"}, + choices: []string{"Compose post", "Submit post", "Compose & submit post"}, ctx: context.Background(), conf: conf, } @@ -73,11 +74,20 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } case "enter": switch m.cursor { - case composeNewPostCursor: + case cursorCompose: return m, composeAction(m.conf, false) - case submitPostCursor: + case cursorSubmit: return m, submitAction(m.ctx, m.conf) + case cursorComposeAndSubmit: + // TODO: Find out correct way how to chain two tea.Cmd's?? + panic("not yet implemented") } + case "1": + return m, composeAction(m.conf, false) + case "2": + return m, submitAction(m.ctx, m.conf) + case "3": + panic("not yet implemented") case "a": m.altscreenActive = !m.altscreenActive @@ -111,7 +121,7 @@ func (m model) View() string { cursor = "==>" } - s += fmt.Sprintf("%s %s\n", cursor, choice) + s += fmt.Sprintf("%s %d. %s\n", cursor, i+1, choice) } if m.err != nil { diff --git a/internal/easyhttp/easyhttp.go b/internal/easyhttp/easyhttp.go index ce3d53b..c44cce3 100644 --- a/internal/easyhttp/easyhttp.go +++ b/internal/easyhttp/easyhttp.go @@ -68,7 +68,14 @@ func Post(ctx context.Context, uri, apiKey string, data []byte) ([]byte, error) return []byte{}, fmt.Errorf("%s: %w", uri, err) } - return body, nil + switch resp.StatusCode { + case 200: + return body, nil + case 401: + return body, fmt.Errorf("unauthorized, API key configured?") + default: + return body, fmt.Errorf("unexpected HTTP response code %d", resp.StatusCode) + } } // Submit structure as JSON to API diff --git a/internal/server/handler/handler.go b/internal/server/handler/handler.go index eabffa3..835f3fb 100644 --- a/internal/server/handler/handler.go +++ b/internal/server/handler/handler.go @@ -36,11 +36,11 @@ func (h Handler) Submit(ctx context.Context, w http.ResponseWriter, r *http.Requ return err } - entry, err := types.NewEntry(bytes) + ent, err := types.NewEntry(bytes) if err != nil { return err } - return repository.Instance(h.conf.DataDir).Merge(entry) + return repository.Instance(h.conf.DataDir).Merge(ent) } func (h Handler) List(w http.ResponseWriter, r *http.Request) error { @@ -63,12 +63,12 @@ func (h Handler) Get(w http.ResponseWriter, r *http.Request) error { return fmt.Errorf("invalid id %s", id) } - entry, ok := repository.Instance(h.conf.DataDir).Get(id) + ent, ok := repository.Instance(h.conf.DataDir).Get(id) if !ok { return fmt.Errorf("no entry with id %s found", id) } - fmt.Fprint(w, entry.String()) + fmt.Fprint(w, ent.String()) return nil } @@ -107,22 +107,22 @@ func (h Handler) mergeFromPartner(ctx context.Context, partner string) error { } var ( - entry types.Entry - uri = fmt.Sprintf("%s/get?id=%s", partner, pair.ID) + ent types.Entry + uri = fmt.Sprintf("%s/get?id=%s", partner, pair.ID) ) - if err := easyhttp.GetData(ctx, uri, h.conf.APIKey, &entry); err != nil { + if err := easyhttp.GetData(ctx, uri, h.conf.APIKey, &ent); err != nil { errs = append(errs, err) continue } // In theory, this should never happen - if pair.ID != entry.ID { - errs = append(errs, fmt.Errorf("pair ID %s does not match entry id %s", pair.ID, entry.ID)) + if pair.ID != ent.ID { + errs = append(errs, fmt.Errorf("pair ID %s does not match entry id %s", pair.ID, ent.ID)) continue } - errs = append(errs, repo.Merge(entry)) + errs = append(errs, repo.Merge(ent)) } return errors.Join(errs...) diff --git a/internal/server/repository/repository.go b/internal/server/repository/repository.go index 0b7d162..90b64bc 100644 --- a/internal/server/repository/repository.go +++ b/internal/server/repository/repository.go @@ -55,16 +55,16 @@ func newRepository(dataDir string, fs fs) Repository { } } -func (r Repository) put(entry types.Entry) error { +func (r Repository) put(ent types.Entry) error { r.mu.Lock() defer r.mu.Unlock() - r.entries[entry.ID] = entry + r.entries[ent.ID] = ent - bytes, err := entry.Serialize() + bytes, err := ent.Serialize() if err != err { return err } - return r.fs.WriteFile(r.entryPath(entry), bytes) + return r.fs.WriteFile(r.entryPath(ent), bytes) } // Load repository into memory if not done yet. @@ -87,12 +87,12 @@ func (r Repository) load() error { continue } - entry, err := types.NewEntry(bytes) + ent, err := types.NewEntry(bytes) if err != err { continue } - if err := r.put(entry); err != nil { + if err := r.put(ent); err != nil { errs = append(errs, err) } } @@ -113,8 +113,8 @@ func (r Repository) List() ([]EntryPair, error) { r.mu.Lock() defer r.mu.Unlock() - for _, entry := range r.entries { - pairs = append(pairs, EntryPair{entry.ID, entry.Checksum()}) + for _, ent := range r.entries { + pairs = append(pairs, EntryPair{ent.ID, ent.Checksum()}) } return pairs, nil @@ -133,8 +133,8 @@ func (r Repository) Get(id string) (types.Entry, bool) { r.mu.Lock() defer r.mu.Unlock() - entry, ok := r.entries[id] - return entry, ok + ent, ok := r.entries[id] + return ent, ok } func (r Repository) HasSameEntry(pair EntryPair) bool { @@ -149,34 +149,34 @@ func (r Repository) HasSameEntry(pair EntryPair) bool { return true } -func (r Repository) entryPath(entry types.Entry) string { - return fmt.Sprintf("%s/%s/%s.json", r.dataDir, time.Now().Format("2006"), entry.ID) +func (r Repository) entryPath(ent types.Entry) string { + return fmt.Sprintf("%s/%s/%s.json", r.dataDir, time.Now().Format("2006"), ent.ID) } -func (r Repository) Merge(otherEntry types.Entry) error { +func (r Repository) Merge(otherEnt types.Entry) error { _ = r.load() r.mu.Lock() defer r.mu.Unlock() - entry, ok := r.entries[otherEntry.ID] + ent, ok := r.entries[otherEnt.ID] if !ok { - log.Println("can't find entry with ID", otherEntry.ID, "in local db, create new from copy") + log.Println("can't find entry with ID", otherEnt.ID, "in local db, create new from copy") var err error - if entry, err = types.NewEntryFromCopy(otherEntry); err != nil { + if ent, err = types.NewEntryFromCopy(otherEnt); err != nil { return err } } - entry, _ = entry.Update(otherEntry) - r.entries[otherEntry.ID] = entry + ent, _ = ent.Update(otherEnt) + r.entries[otherEnt.ID] = ent - if !entry.Changed { + if !ent.Changed { return nil } - bytes, err := entry.Serialize() + bytes, err := ent.Serialize() if err != err { return err } - return r.fs.WriteFile(r.entryPath(entry), bytes) + return r.fs.WriteFile(r.entryPath(ent), bytes) } diff --git a/internal/server/repository/repository_test.go b/internal/server/repository/repository_test.go index a27108c..01babcb 100644 --- a/internal/server/repository/repository_test.go +++ b/internal/server/repository/repository_test.go @@ -13,15 +13,15 @@ func TestRepositoryPutGet(t *testing.T) { fs := make(vfs.MemoryFS) repo := newRepository("./data", fs) - for _, entry := range makeEntries(t) { - t.Run(entry.ID, func(t *testing.T) { - _ = repo.put(entry) - entryGot, ok := repo.Get(entry.ID) + for _, ent := range makeEntries(t) { + t.Run(ent.ID, func(t *testing.T) { + _ = repo.put(ent) + entGot, ok := repo.Get(ent.ID) if !ok { - t.Errorf("could not find entry with id %s in repo", entry.ID) + t.Errorf("could not find entry with id %s in repo", ent.ID) } - if !entryGot.Equals(entry) { - t.Error("expected to get", entry, "but got", entryGot) + if !entGot.Equals(ent) { + t.Error("expected to get", ent, "but got", entGot) } }) } @@ -35,9 +35,9 @@ func TestRepositoryLoad(t *testing.T) { entries := makeEntries(t) // Write entries into the VFS - for _, entry := range entries { - bytes, _ := entry.Serialize() - _ = repo.fs.WriteFile(repo.entryPath(entry), bytes) + for _, ent := range entries { + bytes, _ := ent.Serialize() + _ = repo.fs.WriteFile(repo.entryPath(ent), bytes) } // Load entries from VFS into the repo @@ -45,14 +45,14 @@ func TestRepositoryLoad(t *testing.T) { t.Error(err) } - for _, entry := range entries { - t.Run(entry.ID, func(t *testing.T) { - entryGot, ok := repo.Get(entry.ID) + for _, ent := range entries { + t.Run(ent.ID, func(t *testing.T) { + entGot, ok := repo.Get(ent.ID) if !ok { - t.Errorf("could not find entry with id %s in repo", entry.ID) + t.Errorf("could not find entry with id %s in repo", ent.ID) } - if !entryGot.Equals(entry) { - t.Error("expected to get", entry, "but got", entryGot) + if !entGot.Equals(ent) { + t.Error("expected to get", ent, "but got", entGot) } }) } @@ -65,8 +65,8 @@ func TestRepositoryList(t *testing.T) { repo := newRepository("./data", fs) entries := makeEntries(t) - for _, entry := range entries { - _ = repo.put(entry) + for _, ent := range entries { + _ = repo.put(ent) } pairs, _ := repo.List() @@ -74,17 +74,17 @@ func TestRepositoryList(t *testing.T) { t.Error("expected as many entries as pairs") } - for _, entry := range entries { + for _, ent := range entries { var found bool for _, pair := range pairs { - if entry.ID == pair.ID && entry.Checksum() == pair.Checksum { + if ent.ID == pair.ID && ent.Checksum() == pair.Checksum { found = true - t.Log("entry matches pair", entry, pair) + t.Log("entry matches pair", ent, pair) break } } if !found { - t.Error("could not find entry", entry, "in", pairs) + t.Error("could not find entry", ent, "in", pairs) } } } @@ -94,10 +94,10 @@ func TestRepositoryHasSameEntry(t *testing.T) { fs := make(vfs.MemoryFS) repo := newRepository("./data", fs) - entry, _ := makeAnEntry() - _ = repo.put(entry) + ent, _ := makeAnEntry() + _ = repo.put(ent) - pair := EntryPair{entry.ID, entry.Checksum()} + pair := EntryPair{ent.ID, ent.Checksum()} if !repo.HasSameEntry(pair) { t.Error("repo does not contain entry corresponding to pair", pair) } @@ -113,16 +113,16 @@ func TestRepositoryMerge(t *testing.T) { fs := make(vfs.MemoryFS) repo := newRepository("./data", fs) - entry1, _ := makeAnEntry() - _ = repo.put(entry1) + ent1, _ := makeAnEntry() + _ = repo.put(ent1) - entry2, _ := makeAnotherEntry() + ent2, _ := makeAnotherEntry() // Need to have the same IDs so that the entries will actually be merged - entry2.ID = entry1.ID - // Merge a modified entry2 into the repository. - entry2.Body = "merged" - entry2.Epoch = 12345 - _ = repo.Merge(entry2) + ent2.ID = ent1.ID + // Merge a modified ent2 into the repository. + ent2.Body = "merged" + ent2.Epoch = 12345 + _ = repo.Merge(ent2) pairs, _ := repo.List() // Ensuring the merge didn't add a new entry @@ -130,25 +130,25 @@ func TestRepositoryMerge(t *testing.T) { t.Error("expected exactly one element in the repo but got", pairs) } - entryGot, _ := repo.Get(entry1.ID) - if entryGot.Body != "merged" { - t.Error("unexpected body", entryGot.Body) + entGot, _ := repo.Get(ent1.ID) + if entGot.Body != "merged" { + t.Error("unexpected body", entGot.Body) } - if entryGot.Epoch != 12345 { - t.Error("unexpected epoch", entryGot.Epoch) + if entGot.Epoch != 12345 { + t.Error("unexpected epoch", entGot.Epoch) } } func makeEntries(t *testing.T) []types.Entry { - entry1, err := makeAnEntry() + ent1, err := makeAnEntry() if err != nil { t.Error(err) } - entry2, err := makeAnotherEntry() + ent2, err := makeAnotherEntry() if err != nil { t.Error(err) } - return []types.Entry{entry1, entry2} + return []types.Entry{ent1, ent2} } func makeAnEntry() (types.Entry, error) { diff --git a/internal/types/entry_test.go b/internal/types/entry_test.go index 90e7972..6b7e289 100644 --- a/internal/types/entry_test.go +++ b/internal/types/entry_test.go @@ -5,73 +5,73 @@ import "testing" func TestEntryChecksum(t *testing.T) { t.Parallel() - entry, err := NewEntry([]byte(`{"Body": "Body text here"}`)) + ent, err := NewEntry([]byte(`{"Body": "Body text here"}`)) if err != nil { t.Error(err) return } expected := "e139c0788fbc0d9cce370e4918c1cbc8862184d9461bd1238c02b7f80cb042fe" - got := entry.Checksum() + got := ent.Checksum() if expected != got { t.Errorf("expected checksum '%s' but got '%s'", expected, got) return } - t.Log(entry.Checksum()) + t.Log(ent.Checksum()) } func TestEquals(t *testing.T) { t.Parallel() - entry1, entry2, err := twoDifferentEntries() + ent1, ent2, err := twoDifferentEntries() if err != nil { t.Error(err) return } - if entry1.Equals(entry2) { - t.Error("entries should not be equal", entry1, entry2) + if ent1.Equals(ent2) { + t.Error("entries should not be equal", ent1, ent2) } - t.Log("both entries differ", entry1, entry2) + t.Log("both entries differ", ent1, ent2) } func TestUpdate(t *testing.T) { t.Parallel() - entry1, entry2, err := twoDifferentEntries() + ent1, ent2, err := twoDifferentEntries() if err != nil { t.Error(err) } - if entry1.Changed { - t.Error("didn't expect the entry to be changed before the update", entry1) + if ent1.Changed { + t.Error("didn't expect the entry to be changed before the update", ent1) } - entry1, _ = entry1.Update(entry2) - if len(entry1.Shared) != 3 { - t.Error("expected 3 entries after update", entry1) + ent1, _ = ent1.Update(ent2) + if len(ent1.Shared) != 3 { + t.Error("expected 3 entries after update", ent1) } - if !entry1.Changed { + if !ent1.Changed { t.Error("expected the entry to be changed after update") } var isShared int - for _, shared := range entry1.Shared { + for _, shared := range ent1.Shared { if shared.Is { isShared++ } } if isShared != 2 { - t.Error("expected 2 shared entries after update but got", isShared, entry1) + t.Error("expected 2 shared entries after update but got", isShared, ent1) } } -func twoDifferentEntries() (entry1, entry2 Entry, err error) { - entry1Str := ` +func twoDifferentEntries() (ent1, ent2 Entry, err error) { + ent1Str := ` { "Body": "Body text here", "Shared": [ @@ -80,12 +80,12 @@ func twoDifferentEntries() (entry1, entry2 Entry, err error) { ] } ` - entry1, err = NewEntry([]byte(entry1Str)) + ent1, err = NewEntry([]byte(ent1Str)) if err != nil { return } - entry2Str := ` + ent2Str := ` { "Body": "Body text here", "Shared": [ @@ -95,6 +95,6 @@ func twoDifferentEntries() (entry1, entry2 Entry, err error) { ] } ` - entry2, err = NewEntry([]byte(entry2Str)) + ent2, err = NewEntry([]byte(ent2Str)) return } |
