cmd/tilstream/cmd/root.go

// Package cmd hosts the cobra subcommands that together make up the
// tilstream binary. main() calls Execute() and that's it. See
// mercemay.top/src/tilstream/ for each subcommand's behaviour.
package cmd

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"
)

// Globals shared across subcommands.
var (
	cfgFile    string
	verbose    bool
	colorFlag  string
	rootCmd    = &cobra.Command{
		Use:   "tilstream",
		Short: "Static site generator for Today-I-Learned notes",
		Long: "tilstream walks a directory of markdown notes and produces a " +
			"static HTML site plus RSS, Atom, JSON Feed, and a lunr-compatible " +
			"search index.",
		SilenceUsage: true,
	}
)

// Execute is called from main.
func Execute() {
	if err := rootCmd.Execute(); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

func init() {
	rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "tilstream.yaml",
		"path to config file")
	rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false,
		"verbose logging")
	rootCmd.PersistentFlags().StringVar(&colorFlag, "color", "auto",
		"color output: auto|always|never")

	rootCmd.AddCommand(buildCmd)
	rootCmd.AddCommand(serveCmd)
	rootCmd.AddCommand(initCmd)
	rootCmd.AddCommand(newCmd)
	rootCmd.AddCommand(deployCmd)
	rootCmd.AddCommand(doctorCmd)
	rootCmd.AddCommand(versionCmd)
}

// Verbose returns the current verbose flag value. Subcommands import this
// instead of wiring cobra args through every call site.
func Verbose() bool { return verbose }

// Config returns the currently configured config path.
func Config() string { return cfgFile }

// SetStderr lets tests capture error output. Cobra exposes SetErr on
// commands directly; this is a convenience wrapper for callers that
// already held a *cobra.Command reference to the root.
func SetStderr(c *cobra.Command) { rootCmd.SetErr(c.ErrOrStderr()) }

// PrintBanner writes the tilstream banner to stdout when not in quiet mode.
// Subcommands can call this at the start of long-running commands.
func PrintBanner() {
	if verbose {
		fmt.Println("tilstream - static site generator for TIL notes")
		fmt.Println("docs: mercemay.top/src/tilstream/")
	}
}

// RootCmd returns the root command. Tests and embedding callers use this
// to wire additional commands without touching the package-level var.
func RootCmd() *cobra.Command { return rootCmd }