~ianmjones/snippetpixie

002987c5f0fe7d3c499bee17308fc82e5719def2 — Ian M. Jones 18 days ago 3a4d8a7
Add CLI command to stop the daemon

Implements: https://todo.sr.ht/~ianmjones/snippetpixie/5
M cmd/snippetpixie/config.go => cmd/snippetpixie/config.go +7 -0
@@ 69,6 69,10 @@ func (c *config) initDefaultFlagset(appName string, args []string, out io.Writer
	statusUsage := "get status of the daemon, exits with status 0 if running, 1 if not"
	flags.BoolVar(&statusFlag, "status", false, statusUsage)

	var stopFlag bool
	stopUsage := "stop the daemon"
	flags.BoolVar(&stopFlag, "stop", false, stopUsage)

	var versionFlag bool
	versionUsage := "display version number"
	flags.BoolVar(&versionFlag, "version", false, versionUsage)


@@ 93,6 97,9 @@ func (c *config) initDefaultFlagset(appName string, args []string, out io.Writer
	case statusFlag:
		c.Cmd = "Status"

	case stopFlag:
		c.Cmd = "Stop"

	case flags.Changed("export"):
		exportFile = strings.TrimSpace(exportFile)


M cmd/snippetpixie/config_test.go => cmd/snippetpixie/config_test.go +3 -0
@@ 24,6 24,9 @@ func TestConfig_Init(t *testing.T) {
	}{
		{[]string{"snippetpixie", "--help"}, "", nil, "Usage: snippetpixie [COMMAND] [FLAGS]..."},
		{[]string{"snippetpixie", "-h"}, "", nil, "Usage: snippetpixie [COMMAND] [FLAGS]..."},
		{[]string{"snippetpixie", "--stop"}, "Stop", nil, ""},
		{[]string{"snippetpixie", "--stop", "--version"}, "Version", nil, ""},
		{[]string{"snippetpixie", "--stop", "--status"}, "Status", nil, ""},
		{[]string{"snippetpixie", "--status"}, "Status", nil, ""},
		{[]string{"snippetpixie", "--version"}, "Version", nil, ""},
		{[]string{"snippetpixie", "--version", "--status"}, "Version", nil, ""},

M cmd/snippetpixie/main.go => cmd/snippetpixie/main.go +6 -0
@@ 93,6 93,12 @@ func run(ctx context.Context, c *config, out io.Writer) error {
			os.Exit(1)
		}

	case "Stop":
		err := serv.Stop()
		if err != nil {
			os.Exit(1)
		}

	case "Export":
		snippets, err := serv.GetSnippets("", false)
		if err != nil {

M cmd/snippetpixied/dbus.go => cmd/snippetpixied/dbus.go +9 -0
@@ 7,6 7,7 @@ import (
	"github.com/godbus/dbus/v5"
	"github.com/gofrs/uuid"
	"log"
	"os"
	"time"

	"git.sr.ht/~ianmjones/snippetpixie/internal/manager"


@@ 30,6 31,14 @@ func (s *service) Ping(response string) (string, *dbus.Error) {
	return "pong", nil
}

// Stop daemon.
func (s *service) Stop() *dbus.Error {
	log.Printf("Got stop message, exiting.")
	os.Exit(0)

	return nil
}

// AddSnippet creates a new Snippet with given abbreviation and body.
func (s *service) AddSnippet(abbreviation string, body string) (*dbusservice.Snippet, *dbus.Error) {
	theSnippet, err := s.snippets.GetSnippetWithAbbreviation(abbreviation)

M internal/service/service.go => internal/service/service.go +10 -0
@@ 48,6 48,16 @@ func (service *Service) Ping(expected string) (string, error) {
	return response, nil
}

// Stop daemon.
func (service *Service) Stop() error {
	call := service.Call(dbusservice.BaseDest+".Stop", 0)
	if call.Err != nil {
		return call.Err
	}

	return nil
}

// AddSnippet adds a Snippet via the dbus service.
func (service *Service) AddSnippet(abbreviation string, body string) (*snippet.Snippet, error) {
	dbusSnippet := &dbusservice.Snippet{}

M pkg/dbusservice/dbusservice.go => pkg/dbusservice/dbusservice.go +3 -0
@@ 32,6 32,9 @@ type DBusService interface {
	// Ping daemon for expected response.
	Ping(expected string) (string, error)

	// Stop daemon.
	Stop() error

	// AddSnippet to database.
	AddSnippet(abbreviation string, body string) (*snippet.Snippet, error)