Skip to content

Commit

Permalink
wip: combine cli and host
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanHope committed Jun 1, 2024
1 parent baa6bd0 commit 676d6bb
Show file tree
Hide file tree
Showing 50 changed files with 185 additions and 223 deletions.
90 changes: 5 additions & 85 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -135,43 +80,18 @@ 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:
- armaria-darwin-amd64
- 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
Expand All @@ -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
Expand All @@ -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'

Expand All @@ -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
Expand All @@ -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
Expand Down
10 changes: 1 addition & 9 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -34,7 +29,6 @@ tasks:
build:
cmds:
- task: build-cli
- task: build-host
- task: build-seeder
migrate-up:
cmds:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/internal/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
68 changes: 37 additions & 31 deletions cmd/cli/internal/formatters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions cmd/cli/internal/messaging/doc.go
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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{}

Expand Down
Loading

0 comments on commit 676d6bb

Please sign in to comment.