~ianmjones/snippetpixie

8acfeae9116edab9f7b78216e2d57c32993eb54e — Ian M. Jones 18 days ago 4190561 trunk
Add start flag to CLI

Implements: https://todo.sr.ht/~ianmjones/snippetpixie/4
3 files changed, 69 insertions(+), 2 deletions(-)

M cmd/snippetpixie/config.go
M cmd/snippetpixie/config_test.go
M cmd/snippetpixie/main.go
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 startFlag bool
	startUsage := "start the daemon"
	flags.BoolVar(&startFlag, "start", false, startUsage)

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


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

	case startFlag:
		c.Cmd = "Start"

	case stopFlag:
		c.Cmd = "Stop"


M cmd/snippetpixie/config_test.go => cmd/snippetpixie/config_test.go +5 -0
@@ 24,9 24,14 @@ func TestConfig_Init(t *testing.T) {
	}{
		{[]string{"snippetpixie", "--help"}, "", nil, "Usage: snippetpixie [COMMAND] [FLAGS]..."},
		{[]string{"snippetpixie", "-h"}, "", nil, "Usage: snippetpixie [COMMAND] [FLAGS]..."},
		{[]string{"snippetpixie", "--start"}, "Start", nil, ""},
		{[]string{"snippetpixie", "--start", "--version"}, "Version", nil, ""},
		{[]string{"snippetpixie", "--start", "--status"}, "Status", nil, ""},
		{[]string{"snippetpixie", "--start", "--stop"}, "Start", nil, ""},
		{[]string{"snippetpixie", "--stop"}, "Stop", nil, ""},
		{[]string{"snippetpixie", "--stop", "--version"}, "Version", nil, ""},
		{[]string{"snippetpixie", "--stop", "--status"}, "Status", nil, ""},
		{[]string{"snippetpixie", "--stop", "--start"}, "Start", 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 +57 -2
@@ 10,6 10,7 @@ import (
	"io"
	"log"
	"os"
	"os/exec"
	"os/signal"
)



@@ 77,6 78,8 @@ func run(ctx context.Context, c *config, out io.Writer) error {
		}

	case "Ping":
		assertDaemonRunning(serv, out)

		response, err := serv.Ping(c.Args["response"].(string))
		if err != nil {
			return err


@@ 88,11 91,30 @@ func run(ctx context.Context, c *config, out io.Writer) error {
		}

	case "Status":
		response, err := serv.Ping("")
		if err != nil || response != "pong" {
		if !daemonRunning(serv) {
			os.Exit(1)
		}

	case "Start":
		path, err := exec.LookPath("snippetpixied")
		if err != nil {
			_, err = fmt.Fprintln(out, "Could not find snippetpixied, is it installed?")
			os.Exit(1)
		}

		if daemonRunning(serv) {
			_, err = fmt.Fprintln(out, "The daemon is already running.")
			os.Exit(0)
		}

		cmd := exec.Command("nohup", path)
		err = cmd.Start()
		if err != nil {
			_, err = fmt.Fprintln(out, "There was a problem starting the daemon:", err)
			os.Exit(1)
		}
		_ = cmd.Process.Release()

	case "Stop":
		err := serv.Stop()
		if err != nil {


@@ 100,6 122,8 @@ func run(ctx context.Context, c *config, out io.Writer) error {
		}

	case "Export":
		assertDaemonRunning(serv, out)

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


@@ 118,6 142,8 @@ func run(ctx context.Context, c *config, out io.Writer) error {
		}

	case "Import":
		assertDaemonRunning(serv, out)

		_, _, snippets, err := export.ReadFile(c.Args["filename"].(string))
		if err != nil {
			return err


@@ 145,12 171,16 @@ func run(ctx context.Context, c *config, out io.Writer) error {
		_, err = fmt.Fprintln(out, "Deleted:", stats.Deleted)

	case "AddSnippet":
		assertDaemonRunning(serv, out)

		_, err := serv.AddSnippet(c.Args["abbreviation"].(string), c.Args["body"].(string))
		if err != nil {
			return err
		}

	case "GetSnippets":
		assertDaemonRunning(serv, out)

		snippets, err := serv.GetSnippets(c.Args["search"].(string), c.Args["recent"].(bool))
		if err != nil {
			return err


@@ 161,18 191,24 @@ func run(ctx context.Context, c *config, out io.Writer) error {
		}

	case "RemoveSnippet":
		assertDaemonRunning(serv, out)

		_, err := serv.RemoveSnippet(c.Args["abbreviation"].(string))
		if err != nil {
			return err
		}

	case "Copy":
		assertDaemonRunning(serv, out)

		err := serv.CopySnippet(c.Args["abbreviation"].(string), c.Args["skipLastUsed"].(bool))
		if err != nil {
			return err
		}

	case "ExpandAbbreviation":
		assertDaemonRunning(serv, out)

		body, _, err := serv.ExpandAbbreviation(c.Args["abbreviation"].(string))
		if err != nil {
			return err


@@ 187,3 223,22 @@ func run(ctx context.Context, c *config, out io.Writer) error {

	return nil
}

// assertDaemonRunning checks whether the daemon is contactable.
// If it is not, then a message is displayed and the program exits.
func assertDaemonRunning(serv dbusservice.DBusService, out io.Writer) {
	if !daemonRunning(serv) {
		_, _ = fmt.Fprintln(out, "The daemon is not running.")
		os.Exit(0)
	}
}

// daemonRunning checks whether the daemon is contactable.
func daemonRunning(serv dbusservice.DBusService) bool {
	response, err := serv.Ping("")
	if err == nil && response == "pong" {
		return true
	}

	return false
}