Fork channel

Create a new channel as a copy of main.

Rename channel

Rename main to:

Delete channel

Delete main? This cannot be undone.

common.go
package cmd

import (
	"flag"
	"fmt"
	"os"
	"sort"

	"skraak/db"
)

// initEventLog configures transaction event logging for the given database path.
// Returns a cleanup function that should be deferred by the caller.
func initEventLog(dbPath string) func() {
	db.SetEventLogConfig(db.EventLogConfig{
		Enabled: true,
		Path:    dbPath + ".events.jsonl",
	})
	return func() {
		if err := db.CloseEventLog(); err != nil {
			fmt.Fprintf(os.Stderr, "Warning: failed to close event log: %v\n", err)
		}
	}
}

// requireFlags reports an error listing any required flags whose values are
// still at their zero value. Strings must be non-empty; ints must be non-zero.
// Map iteration is randomised, so the returned list is sorted for stability.
func requireFlags(fs *flag.FlagSet, required map[string]any) error {
	var missing []string
	for name, v := range required {
		switch x := v.(type) {
		case string:
			if x == "" {
				missing = append(missing, name)
			}
		case int:
			if x == 0 {
				missing = append(missing, name)
			}
		default:
			return fmt.Errorf("requireFlags: unsupported type for %s: %T", name, v)
		}
	}
	if len(missing) > 0 {
		sort.Strings(missing)
		fs.Usage()
		return fmt.Errorf("missing required flags: %v", missing)
	}
	return nil
}

// usagePrinter returns an fs.Usage closure that prints a structured header,
// the auto-generated flag table (via fs.PrintDefaults), and optional examples.
// The flag table reflects the flags as registered — no manual duplication.
func usagePrinter(fs *flag.FlagSet, synopsis, description string, examples ...string) func() {
	return func() {
		fmt.Fprintf(os.Stderr, "Usage: %s\n\n", synopsis)
		if description != "" {
			fmt.Fprintf(os.Stderr, "%s\n\n", description)
		}
		fmt.Fprintf(os.Stderr, "Options:\n")
		fs.PrintDefaults()
		if len(examples) > 0 {
			fmt.Fprintf(os.Stderr, "\nExamples:\n")
			for _, ex := range examples {
				fmt.Fprintf(os.Stderr, "  %s\n", ex)
			}
		}
	}
}

// mustValue returns the value for a flag that requires an argument, advancing i by 2.
// Exits the program if the value is missing (unrecoverable CLI error).
func mustValue(args []string, i *int, flag string) string {
	if *i+1 >= len(args) {
		fmt.Fprintf(os.Stderr, "Error: %s requires a value\n", flag)
		os.Exit(1)
	}
	v := args[*i+1]
	*i += 2
	return v
}