From e4788b6af46370afd5183205244a161ba79d59b3 Mon Sep 17 00:00:00 2001 From: Kamil Samigullin Date: Mon, 31 Jul 2023 10:03:18 +0300 Subject: [PATCH] feat: switch to single binary instead of splitted by server and client --- .goreleaser.yml | 40 ++++++-------------------------------- Makefile | 2 +- bin/install | 12 ++++++------ main.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ main_test.go | 32 +++++++++++++++++++++++++++++++ 5 files changed, 96 insertions(+), 41 deletions(-) create mode 100644 main.go create mode 100644 main_test.go diff --git a/.goreleaser.yml b/.goreleaser.yml index 63d0c41..600ac35 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -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 } diff --git a/Makefile b/Makefile index 6d13d0a..1c304fe 100644 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/bin/install b/bin/install index e59b810..6e71284 100644 --- a/bin/install +++ b/bin/install @@ -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) diff --git a/main.go b/main.go new file mode 100644 index 0000000..69cfe3b --- /dev/null +++ b/main.go @@ -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) +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..71de419 --- /dev/null +++ b/main_test.go @@ -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) + }) +}