From 676d6bb99e87acd72ef631598f1abcd56b2dec34 Mon Sep 17 00:00:00 2001 From: Jonathan Hope Date: Fri, 31 May 2024 17:58:29 -0700 Subject: [PATCH] wip: combine cli and host --- .goreleaser.yaml | 90 ++----------------- Taskfile.yml | 10 +-- cmd/cli/internal/cmd.go | 2 +- cmd/cli/internal/formatters.go | 68 +++++++------- .../internal/messaging/book_dto.go | 0 .../internal/messaging/dispatcher.go | 0 cmd/cli/internal/messaging/doc.go | 9 ++ .../internal/messaging/handlers.go | 0 .../internal/messaging/messages.go | 7 +- .../internal/messaging/messages_test.go | 0 .../internal/messaging/payload.go | 12 ++- .../tui/controls/footer/footer.go | 4 +- .../tui/controls/footer/footer_test.go | 2 +- .../tui/controls/header/header.go | 2 +- .../tui/controls/header/header_test.go | 0 .../{ => internal}/tui/controls/help/help.go | 0 .../tui/controls/help/help_test.go | 2 +- .../tui/controls/scrolltable/scrolltable.go | 4 +- .../controls/scrolltable/scrolltable_test.go | 4 +- .../tui/controls/textinput/textinput.go | 2 +- .../tui/controls/textinput/textinput_test.go | 0 .../tui/controls/typeahead/typeahead.go | 6 +- .../tui/controls/typeahead/typeahead_test.go | 4 +- cmd/cli/{ => internal}/tui/msgs/msgs.go | 0 cmd/cli/{ => internal}/tui/program.go | 6 +- cmd/cli/{ => internal}/tui/utils/cmp_cmd.go | 0 cmd/cli/{ => internal}/tui/utils/substr.go | 0 .../{ => internal}/tui/utils/substr_test.go | 0 .../tui/views/booksview/books.go | 12 +-- .../tui/views/errorview/error.go | 2 +- .../tui/views/errorview/error_test.go | 4 +- cmd/cli/main.go | 18 +++- .../test/native_host}/add_book_test.go | 2 +- .../test/native_host}/add_folder_test.go | 2 +- .../test/native_host}/add_tags_test.go | 2 +- .../test => cli/test/native_host}/helpers.go | 2 +- .../test/native_host}/list_books_test.go | 2 +- .../test/native_host}/list_tags_test.go | 2 +- .../test/native_host}/remove_book_test.go | 2 +- .../test/native_host}/remove_folder_test.go | 2 +- .../test/native_host}/remove_tags_test.go | 2 +- .../test/native_host}/update_book_test.go | 2 +- .../test/native_host}/update_folder_test.go | 2 +- cmd/cli/test/steps.go | 69 +++++++++----- cmd/cli/test/test_helpers.go | 9 +- cmd/host/main.go | 17 ---- flake.nix | 1 + internal/manifest/install.go | 12 +-- internal/paths/paths.go | 4 +- internal/paths/paths_test.go | 4 +- 50 files changed, 185 insertions(+), 223 deletions(-) rename cmd/{host => cli}/internal/messaging/book_dto.go (100%) rename cmd/{host => cli}/internal/messaging/dispatcher.go (100%) create mode 100644 cmd/cli/internal/messaging/doc.go rename cmd/{host => cli}/internal/messaging/handlers.go (100%) rename cmd/{host => cli}/internal/messaging/messages.go (91%) rename cmd/{host => cli}/internal/messaging/messages_test.go (100%) rename cmd/{host => cli}/internal/messaging/payload.go (93%) rename cmd/cli/{ => internal}/tui/controls/footer/footer.go (96%) rename cmd/cli/{ => internal}/tui/controls/footer/footer_test.go (95%) rename cmd/cli/{ => internal}/tui/controls/header/header.go (97%) rename cmd/cli/{ => internal}/tui/controls/header/header_test.go (100%) rename cmd/cli/{ => internal}/tui/controls/help/help.go (100%) rename cmd/cli/{ => internal}/tui/controls/help/help_test.go (95%) rename cmd/cli/{ => internal}/tui/controls/scrolltable/scrolltable.go (98%) rename cmd/cli/{ => internal}/tui/controls/scrolltable/scrolltable_test.go (98%) rename cmd/cli/{ => internal}/tui/controls/textinput/textinput.go (99%) rename cmd/cli/{ => internal}/tui/controls/textinput/textinput_test.go (100%) rename cmd/cli/{ => internal}/tui/controls/typeahead/typeahead.go (97%) rename cmd/cli/{ => internal}/tui/controls/typeahead/typeahead_test.go (95%) rename cmd/cli/{ => internal}/tui/msgs/msgs.go (100%) rename cmd/cli/{ => internal}/tui/program.go (90%) rename cmd/cli/{ => internal}/tui/utils/cmp_cmd.go (100%) rename cmd/cli/{ => internal}/tui/utils/substr.go (100%) rename cmd/cli/{ => internal}/tui/utils/substr_test.go (100%) rename cmd/cli/{ => internal}/tui/views/booksview/books.go (98%) rename cmd/cli/{ => internal}/tui/views/errorview/error.go (94%) rename cmd/cli/{ => internal}/tui/views/errorview/error_test.go (88%) rename cmd/{host/internal/test => cli/test/native_host}/add_book_test.go (98%) rename cmd/{host/internal/test => cli/test/native_host}/add_folder_test.go (97%) rename cmd/{host/internal/test => cli/test/native_host}/add_tags_test.go (94%) rename cmd/{host/internal/test => cli/test/native_host}/helpers.go (96%) rename cmd/{host/internal/test => cli/test/native_host}/list_books_test.go (99%) rename cmd/{host/internal/test => cli/test/native_host}/list_tags_test.go (98%) rename cmd/{host/internal/test => cli/test/native_host}/remove_book_test.go (93%) rename cmd/{host/internal/test => cli/test/native_host}/remove_folder_test.go (93%) rename cmd/{host/internal/test => cli/test/native_host}/remove_tags_test.go (95%) rename cmd/{host/internal/test => cli/test/native_host}/update_book_test.go (99%) rename cmd/{host/internal/test => cli/test/native_host}/update_folder_test.go (99%) delete mode 100644 cmd/host/main.go diff --git a/.goreleaser.yaml b/.goreleaser.yaml index ac8605a..2b0b883 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -22,24 +22,6 @@ builds: - -w - -X "main.version={{ .Version }}" - - id: armaria-host-darwin-amd64 - binary: armaria - main: ./cmd/host - goarch: - - amd64 - goos: - - darwin - env: - - CC=zig cc -target x86_64-macos -g0 --sysroot={{ .Env.SDK }} -I{{ .Env.SDK }}/usr/include -L{{ .Env.SDK }}/usr/lib -F{{ .Env.SDK }}/System/Library/Frameworks -framework CoreFoundation -Wno-expansion-to-defined -Wno-availability -Wno-nullability-completeness - - CXX=zig c++ -target x86_64-macos -g0 --sysroot={{ .Env.SDK }} -I{{ .Env.SDK }}/usr/include -L{{ .Env.SDK }}/usr/lib -F{{ .Env.SDK }}/System/Library/Frameworks -framework CoreFoundation -Wno-expansion-to-defined -Wno-availability -Wno-nullability-completeness - flags: - - -trimpath - - -tags=fts5 - - -buildmode=pie - ldflags: - - -s - - -w - - id: armaria-darwin-arm64 binary: armria main: ./cmd/cli @@ -59,24 +41,6 @@ builds: - -w - -X "main.version={{ .Version }}" - - id: armaria-host-darwin-arm64 - binary: armria - main: ./cmd/host - goarch: - - arm64 - goos: - - darwin - env: - - CC=zig cc -target aarch64-macos -g0 --sysroot={{ .Env.SDK }} -I{{ .Env.SDK }}/usr/include -L{{ .Env.SDK }}/usr/lib -F{{ .Env.SDK }}/System/Library/Frameworks -framework CoreFoundation -Wno-expansion-to-defined -Wno-availability -Wno-nullability-completeness - - CXX=zig c++ -target aarch64-macos -g0 --sysroot={{ .Env.SDK }} -I{{ .Env.SDK }}/usr/include -L{{ .Env.SDK }}/usr/lib -F{{ .Env.SDK }}/System/Library/Frameworks -framework CoreFoundation -Wno-expansion-to-defined -Wno-availability -Wno-nullability-completeness - flags: - - -trimpath - - -tags=fts5 - - -buildmode=pie - ldflags: - - -s - - -w - - id: armaria-linux-amd64 binary: armaria main: ./cmd/cli @@ -97,25 +61,6 @@ builds: - -w - -X "main.version={{ .Version }}" - - id: armaria-host-linux-amd64 - binary: armaria-host - main: ./cmd/host - env: - - CC=zig cc -target x86_64-linux-musl -g0 - - CXX=zig c++ -target x86_64-linux-musl -g0 - # https://github.com/mattn/go-sqlite3/issues/1164 - - CGO_CFLAGS="-D_LARGEFILE64_SOURCE" - goarch: - - amd64 - goos: - - linux - flags: - - -trimpath - - -tags=fts5 - ldflags: - - -s - - -w - - id: armaria-windows-amd64 binary: armaria main: ./cmd/cli @@ -135,24 +80,6 @@ builds: - -w - -X "main.version={{ .Version }}" - - id: armaria-host-windows-amd64 - binary: armaria-host - main: ./cmd/host - goarch: - - amd64 - goos: - - windows - env: - - CC=zig cc -target x86_64-windows-gnu -g0 - - CXX=zig c++ -target x86_64-windows-gnu -g0 - flags: - - -trimpath - - -buildmode=exe - - -tags=fts5 - ldflags: - - -s - - -w - universal_binaries: - id: armaria-darwin-universal ids: @@ -160,18 +87,11 @@ universal_binaries: - armaria-darwin-arm64 replace: true name_template: "armaria" - - id: armaria-host-darwin-universal - ids: - - armaria-host-darwin-amd64 - - armaria-host-darwin-arm64 - replace: true - name_template: "armaria-host" archives: - id: armaria-linux-archive builds: - armaria-linux-amd64 - - armaria-host-linux-amd64 name_template: "armaria_{{ .Version }}_{{ .Os }}_{{ .Arch }}" wrap_in_directory: false format: zip @@ -180,7 +100,6 @@ archives: - id: armaria-windows-archive builds: - armaria-windows-amd64 - - armaria-host-windows-amd64 name_template: "armaria_{{ .Version }}_{{ .Os }}_{{ .Arch }}" wrap_in_directory: false format: zip @@ -189,13 +108,17 @@ archives: - id: armaria-macos-archive builds: - armaria-darwin-universal - - armaria-host-darwin-universal name_template: "armaria_{{ .Version }}_{{ .Os }}_{{ .Arch }}" wrap_in_directory: false format: zip files: - none* +upx: + - enabled: true + goos: [linux, windows] # UPX doesn't work for MacOS ATM + compress: best + checksum: name_template: 'checksums.txt' @@ -219,7 +142,6 @@ snapcrafts: - id: armaria-snap builds: - armaria-linux-amd64 - - armaria-host-linux-amd64 name_template: "armaria_{{ .Version }}_{{ .Os }}_{{ .Arch }}" name: armaria title: Armaria @@ -237,8 +159,6 @@ snapcrafts: armaria: command: armaria plugs: ["dot-mozilla-native-messaging-hosts", "dot-config-google-chrome-native-messaging-hosts", "dot-config-chromium-native-messaging-hosts"] - armaria-host: - command: armaria-host plugs: dot-mozilla-native-messaging-hosts: interface: personal-files diff --git a/Taskfile.yml b/Taskfile.yml index ccdfc81..b1c76f3 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -7,7 +7,7 @@ tasks: test-internal: dir: internal cmds: - - go test -count=1 ./... + - go test -count=1 --tags "fts5" ./... test-cmd: dir: cmd cmds: @@ -21,11 +21,6 @@ tasks: cmds: - go build --tags "fts5" -ldflags="-X main.version=local" - cp cli ../../armaria - build-host: - dir: cmd/host - cmds: - - go build --tags "fts5" - - cp host ../../armaria-host build-seeder: dir: cmd/seeder cmds: @@ -34,7 +29,6 @@ tasks: build: cmds: - task: build-cli - - task: build-host - task: build-seeder migrate-up: cmds: @@ -45,10 +39,8 @@ tasks: clean: cmds: - rm -f armaria - - rm -f armaria-host - rm -f armaria-seeder - rm -f cmd/cli/cli - - rm -f cmd/host/host - rm -f cmd/seeder/seeder - find . -name "*.db" -type f -delete - find . -name "*.db-shm" -type f -delete diff --git a/cmd/cli/internal/cmd.go b/cmd/cli/internal/cmd.go index cfa140d..c709afb 100644 --- a/cmd/cli/internal/cmd.go +++ b/cmd/cli/internal/cmd.go @@ -5,7 +5,7 @@ import ( "fmt" "time" - "github.com/jonathanhope/armaria/cmd/cli/tui" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui" "github.com/jonathanhope/armaria/pkg" ) diff --git a/cmd/cli/internal/formatters.go b/cmd/cli/internal/formatters.go index 6721018..789e920 100644 --- a/cmd/cli/internal/formatters.go +++ b/cmd/cli/internal/formatters.go @@ -9,6 +9,7 @@ import ( "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss/table" + "github.com/jonathanhope/armaria/cmd/cli/internal/messaging" "github.com/jonathanhope/armaria/internal/null" "github.com/jonathanhope/armaria/pkg" "github.com/nathan-fiscaletti/consolesize-go" @@ -26,16 +27,18 @@ const ( FormatterPretty Formatter = "pretty" // format results in human readable way. ) -// BookDTO is a bookmark or folder that can be marshalled into JSON. -type BookDTO struct { - ID string `json:"id"` - URL null.NullString `json:"url"` - Name string `json:"name"` - Description null.NullString `json:"description"` - ParentID null.NullString `json:"parentId"` - IsFolder bool `json:"isFolder"` - ParentName null.NullString `json:"parentName"` - Tags []string `json:"tags"` +// marshalMsg will marshal a native message as JSON. +func marshalMsg[T messaging.Payload](kind messaging.MessageKind, payload T) string { + msg, err := messaging.PayloadToMessage(kind, payload) + if err != nil { + panic(err) + } + json, err := json.Marshal(msg) + if err != nil { + panic(err) + } + + return string(json) } // formatSuccess formats a success message. @@ -108,7 +111,10 @@ func formatError(writer io.Writer, formatter Formatter, err error) { switch formatter { case FormatterJSON: - fmt.Fprintf(writer, "\"%s\"\n", errorString) + json := marshalMsg(messaging.MessageKindError, messaging.ErrorPayload{ + Error: errorString, + }) + fmt.Fprint(writer, json) case FormatterPretty: style := lipgloss. @@ -127,9 +133,10 @@ func formatError(writer io.Writer, formatter Formatter, err error) { func formatBookResults(writer io.Writer, formatter Formatter, books []armaria.Book) { switch formatter { + case FormatterJSON: - dtos := lo.Map(books, func(x armaria.Book, index int) BookDTO { - return BookDTO{ + dtos := lo.Map(books, func(x armaria.Book, index int) messaging.BookDTO { + return messaging.BookDTO{ ID: x.ID, URL: null.NullStringFromPtr(x.URL), Name: x.Name, @@ -141,12 +148,11 @@ func formatBookResults(writer io.Writer, formatter Formatter, books []armaria.Bo } }) - json, err := json.Marshal(&dtos) - if err != nil { - panic(err) - } + json := marshalMsg(messaging.MessageKindBooks, messaging.BooksPayload{ + Books: dtos, + }) - fmt.Fprintln(writer, string(json)) + fmt.Fprintln(writer, json) case FormatterPretty: width, _ := consolesize.GetConsoleSize() @@ -199,13 +205,11 @@ func formatTagResults(writer io.Writer, formatter Formatter, tags []string) { switch formatter { case FormatterJSON: - json, err := json.Marshal(&tags) - - if err != nil { - panic(err) - } + json := marshalMsg(messaging.MessageKindTags, messaging.TagsPayload{ + Tags: tags, + }) - fmt.Fprintln(writer, string(json)) + fmt.Fprintln(writer, json) case FormatterPretty: width, _ := consolesize.GetConsoleSize() @@ -229,7 +233,11 @@ func formatConfigResult(writer io.Writer, formatter Formatter, value string) { switch formatter { case FormatterJSON: - fmt.Fprintf(writer, "\"%s\"\n", value) + json := marshalMsg(messaging.MessageKindConfigValue, messaging.ConfigValuePayload{ + Value: value, + }) + + fmt.Fprint(writer, json) case FormatterPretty: width, _ := consolesize.GetConsoleSize() @@ -255,13 +263,11 @@ func formatParentNames(writer io.Writer, formatter Formatter, parentNames []stri switch formatter { case FormatterJSON: - json, err := json.Marshal(&parentNames) - - if err != nil { - panic(err) - } + json := marshalMsg(messaging.MessageKindParentNames, messaging.ParentNamesPayload{ + ParentNames: parentNames, + }) - fmt.Fprintln(writer, string(json)) + fmt.Fprintln(writer, json) case FormatterPretty: width, _ := consolesize.GetConsoleSize() diff --git a/cmd/host/internal/messaging/book_dto.go b/cmd/cli/internal/messaging/book_dto.go similarity index 100% rename from cmd/host/internal/messaging/book_dto.go rename to cmd/cli/internal/messaging/book_dto.go diff --git a/cmd/host/internal/messaging/dispatcher.go b/cmd/cli/internal/messaging/dispatcher.go similarity index 100% rename from cmd/host/internal/messaging/dispatcher.go rename to cmd/cli/internal/messaging/dispatcher.go diff --git a/cmd/cli/internal/messaging/doc.go b/cmd/cli/internal/messaging/doc.go new file mode 100644 index 0000000..4d55d7a --- /dev/null +++ b/cmd/cli/internal/messaging/doc.go @@ -0,0 +1,9 @@ +// messaging contains the logic for Armaria to communicate with JSON. +// This is the message format used to communicate with browser extensions. +// They are encoded by first writing the size of the message as a unit32. +// After that the message is encoded to JSON and written as binary. +// All of this should be done over stdout and stdin. +// This is also the format the JSON formatter uses. +// The messages require calls to unmarshal the JSON. +// First to get the kind of the message; second to unmarshal the payload once the type is known. +package messaging diff --git a/cmd/host/internal/messaging/handlers.go b/cmd/cli/internal/messaging/handlers.go similarity index 100% rename from cmd/host/internal/messaging/handlers.go rename to cmd/cli/internal/messaging/handlers.go diff --git a/cmd/host/internal/messaging/messages.go b/cmd/cli/internal/messaging/messages.go similarity index 91% rename from cmd/host/internal/messaging/messages.go rename to cmd/cli/internal/messaging/messages.go index b93655c..2f98226 100644 --- a/cmd/host/internal/messaging/messages.go +++ b/cmd/cli/internal/messaging/messages.go @@ -6,11 +6,6 @@ import ( "io" ) -// This is the message format used to communicate with browser extensions. -// They are encoded by first writing the size of the message as a unit32. -// After that the message is encoded to JSON and written as binary. -// All of this should be done over stdout and stdin. - // MessageKind denotes the kind of message that was sent to or received from a browser extension. type MessageKind string @@ -20,6 +15,8 @@ const ( MessageKindBook MessageKind = "book" // message contains a single book MessageKindVoid MessageKind = "void" // message contains nothing MessageKindTags MessageKind = "tags" // message contains zero or more tags + MessageKindConfigValue MessageKind = "config-value" // message contains a config value + MessageKindParentNames MessageKind = "parent-names" // message contains zero or more parent names MessageKindAddBook MessageKind = "add-book" // message is a request to add a bookmark MessageKindAddFolder MessageKind = "add-folder" // message is a request to add a bookmark MessageKindAddTags MessageKind = "add-tags" // message is a request to add tags diff --git a/cmd/host/internal/messaging/messages_test.go b/cmd/cli/internal/messaging/messages_test.go similarity index 100% rename from cmd/host/internal/messaging/messages_test.go rename to cmd/cli/internal/messaging/messages_test.go diff --git a/cmd/host/internal/messaging/payload.go b/cmd/cli/internal/messaging/payload.go similarity index 93% rename from cmd/host/internal/messaging/payload.go rename to cmd/cli/internal/messaging/payload.go index 8b2d3d1..6c6a0a5 100644 --- a/cmd/host/internal/messaging/payload.go +++ b/cmd/cli/internal/messaging/payload.go @@ -12,7 +12,7 @@ import ( // Payload are the payloads that a message can have. type Payload interface { - AddBookPayload | AddFolderPayload | AddTagsPayload | ListBooksPayload | ListTagsPayload | RemoveBookPayload | RemoveFolderPayload | RemoveTagsPayload | UpdateBookPayload | UpdateFolderPayload | ErrorPayload | BooksPayload | BookPayload | TagsPayload | VoidPayload + AddBookPayload | AddFolderPayload | AddTagsPayload | ListBooksPayload | ListTagsPayload | RemoveBookPayload | RemoveFolderPayload | RemoveTagsPayload | UpdateBookPayload | UpdateFolderPayload | ErrorPayload | BooksPayload | BookPayload | TagsPayload | VoidPayload | ConfigValuePayload | ParentNamesPayload } // AddBookPayload is a payload for a request to add a bookmark. @@ -127,6 +127,16 @@ type TagsPayload struct { Tags []string `json:"tags"` } +// ConfigValuePayload is a payload for a response with a config value in it. +type ConfigValuePayload struct { + Value string `json:"value"` +} + +// ParentNamesPayload is a payload for a response with a books parents names in it. +type ParentNamesPayload struct { + ParentNames []string +} + // VoidPayload is a payload for a response with nothing in it. type VoidPayload struct{} diff --git a/cmd/cli/tui/controls/footer/footer.go b/cmd/cli/internal/tui/controls/footer/footer.go similarity index 96% rename from cmd/cli/tui/controls/footer/footer.go rename to cmd/cli/internal/tui/controls/footer/footer.go index bc41e40..66f383c 100644 --- a/cmd/cli/tui/controls/footer/footer.go +++ b/cmd/cli/internal/tui/controls/footer/footer.go @@ -5,8 +5,8 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" - "github.com/jonathanhope/armaria/cmd/cli/tui/controls/textinput" - "github.com/jonathanhope/armaria/cmd/cli/tui/utils" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/controls/textinput" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/utils" ) const HelpInfoWidth = 7 // width of the help info in the footer diff --git a/cmd/cli/tui/controls/footer/footer_test.go b/cmd/cli/internal/tui/controls/footer/footer_test.go similarity index 95% rename from cmd/cli/tui/controls/footer/footer_test.go rename to cmd/cli/internal/tui/controls/footer/footer_test.go index 558317b..6a20b70 100644 --- a/cmd/cli/tui/controls/footer/footer_test.go +++ b/cmd/cli/internal/tui/controls/footer/footer_test.go @@ -5,7 +5,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" - "github.com/jonathanhope/armaria/cmd/cli/tui/controls/textinput" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/controls/textinput" ) const Name = "footer" diff --git a/cmd/cli/tui/controls/header/header.go b/cmd/cli/internal/tui/controls/header/header.go similarity index 97% rename from cmd/cli/tui/controls/header/header.go rename to cmd/cli/internal/tui/controls/header/header.go index 8d00c42..b7fc622 100644 --- a/cmd/cli/tui/controls/header/header.go +++ b/cmd/cli/internal/tui/controls/header/header.go @@ -4,7 +4,7 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss/table" - "github.com/jonathanhope/armaria/cmd/cli/tui/utils" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/utils" ) // HeaderModel is the model for a header. diff --git a/cmd/cli/tui/controls/header/header_test.go b/cmd/cli/internal/tui/controls/header/header_test.go similarity index 100% rename from cmd/cli/tui/controls/header/header_test.go rename to cmd/cli/internal/tui/controls/header/header_test.go diff --git a/cmd/cli/tui/controls/help/help.go b/cmd/cli/internal/tui/controls/help/help.go similarity index 100% rename from cmd/cli/tui/controls/help/help.go rename to cmd/cli/internal/tui/controls/help/help.go diff --git a/cmd/cli/tui/controls/help/help_test.go b/cmd/cli/internal/tui/controls/help/help_test.go similarity index 95% rename from cmd/cli/tui/controls/help/help_test.go rename to cmd/cli/internal/tui/controls/help/help_test.go index 5e36b7e..dd2dffa 100644 --- a/cmd/cli/tui/controls/help/help_test.go +++ b/cmd/cli/internal/tui/controls/help/help_test.go @@ -5,7 +5,7 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/google/go-cmp/cmp" - "github.com/jonathanhope/armaria/cmd/cli/tui/utils" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/utils" ) const Name = "help" diff --git a/cmd/cli/tui/controls/scrolltable/scrolltable.go b/cmd/cli/internal/tui/controls/scrolltable/scrolltable.go similarity index 98% rename from cmd/cli/tui/controls/scrolltable/scrolltable.go rename to cmd/cli/internal/tui/controls/scrolltable/scrolltable.go index a8366da..1e14e51 100644 --- a/cmd/cli/tui/controls/scrolltable/scrolltable.go +++ b/cmd/cli/internal/tui/controls/scrolltable/scrolltable.go @@ -4,8 +4,8 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss/table" - "github.com/jonathanhope/armaria/cmd/cli/tui/msgs" - "github.com/jonathanhope/armaria/cmd/cli/tui/utils" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/msgs" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/utils" "github.com/samber/lo" ) diff --git a/cmd/cli/tui/controls/scrolltable/scrolltable_test.go b/cmd/cli/internal/tui/controls/scrolltable/scrolltable_test.go similarity index 98% rename from cmd/cli/tui/controls/scrolltable/scrolltable_test.go rename to cmd/cli/internal/tui/controls/scrolltable/scrolltable_test.go index 042fbe2..bf24b5c 100644 --- a/cmd/cli/tui/controls/scrolltable/scrolltable_test.go +++ b/cmd/cli/internal/tui/controls/scrolltable/scrolltable_test.go @@ -5,8 +5,8 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/google/go-cmp/cmp" - "github.com/jonathanhope/armaria/cmd/cli/tui/msgs" - "github.com/jonathanhope/armaria/cmd/cli/tui/utils" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/msgs" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/utils" ) func TestCanUpdateData(t *testing.T) { diff --git a/cmd/cli/tui/controls/textinput/textinput.go b/cmd/cli/internal/tui/controls/textinput/textinput.go similarity index 99% rename from cmd/cli/tui/controls/textinput/textinput.go rename to cmd/cli/internal/tui/controls/textinput/textinput.go index 888339f..2e76fee 100644 --- a/cmd/cli/tui/controls/textinput/textinput.go +++ b/cmd/cli/internal/tui/controls/textinput/textinput.go @@ -5,7 +5,7 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" - "github.com/jonathanhope/armaria/cmd/cli/tui/msgs" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/msgs" "github.com/muesli/reflow/ansi" ) diff --git a/cmd/cli/tui/controls/textinput/textinput_test.go b/cmd/cli/internal/tui/controls/textinput/textinput_test.go similarity index 100% rename from cmd/cli/tui/controls/textinput/textinput_test.go rename to cmd/cli/internal/tui/controls/textinput/textinput_test.go diff --git a/cmd/cli/tui/controls/typeahead/typeahead.go b/cmd/cli/internal/tui/controls/typeahead/typeahead.go similarity index 97% rename from cmd/cli/tui/controls/typeahead/typeahead.go rename to cmd/cli/internal/tui/controls/typeahead/typeahead.go index c866d2e..c0fa5d1 100644 --- a/cmd/cli/tui/controls/typeahead/typeahead.go +++ b/cmd/cli/internal/tui/controls/typeahead/typeahead.go @@ -5,9 +5,9 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" - "github.com/jonathanhope/armaria/cmd/cli/tui/controls/scrolltable" - "github.com/jonathanhope/armaria/cmd/cli/tui/controls/textinput" - "github.com/jonathanhope/armaria/cmd/cli/tui/msgs" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/controls/scrolltable" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/controls/textinput" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/msgs" "github.com/samber/lo" ) diff --git a/cmd/cli/tui/controls/typeahead/typeahead_test.go b/cmd/cli/internal/tui/controls/typeahead/typeahead_test.go similarity index 95% rename from cmd/cli/tui/controls/typeahead/typeahead_test.go rename to cmd/cli/internal/tui/controls/typeahead/typeahead_test.go index 8b9d358..55547a2 100644 --- a/cmd/cli/tui/controls/typeahead/typeahead_test.go +++ b/cmd/cli/internal/tui/controls/typeahead/typeahead_test.go @@ -6,8 +6,8 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" - "github.com/jonathanhope/armaria/cmd/cli/tui/msgs" - "github.com/jonathanhope/armaria/cmd/cli/tui/utils" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/msgs" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/utils" ) const name = "typeaheaed" diff --git a/cmd/cli/tui/msgs/msgs.go b/cmd/cli/internal/tui/msgs/msgs.go similarity index 100% rename from cmd/cli/tui/msgs/msgs.go rename to cmd/cli/internal/tui/msgs/msgs.go diff --git a/cmd/cli/tui/program.go b/cmd/cli/internal/tui/program.go similarity index 90% rename from cmd/cli/tui/program.go rename to cmd/cli/internal/tui/program.go index de5bdf0..9b9a046 100644 --- a/cmd/cli/tui/program.go +++ b/cmd/cli/internal/tui/program.go @@ -2,9 +2,9 @@ package tui import ( tea "github.com/charmbracelet/bubbletea" - "github.com/jonathanhope/armaria/cmd/cli/tui/msgs" - "github.com/jonathanhope/armaria/cmd/cli/tui/views/booksview" - "github.com/jonathanhope/armaria/cmd/cli/tui/views/errorview" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/msgs" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/views/booksview" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/views/errorview" ) // model is an app level model that has multiple view models. diff --git a/cmd/cli/tui/utils/cmp_cmd.go b/cmd/cli/internal/tui/utils/cmp_cmd.go similarity index 100% rename from cmd/cli/tui/utils/cmp_cmd.go rename to cmd/cli/internal/tui/utils/cmp_cmd.go diff --git a/cmd/cli/tui/utils/substr.go b/cmd/cli/internal/tui/utils/substr.go similarity index 100% rename from cmd/cli/tui/utils/substr.go rename to cmd/cli/internal/tui/utils/substr.go diff --git a/cmd/cli/tui/utils/substr_test.go b/cmd/cli/internal/tui/utils/substr_test.go similarity index 100% rename from cmd/cli/tui/utils/substr_test.go rename to cmd/cli/internal/tui/utils/substr_test.go diff --git a/cmd/cli/tui/views/booksview/books.go b/cmd/cli/internal/tui/views/booksview/books.go similarity index 98% rename from cmd/cli/tui/views/booksview/books.go rename to cmd/cli/internal/tui/views/booksview/books.go index 240be8c..6f9f1ef 100644 --- a/cmd/cli/tui/views/booksview/books.go +++ b/cmd/cli/internal/tui/views/booksview/books.go @@ -8,12 +8,12 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" - "github.com/jonathanhope/armaria/cmd/cli/tui/controls/footer" - "github.com/jonathanhope/armaria/cmd/cli/tui/controls/header" - "github.com/jonathanhope/armaria/cmd/cli/tui/controls/help" - "github.com/jonathanhope/armaria/cmd/cli/tui/controls/scrolltable" - "github.com/jonathanhope/armaria/cmd/cli/tui/controls/typeahead" - "github.com/jonathanhope/armaria/cmd/cli/tui/msgs" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/controls/footer" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/controls/header" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/controls/help" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/controls/scrolltable" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/controls/typeahead" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/msgs" "github.com/jonathanhope/armaria/pkg" "github.com/samber/lo" ) diff --git a/cmd/cli/tui/views/errorview/error.go b/cmd/cli/internal/tui/views/errorview/error.go similarity index 94% rename from cmd/cli/tui/views/errorview/error.go rename to cmd/cli/internal/tui/views/errorview/error.go index 397e01a..b1be4a2 100644 --- a/cmd/cli/tui/views/errorview/error.go +++ b/cmd/cli/internal/tui/views/errorview/error.go @@ -5,7 +5,7 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" - "github.com/jonathanhope/armaria/cmd/cli/tui/msgs" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/msgs" ) // model is the model for an error view. diff --git a/cmd/cli/tui/views/errorview/error_test.go b/cmd/cli/internal/tui/views/errorview/error_test.go similarity index 88% rename from cmd/cli/tui/views/errorview/error_test.go rename to cmd/cli/internal/tui/views/errorview/error_test.go index 2f58e06..9d6e234 100644 --- a/cmd/cli/tui/views/errorview/error_test.go +++ b/cmd/cli/internal/tui/views/errorview/error_test.go @@ -7,8 +7,8 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" - "github.com/jonathanhope/armaria/cmd/cli/tui/msgs" - "github.com/jonathanhope/armaria/cmd/cli/tui/utils" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/msgs" + "github.com/jonathanhope/armaria/cmd/cli/internal/tui/utils" ) func TestHandlesErrorMessage(t *testing.T) { diff --git a/cmd/cli/main.go b/cmd/cli/main.go index dbf8c95..da324da 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -1,18 +1,34 @@ package main import ( + "fmt" "os" + "slices" "github.com/alecthomas/kong" "github.com/jonathanhope/armaria/cmd/cli/internal" + "github.com/jonathanhope/armaria/cmd/cli/internal/messaging" + "github.com/jonathanhope/armaria/internal/manifest" ) var version string func main() { + // When a browser sends a native message it will send the extension ID as the last argument. + // When we see one of Armaria's extension IDs we switch to native messaging mode. + extensionIds := []string{manifest.FirefoxExtension, manifest.ChromeExtension1, manifest.ChromeExtension2} + lastArg := os.Args[len(os.Args)-1] + if slices.Contains(extensionIds, lastArg) { + if err := messaging.Dispatch(os.Stdin, os.Stdout); err != nil { + fmt.Printf("Unexpected error: %s", err) + os.Exit(1) + } + os.Exit(0) + } + + // Otherwise we fall into the standard CLI. rootCmd := cmd.RootCmdFactory() ctx := kong.Parse(&rootCmd) - err := ctx.Run(&cmd.Context{ DB: rootCmd.DB, Formatter: rootCmd.Formatter, diff --git a/cmd/host/internal/test/add_book_test.go b/cmd/cli/test/native_host/add_book_test.go similarity index 98% rename from cmd/host/internal/test/add_book_test.go rename to cmd/cli/test/native_host/add_book_test.go index 07ba84b..020233b 100644 --- a/cmd/host/internal/test/add_book_test.go +++ b/cmd/cli/test/native_host/add_book_test.go @@ -7,7 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/uuid" - "github.com/jonathanhope/armaria/cmd/host/internal/messaging" + "github.com/jonathanhope/armaria/cmd/cli/internal/messaging" "github.com/jonathanhope/armaria/internal/null" "github.com/jonathanhope/armaria/pkg" ) diff --git a/cmd/host/internal/test/add_folder_test.go b/cmd/cli/test/native_host/add_folder_test.go similarity index 97% rename from cmd/host/internal/test/add_folder_test.go rename to cmd/cli/test/native_host/add_folder_test.go index b4687ae..bad7445 100644 --- a/cmd/host/internal/test/add_folder_test.go +++ b/cmd/cli/test/native_host/add_folder_test.go @@ -7,7 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/uuid" - "github.com/jonathanhope/armaria/cmd/host/internal/messaging" + "github.com/jonathanhope/armaria/cmd/cli/internal/messaging" "github.com/jonathanhope/armaria/internal/null" "github.com/jonathanhope/armaria/pkg" ) diff --git a/cmd/host/internal/test/add_tags_test.go b/cmd/cli/test/native_host/add_tags_test.go similarity index 94% rename from cmd/host/internal/test/add_tags_test.go rename to cmd/cli/test/native_host/add_tags_test.go index 552302d..514f89b 100644 --- a/cmd/host/internal/test/add_tags_test.go +++ b/cmd/cli/test/native_host/add_tags_test.go @@ -7,7 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/uuid" - "github.com/jonathanhope/armaria/cmd/host/internal/messaging" + "github.com/jonathanhope/armaria/cmd/cli/internal/messaging" "github.com/jonathanhope/armaria/internal/null" "github.com/jonathanhope/armaria/pkg" ) diff --git a/cmd/host/internal/test/helpers.go b/cmd/cli/test/native_host/helpers.go similarity index 96% rename from cmd/host/internal/test/helpers.go rename to cmd/cli/test/native_host/helpers.go index 0cf2a46..d11734e 100644 --- a/cmd/host/internal/test/helpers.go +++ b/cmd/cli/test/native_host/helpers.go @@ -5,7 +5,7 @@ import ( "database/sql" "github.com/blockloop/scan/v2" - "github.com/jonathanhope/armaria/cmd/host/internal/messaging" + "github.com/jonathanhope/armaria/cmd/cli/internal/messaging" "github.com/nullism/bqb" ) diff --git a/cmd/host/internal/test/list_books_test.go b/cmd/cli/test/native_host/list_books_test.go similarity index 99% rename from cmd/host/internal/test/list_books_test.go rename to cmd/cli/test/native_host/list_books_test.go index 66439c9..6ebda46 100644 --- a/cmd/host/internal/test/list_books_test.go +++ b/cmd/cli/test/native_host/list_books_test.go @@ -7,7 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/uuid" - "github.com/jonathanhope/armaria/cmd/host/internal/messaging" + "github.com/jonathanhope/armaria/cmd/cli/internal/messaging" "github.com/jonathanhope/armaria/internal/null" "github.com/jonathanhope/armaria/pkg" ) diff --git a/cmd/host/internal/test/list_tags_test.go b/cmd/cli/test/native_host/list_tags_test.go similarity index 98% rename from cmd/host/internal/test/list_tags_test.go rename to cmd/cli/test/native_host/list_tags_test.go index da9577c..67a555e 100644 --- a/cmd/host/internal/test/list_tags_test.go +++ b/cmd/cli/test/native_host/list_tags_test.go @@ -7,7 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/uuid" - "github.com/jonathanhope/armaria/cmd/host/internal/messaging" + "github.com/jonathanhope/armaria/cmd/cli/internal/messaging" "github.com/jonathanhope/armaria/internal/null" "github.com/jonathanhope/armaria/pkg" ) diff --git a/cmd/host/internal/test/remove_book_test.go b/cmd/cli/test/native_host/remove_book_test.go similarity index 93% rename from cmd/host/internal/test/remove_book_test.go rename to cmd/cli/test/native_host/remove_book_test.go index 9bab6ab..8f2c0f2 100644 --- a/cmd/host/internal/test/remove_book_test.go +++ b/cmd/cli/test/native_host/remove_book_test.go @@ -7,7 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/uuid" - "github.com/jonathanhope/armaria/cmd/host/internal/messaging" + "github.com/jonathanhope/armaria/cmd/cli/internal/messaging" "github.com/jonathanhope/armaria/internal/null" "github.com/jonathanhope/armaria/pkg" ) diff --git a/cmd/host/internal/test/remove_folder_test.go b/cmd/cli/test/native_host/remove_folder_test.go similarity index 93% rename from cmd/host/internal/test/remove_folder_test.go rename to cmd/cli/test/native_host/remove_folder_test.go index c3fad2c..8cca416 100644 --- a/cmd/host/internal/test/remove_folder_test.go +++ b/cmd/cli/test/native_host/remove_folder_test.go @@ -7,7 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/uuid" - "github.com/jonathanhope/armaria/cmd/host/internal/messaging" + "github.com/jonathanhope/armaria/cmd/cli/internal/messaging" "github.com/jonathanhope/armaria/internal/null" "github.com/jonathanhope/armaria/pkg" ) diff --git a/cmd/host/internal/test/remove_tags_test.go b/cmd/cli/test/native_host/remove_tags_test.go similarity index 95% rename from cmd/host/internal/test/remove_tags_test.go rename to cmd/cli/test/native_host/remove_tags_test.go index 535b851..ff9ee70 100644 --- a/cmd/host/internal/test/remove_tags_test.go +++ b/cmd/cli/test/native_host/remove_tags_test.go @@ -7,7 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/uuid" - "github.com/jonathanhope/armaria/cmd/host/internal/messaging" + "github.com/jonathanhope/armaria/cmd/cli/internal/messaging" "github.com/jonathanhope/armaria/internal/null" "github.com/jonathanhope/armaria/pkg" ) diff --git a/cmd/host/internal/test/update_book_test.go b/cmd/cli/test/native_host/update_book_test.go similarity index 99% rename from cmd/host/internal/test/update_book_test.go rename to cmd/cli/test/native_host/update_book_test.go index 8a09f39..26374c7 100644 --- a/cmd/host/internal/test/update_book_test.go +++ b/cmd/cli/test/native_host/update_book_test.go @@ -7,7 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/uuid" - "github.com/jonathanhope/armaria/cmd/host/internal/messaging" + "github.com/jonathanhope/armaria/cmd/cli/internal/messaging" "github.com/jonathanhope/armaria/internal/null" "github.com/jonathanhope/armaria/pkg" "github.com/samber/lo" diff --git a/cmd/host/internal/test/update_folder_test.go b/cmd/cli/test/native_host/update_folder_test.go similarity index 99% rename from cmd/host/internal/test/update_folder_test.go rename to cmd/cli/test/native_host/update_folder_test.go index c418bb9..39d99de 100644 --- a/cmd/host/internal/test/update_folder_test.go +++ b/cmd/cli/test/native_host/update_folder_test.go @@ -7,7 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/uuid" - "github.com/jonathanhope/armaria/cmd/host/internal/messaging" + "github.com/jonathanhope/armaria/cmd/cli/internal/messaging" "github.com/jonathanhope/armaria/internal/null" "github.com/jonathanhope/armaria/pkg" "github.com/samber/lo" diff --git a/cmd/cli/test/steps.go b/cmd/cli/test/steps.go index 964a1c5..783b4d7 100644 --- a/cmd/cli/test/steps.go +++ b/cmd/cli/test/steps.go @@ -11,7 +11,7 @@ import ( "github.com/cucumber/godog" "github.com/google/go-cmp/cmp" "github.com/google/uuid" - "github.com/jonathanhope/armaria/cmd/cli/internal" + "github.com/jonathanhope/armaria/cmd/cli/internal/messaging" "github.com/jonathanhope/armaria/internal/null" ) @@ -161,10 +161,11 @@ func theFollowingBookmarksFoldersExist(ctx context.Context, table *godog.Table) return err } - var actual []cmd.BookDTO - if err := json.Unmarshal([]byte(output), &actual); err != nil { + payload, err := receiveMessage[messaging.BooksPayload](output, messaging.MessageKindBooks) + if err != nil { return err } + actual := payload.Books expected, err := tableToBooks(vars, actual, table) if err != nil { @@ -198,10 +199,11 @@ func theFollowingTagsExist(ctx context.Context, table *godog.Table) error { return err } - var actual []string - if err := json.Unmarshal([]byte(output), &actual); err != nil { + payload, err := receiveMessage[messaging.TagsPayload](output, messaging.MessageKindTags) + if err != nil { return err } + actual := payload.Tags expected := tableToTags(table) if err != nil { @@ -227,9 +229,13 @@ func theFollowingErrorIsReturned(ctx context.Context, message *godog.DocString) return errors.New("Missing output") } - expected := strings.TrimSpace(fmt.Sprintf("\"%s\"", message.Content)) + payload, err := receiveMessage[messaging.ErrorPayload](output, messaging.MessageKindError) + if err != nil { + return err + } + actual := strings.TrimSpace(payload.Error) - actual := strings.TrimSpace(output) + expected := strings.TrimSpace(message.Content) diff := cmp.Diff(expected, actual) if diff != "" { @@ -246,17 +252,17 @@ func theFolllowingTagsAreReturned(ctx context.Context, table *godog.Table) error return errors.New("Missing output") } + payload, err := receiveMessage[messaging.TagsPayload](output, messaging.MessageKindTags) + if err != nil { + return err + } + actual := payload.Tags + var expected []string for _, row := range table.Rows[1:] { expected = append(expected, row.Cells[0].Value) } - var actual []string - err := json.Unmarshal([]byte(output), &actual) - if err != nil { - return err - } - diff := cmp.Diff(expected, actual) if diff != "" { return fmt.Errorf("Expected and actual tags different:\n%s", diff) @@ -272,17 +278,17 @@ func theFolllowingNamesAreReturned(ctx context.Context, table *godog.Table) erro return errors.New("Missing output") } + payload, err := receiveMessage[messaging.ParentNamesPayload](output, messaging.MessageKindParentNames) + if err != nil { + return err + } + actual := payload.ParentNames + var expected []string for _, row := range table.Rows { expected = append(expected, row.Cells[0].Value) } - var actual []string - err := json.Unmarshal([]byte(output), &actual) - if err != nil { - return err - } - diff := cmp.Diff(expected, actual) if diff != "" { return fmt.Errorf("Expected and actual names different:\n%s", diff) @@ -303,11 +309,11 @@ func theFolllowingBooksAreReturned(ctx context.Context, table *godog.Table) erro return errors.New("Missing variables") } - var actual []cmd.BookDTO - err := json.Unmarshal([]byte(output), &actual) + payload, err := receiveMessage[messaging.BooksPayload](output, messaging.MessageKindBooks) if err != nil { return err } + actual := payload.Books expected, err := tableToBooks(vars, actual, table) if err != nil { @@ -323,3 +329,24 @@ func theFolllowingBooksAreReturned(ctx context.Context, table *godog.Table) erro return nil } + +// receiveMessage receives a message from the CLI. +func receiveMessage[T messaging.Payload](output string, kind messaging.MessageKind) (T, error) { + var payload T + + msg := messaging.NativeMessage{} + err := json.Unmarshal([]byte(output), &msg) + if err != nil { + return payload, err + } + if msg.Kind != kind { + return payload, errors.New("Didn't receive expected message kind") + } + + err = json.Unmarshal([]byte(msg.Payload), &payload) + if err != nil { + return payload, err + } + + return payload, nil +} diff --git a/cmd/cli/test/test_helpers.go b/cmd/cli/test/test_helpers.go index 944b1be..f4e6f1d 100644 --- a/cmd/cli/test/test_helpers.go +++ b/cmd/cli/test/test_helpers.go @@ -13,6 +13,7 @@ import ( "github.com/google/shlex" "github.com/google/uuid" "github.com/jonathanhope/armaria/cmd/cli/internal" + "github.com/jonathanhope/armaria/cmd/cli/internal/messaging" "github.com/jonathanhope/armaria/internal/null" "github.com/jonathanhope/armaria/pkg" ) @@ -179,8 +180,8 @@ func insert(args insertArgs) error { } // tableToBooks converts a cucumber table to a collection of bookmarks/folders. -func tableToBooks(vars map[string]interface{}, actual []cmd.BookDTO, table *godog.Table) ([]cmd.BookDTO, error) { - books := make([]cmd.BookDTO, 0) +func tableToBooks(vars map[string]interface{}, actual []messaging.BookDTO, table *godog.Table) ([]messaging.BookDTO, error) { + books := make([]messaging.BookDTO, 0) for _, row := range table.Rows[1:] { id, storeId, _, err := handleString(vars, row.Cells[0].Value) if err != nil { @@ -250,7 +251,7 @@ func tableToBooks(vars map[string]interface{}, actual []cmd.BookDTO, table *godo tags = last.Tags } - book := cmd.BookDTO{ + book := messaging.BookDTO{ ID: id, ParentID: parentId, IsFolder: isFolder, @@ -434,7 +435,7 @@ func noop(code int) {} // markDirty marks every field as dirty. // The dirty field is not relevant to tests. -func markDirty(expected []cmd.BookDTO, actual []cmd.BookDTO) { +func markDirty(expected []messaging.BookDTO, actual []messaging.BookDTO) { for i := range expected { expected[i].Description.Dirty = false expected[i].ParentID.Dirty = false diff --git a/cmd/host/main.go b/cmd/host/main.go deleted file mode 100644 index 9c43a0a..0000000 --- a/cmd/host/main.go +++ /dev/null @@ -1,17 +0,0 @@ -package main - -import ( - "log" - "os" - - "github.com/jonathanhope/armaria/cmd/host/internal/messaging" -) - -// Browser extensions can only access local resources (like files) with something called native messaging. -// This is a native messaging host that allows the Armaria extension to communicate with Armaria itself. - -func main() { - if err := messaging.Dispatch(os.Stdin, os.Stdout); err != nil { - log.Fatalf("Unexpected error: %s", err) - } -} diff --git a/flake.nix b/flake.nix index 91d9f1b..f86bb81 100644 --- a/flake.nix +++ b/flake.nix @@ -26,6 +26,7 @@ goreleaser syft delve + upx ]; hardeningDisable = [ "fortify" ]; shellHook = '' diff --git a/internal/manifest/install.go b/internal/manifest/install.go index 5d4d6ab..49f5641 100644 --- a/internal/manifest/install.go +++ b/internal/manifest/install.go @@ -9,9 +9,9 @@ import ( const name = "armaria" const description = "Armaria is a fast local-first bookmarks manager." const hostType = "stdio" -const firefoxExtension = "armaria@net" -const chromeExtension1 = "chrome-extension://cahkgigfdplmhgjbioakkgennhncioli/" -const chromeExtension2 = "chrome-extension://fbnilfpngakppdkddndcnckolmlpghdf/" +const FirefoxExtension = "armaria@armaria.net" +const ChromeExtension1 = "chrome-extension://cahkgigfdplmhgjbioakkgennhncioli/" +const ChromeExtension2 = "chrome-extension://fbnilfpngakppdkddndcnckolmlpghdf/" // TODO: Windows needs registry entiries. @@ -27,8 +27,8 @@ func InstallManifest(path string, hostPath string, manifestType ManifestType) er Path: hostPath, HostType: hostType, AllowedOrigins: []string{ - chromeExtension1, - chromeExtension2, + ChromeExtension1, + ChromeExtension2, }, } @@ -42,7 +42,7 @@ func InstallManifest(path string, hostPath string, manifestType ManifestType) er Description: description, Path: hostPath, HostType: hostType, - AllowedExtensions: []string{firefoxExtension}, + AllowedExtensions: []string{FirefoxExtension}, } buffer, err = json.Marshal(manifest) diff --git a/internal/paths/paths.go b/internal/paths/paths.go index fa14599..32e9fc0 100644 --- a/internal/paths/paths.go +++ b/internal/paths/paths.go @@ -145,7 +145,7 @@ func hostInternal(getenv getenvFn, executable executableFn, dir dirFn, join join snapHome := getenv("SNAP_REAL_HOME") if snapHome != "" { - return "/snap/bin/armaria.armaria-host", nil + return "/snap/bin/armaria", nil } // Otherwise we can assume the host is installed alongside the CLI. @@ -154,7 +154,7 @@ func hostInternal(getenv getenvFn, executable executableFn, dir dirFn, join join if err != nil { return "", fmt.Errorf("error getting current executable while getting host path: %w", err) } - return join(dir(ex), "armaria-host"), nil + return join(dir(ex), "armaria"), nil } // FirefoxManifest gets the path to the Firefox app manifest. diff --git a/internal/paths/paths_test.go b/internal/paths/paths_test.go index 1b9071e..0b616dd 100644 --- a/internal/paths/paths_test.go +++ b/internal/paths/paths_test.go @@ -231,11 +231,11 @@ func TestHostPath(t *testing.T) { tests := []test{ { snapRealHome: "", - hostPath: "/usr/bin/armaria-host", + hostPath: "/usr/bin/armaria", }, { snapRealHome: "/snap", - hostPath: "/snap/bin/armaria.armaria-host", + hostPath: "/snap/bin/armaria", }, }