Skip to content

Commit

Permalink
feat: switch to single binary instead of splitted by server and client
Browse files Browse the repository at this point in the history
kamilsk committed Jul 31, 2023
1 parent a9906fa commit e4788b6
Showing 5 changed files with 96 additions and 41 deletions.
40 changes: 6 additions & 34 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -2,9 +2,6 @@ project_name: sparkle

archives:
- id: sparkle
builds:
- client
- server
files:
- LICENSE
- PATENTS
@@ -16,7 +13,9 @@ brews:
commit_author:
name: Kamil Samigullin
email: kamil@samigullin.info
description: ✨ Sparkle service.
description: |
✨ Sparkle service. The personal development framework
and Personal Knowledge Management platform.
folder: Formula
homepage: https://sparkle.wiki/
repository:
@@ -34,39 +33,12 @@ brews:
output = Utils.popen_read("#{bin}/sparkle completion zsh")
(zsh_completion/"_sparkle").write output
bin.install "sparklectl"
output = Utils.popen_read("#{bin}/sparklectl completion bash")
(bash_completion/"sparklectl").write output
output = Utils.popen_read("#{bin}/sparklectl completion fish")
(fish_completion/"sparklectl.fish").write output
output = Utils.popen_read("#{bin}/sparklectl completion zsh")
(zsh_completion/"_sparklectl").write output
prefix.install_metafiles
test: |
system "#{bin}/sparkle version"
system "#{bin}/sparklectl version"
system "#{bin}/sparkle version"
builds:
- id: client
binary: sparklectl
env:
- CGO_ENABLED=0
flags:
- -trimpath
goarch:
- amd64
- arm64
goos:
- darwin
- linux
ldflags:
- -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}
main: ./cmd/client
- id: server
- id: sparkle
binary: sparkle
env:
- CGO_ENABLED=0
@@ -80,7 +52,7 @@ builds:
- linux
ldflags:
- -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}
main: ./cmd/server
main: .

checksum: { name_template: checksums.txt }

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -312,7 +312,7 @@ go-server:
.PHONY: go-server

go-dist-check:
$(AT) goreleaser --clean --skip-publish --snapshot
$(AT) goreleaser --clean --skip=publish --snapshot
.PHONY: go-dist-check

go-dist-clean:
12 changes: 6 additions & 6 deletions bin/install
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh
set -e
# Code generated by godownloader on 2023-10-12T21:05:39Z. DO NOT EDIT.
# Code generated by godownloader on 2023-10-26T11:22:01Z. DO NOT EDIT.
#

usage() {
@@ -62,10 +62,10 @@ execute() {
}
get_binaries() {
case "$PLATFORM" in
darwin/amd64) BINARIES="sparklectl sparkle" ;;
darwin/arm64) BINARIES="sparklectl sparkle" ;;
linux/amd64) BINARIES="sparklectl sparkle" ;;
linux/arm64) BINARIES="sparklectl sparkle" ;;
darwin/amd64) BINARIES="sparkle" ;;
darwin/arm64) BINARIES="sparkle" ;;
linux/amd64) BINARIES="sparkle" ;;
linux/arm64) BINARIES="sparkle" ;;
*)
log_crit "platform $PLATFORM is not supported. Make sure this script is up-to-date and file request at https://github.com/${PREFIX}/issues/new"
exit 1
@@ -335,7 +335,7 @@ EOF
PROJECT_NAME="sparkle"
OWNER=withsparkle
REPO="service"
BINARY=sparklectl
BINARY=sparkle
FORMAT=tar.gz
OS=$(uname_os)
ARCH=$(uname_arch)
51 changes: 51 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"context"
"fmt"
"os"

"github.com/fatih/color"
"go.octolab.org/errors"
"go.octolab.org/safe"
"go.octolab.org/toolkit/cli/cobra"
"go.octolab.org/unsafe"

"go.octolab.org/ecosystem/sparkle/internal/command"
"go.octolab.org/ecosystem/sparkle/internal/config"
)

const unknown = "unknown"

var (
commit = unknown
date = unknown
version = "dev"
exit = os.Exit
stderr = color.Error
stdout = color.Output
)

func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

root := command.New()
root.SetErr(stderr)
root.SetOut(stdout)
root.AddCommand(
cobra.NewVersionCommand(version, date, commit, config.Features...),
)

safe.Do(func() error { return root.ExecuteContext(ctx) }, shutdown)
}

func shutdown(err error) {
var recovered errors.Recovered
if errors.As(err, &recovered) {
unsafe.DoSilent(fmt.Fprintf(stderr, "recovered: %+v\n", recovered.Cause()))
unsafe.DoSilent(fmt.Fprintln(stderr, "---"))
unsafe.DoSilent(fmt.Fprintf(stderr, "%+v\n", err))
}
exit(1)
}
32 changes: 32 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"io"
"os"
"testing"

"github.com/stretchr/testify/assert"
"go.octolab.org/safe"
)

func TestExecution(t *testing.T) {
t.Run("success", func(t *testing.T) {
exit = func(code int) { assert.Equal(t, 0, code) }
stderr, stdout = io.Discard, io.Discard
os.Args = []string{"root", "version"}
main()
})

t.Run("failure", func(t *testing.T) {
exit = func(code int) { assert.Equal(t, 1, code) }
stderr, stdout = io.Discard, io.Discard
os.Args = []string{"root", "unknown"}
main()
})

t.Run("shutdown with panic", func(t *testing.T) {
exit = func(code int) { assert.Equal(t, 1, code) }
stderr, stdout = io.Discard, io.Discard
safe.Do(func() error { panic("test") }, shutdown)
})
}

0 comments on commit e4788b6

Please sign in to comment.