<commit_before>package daemon

import (
	"context"
	"fmt"
	"os"
	"os/exec"
	"strings"

	"github.com/makii42/gottaw/output"
)

// Daemon is the interface to a background process that is
// started after a successful pipeline execution
type Daemon interface {
	// Start is the trigger to kick of the daemon. Returns an
	// error if the Start fails or if it is running already
	Start() error
	// Stop is the trigger to stop the daemon. Returns an
	// error if the Stop fails or if nothing is running already
	Stop() error
}

type daemon struct {
	l       *output.Logger
	cmdName string
	args    []string
	cmd     *exec.Cmd
	ctx     context.Context
	cancel  context.CancelFunc
}

// NewDaemon creates a new Daemon that can be started and
// stopped using the interface methods.
func NewDaemon(l *output.Logger, cmdStr string) Daemon {
	elements := strings.Split(cmdStr, " ")
	command, elements := elements[0], elements[1:]
	return &daemon{
		cmdName: command,
		args:    elements,
		cmd:     nil,
	}
}

func (d *daemon) Start() error {
	if d.cmd != nil || d.ctx != nil || d.cancel != nil {
		return fmt.Errorf("command already present")
	}
	d.ctx, d.cancel = context.WithCancel(context.Background())
	d.cmd = exec.CommandContext(d.ctx, d.cmdName, d.args...)
	d.cmd.Stdout = os.Stdout
	d.cmd.Stderr = os.Stderr

	if err := d.cmd.Start(); err != nil {
		return err
	}
	return nil
}

func (d *daemon) Stop() error {
	if d.cmd == nil || d.ctx == nil || d.cancel == nil {
		return fmt.Errorf("no command to stop")
	}
	d.cancel()
	d.cmd, d.ctx, d.cancel = nil, nil, nil
	return nil
}<commit_msg>this is unfortunate, but probably no error.<commit_after>package daemon

import (
	"context"
	"fmt"
	"os"
	"os/exec"
	"strings"

	"github.com/makii42/gottaw/output"
)

// Daemon is the interface to a background process that is
// started after a successful pipeline execution
type Daemon interface {
	// Start is the trigger to kick of the daemon. Returns an
	// error if the Start fails or if it is running already
	Start() error
	// Stop is the trigger to stop the daemon. Returns an
	// error if the Stop fails or if nothing is running already
	Stop() error
}

type daemon struct {
	l       *output.Logger
	cmdName string
	args    []string
	cmd     *exec.Cmd
	ctx     context.Context
	cancel  context.CancelFunc
}

// NewDaemon creates a new Daemon that can be started and
// stopped using the interface methods.
func NewDaemon(l *output.Logger, cmdStr string) Daemon {
	elements := strings.Split(cmdStr, " ")
	command, elements := elements[0], elements[1:]
	return &daemon{
		cmdName: command,
		args:    elements,
		cmd:     nil,
	}
}

func (d *daemon) Start() error {
	if d.cmd != nil || d.ctx != nil || d.cancel != nil {
		return fmt.Errorf("command already present")
	}
	d.ctx, d.cancel = context.WithCancel(context.Background())
	d.cmd = exec.CommandContext(d.ctx, d.cmdName, d.args...)
	d.cmd.Stdout = os.Stdout
	d.cmd.Stderr = os.Stderr

	if err := d.cmd.Start(); err != nil {
		return err
	}
	return nil
}

func (d *daemon) Stop() error {
	if d.cmd == nil || d.ctx == nil || d.cancel == nil {
		return nil
	}
	d.cancel()
	d.cmd, d.ctx, d.cancel = nil, nil, nil
	return nil
}
{'fraction_non_alphanumeric': 0.12330495111952065, 'fraction_numerical': 0.002522863450015768, 'mean_word_length': 3.1846965699208445, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5168831', 'n_tokens_mistral': 1100, 'n_tokens_neox': 1076, 'n_words': 377}