Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ jobs:
run: go mod download

- name: Run tests
run: go test -v ./...
run: make test
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ TANTIVY_VERSION := v1.0.4
TANTIVY_LIB_PATH ?= dist/tantivy
CGO_LDFLAGS := -L$(TANTIVY_LIB_PATH)

GOLANGCI_LINT_VERSION := v2.2.1
GOLANGCI_LINT_VERSION := v2.7.2

##@ Build

Expand Down Expand Up @@ -126,9 +126,9 @@ install-linter: ## Install golangci-lint
@go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)
@echo "golangci-lint installed successfully"

test: ## Run tests
test: download-tantivy ## Run tests
@echo "Running tests..."
@go test github.com/anyproto/anytype-cli/...
@CGO_ENABLED=1 CGO_LDFLAGS="$(CGO_LDFLAGS)" go test github.com/anyproto/anytype-cli/...
@echo "Tests completed"

##@ Cleanup
Expand Down
7 changes: 6 additions & 1 deletion cmd/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"github.com/kardianos/service"
"github.com/spf13/cobra"

"github.com/anyproto/anytype-cli/core/config"
"github.com/anyproto/anytype-cli/core/output"
"github.com/anyproto/anytype-cli/core/serviceprogram"
)

var listenAddress string

func NewServeCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "serve",
Expand All @@ -17,6 +20,8 @@ func NewServeCmd() *cobra.Command {
RunE: runServer,
}

cmd.Flags().StringVar(&listenAddress, "listen-address", config.DefaultAPIAddress, "API listen address in `host:port` format")

return cmd
}

Expand All @@ -27,7 +32,7 @@ func runServer(cmd *cobra.Command, args []string) error {
Description: "Anytype",
}

prg := serviceprogram.New()
prg := serviceprogram.New(listenAddress)

s, err := service.New(prg, svcConfig)
if err != nil {
Expand Down
53 changes: 53 additions & 0 deletions cmd/serve/serve_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package serve

import (
"testing"

"github.com/anyproto/anytype-cli/core/config"
)

func TestNewServeCmd(t *testing.T) {
cmd := NewServeCmd()

if cmd.Use != "serve" {
t.Errorf("cmd.Use = %v, want serve", cmd.Use)
}

if len(cmd.Aliases) != 1 || cmd.Aliases[0] != "start" {
t.Errorf("cmd.Aliases = %v, want [start]", cmd.Aliases)
}
}

func TestServeCmd_ListenAddressFlag(t *testing.T) {
cmd := NewServeCmd()

flag := cmd.Flag("listen-address")
if flag == nil {
t.Fatal("listen-address flag not found")
return
}

if flag.DefValue != config.DefaultAPIAddress {
t.Errorf("listen-address default = %v, want %v", flag.DefValue, config.DefaultAPIAddress)
}

if flag.Usage != "API listen address in `host:port` format" {
t.Errorf("listen-address usage = %v, want 'API listen address in `host:port` format'", flag.Usage)
}
}

func TestServeCmd_ListenAddressFlagCustomValue(t *testing.T) {
cmd := NewServeCmd()

customAddr := "0.0.0.0:8080"
cmd.SetArgs([]string{"--listen-address", customAddr})

if err := cmd.ParseFlags([]string{"--listen-address", customAddr}); err != nil {
t.Fatalf("Failed to parse flags: %v", err)
}

flag := cmd.Flag("listen-address")
if flag.Value.String() != customAddr {
t.Errorf("listen-address value = %v, want %v", flag.Value.String(), customAddr)
}
}
45 changes: 45 additions & 0 deletions cmd/service/install/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package install

import (
"github.com/spf13/cobra"

"github.com/anyproto/anytype-cli/core/config"
"github.com/anyproto/anytype-cli/core/output"
"github.com/anyproto/anytype-cli/core/serviceprogram"
)

func NewInstallCmd() *cobra.Command {
var listenAddress string

cmd := &cobra.Command{
Use: "install",
Short: "Install as a user service",
RunE: func(cmd *cobra.Command, args []string) error {
s, err := serviceprogram.GetServiceWithAddress(listenAddress)
if err != nil {
return output.Error("Failed to create service: %w", err)
}

err = s.Install()
if err != nil {
return output.Error("Failed to install service: %w", err)
}

output.Success("anytype service installed successfully")
if listenAddress != config.DefaultAPIAddress {
output.Info("API will listen on %s", listenAddress)
}
output.Print("\nTo manage the service:")
output.Print(" Start: anytype service start")
output.Print(" Stop: anytype service stop")
output.Print(" Restart: anytype service restart")
output.Print(" Status: anytype service status")

return nil
},
}

cmd.Flags().StringVar(&listenAddress, "listen-address", config.DefaultAPIAddress, "API listen address in `host:port` format")

return cmd
}
52 changes: 52 additions & 0 deletions cmd/service/install/install_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package install

import (
"testing"

"github.com/anyproto/anytype-cli/core/config"
)

func TestNewInstallCmd(t *testing.T) {
cmd := NewInstallCmd()

if cmd.Use != "install" {
t.Errorf("cmd.Use = %v, want install", cmd.Use)
}

if cmd.Short != "Install as a user service" {
t.Errorf("cmd.Short = %v, want 'Install as a user service'", cmd.Short)
}
}

func TestInstallCmd_ListenAddressFlag(t *testing.T) {
cmd := NewInstallCmd()

flag := cmd.Flag("listen-address")
if flag == nil {
t.Fatal("listen-address flag not found")
return
}

if flag.DefValue != config.DefaultAPIAddress {
t.Errorf("listen-address default = %v, want %v", flag.DefValue, config.DefaultAPIAddress)
}

if flag.Usage != "API listen address in `host:port` format" {
t.Errorf("listen-address usage = %v, want 'API listen address in `host:port` format'", flag.Usage)
}
}

func TestInstallCmd_ListenAddressFlagCustomValue(t *testing.T) {
cmd := NewInstallCmd()

customAddr := "0.0.0.0:9000"

if err := cmd.ParseFlags([]string{"--listen-address", customAddr}); err != nil {
t.Fatalf("Failed to parse flags: %v", err)
}

flag := cmd.Flag("listen-address")
if flag.Value.String() != customAddr {
t.Errorf("listen-address value = %v, want %v", flag.Value.String(), customAddr)
}
}
39 changes: 39 additions & 0 deletions cmd/service/restart/restart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package restart

import (
"errors"

"github.com/kardianos/service"
"github.com/spf13/cobra"

"github.com/anyproto/anytype-cli/core/output"
"github.com/anyproto/anytype-cli/core/serviceprogram"
)

func NewRestartCmd() *cobra.Command {
return &cobra.Command{
Use: "restart",
Short: "Restart the service",
RunE: func(cmd *cobra.Command, args []string) error {
s, err := serviceprogram.GetService()
if err != nil {
return output.Error("Failed to create service: %w", err)
}

_, err = s.Status()
if err != nil && errors.Is(err, service.ErrNotInstalled) {
output.Warning("anytype service is not installed")
output.Info("Run 'anytype service install' to install it first")
return nil
}

err = s.Restart()
if err != nil {
return output.Error("Failed to restart service: %w", err)
}

output.Success("anytype service restarted")
return nil
},
}
}
Loading
Loading