app

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 31, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package app holds the use-case orchestrators the tyche CLI runs. Every function in this package is pure (no CLI types, no Kong flags, no Cobra commands) and takes plain Go values, so it can be called from the CLI, from a future GUI, or from a programmatic embedding of tyche in another tool. The CLI layer in internal/cli is a thin adapter that parses arguments and calls into here.

Keeping this layer separate from internal/cli has two payoffs:

  1. The servergen and clientgen libraries never need to know about CLI types, so importing them does not pull Kong into a user's binary.
  2. Tests for the business logic do not need a CLI framework — they call app.X(...) directly with a tempdir.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoConfig = config.ErrNoConfig

ErrNoConfig is re-exported from the config package so CLI handlers can distinguish "no config file found" from real load failures without importing internal/config directly.

Functions

func Cleanup

func Cleanup(root string, patterns []string) error

Cleanup removes previously generated codec files from root for the given patterns. It is the implementation behind `tyche clean`.

func Generate

func Generate(root string, patterns []string) error

Generate writes typed route codecs into the working tree at root for every package pattern. Patterns default to ["./..."]. It is the implementation behind `tyche generate` and the prefetch step of `tyche build|run|test`.

func LoadConfig

func LoadConfig(opts LoadOptions) (*config.LoadResult, error)

LoadConfig loads the tyche.json file using the same precedence as the CLI: explicit path > env var > discovery. The optional InfoCallback receives informational messages ("using config ...", " <README line>") so the CLI layer can route them through its Printer. If no callback is set, the messages are dropped — useful for tests and quiet mode. When no config is discovered, LoadConfig returns (nil, config.ErrNoConfig).

func ResolvePath

func ResolvePath(rootDir, path string) string

ResolvePath joins path to rootDir unless path is absolute.

func ResolveRoot

func ResolveRoot(root string) (string, error)

ResolveRoot returns the directory to operate in: the explicit --root if non-empty, otherwise the current working directory. It is the single place that decides "where is the project".

func ResolveServerPatterns

func ResolveServerPatterns(root, configPath, envConfig string, fallback []string) (patterns, ignore []string)

ResolveServerPatterns returns the package patterns and ignore globs for server codegen. It honours tyche.json's server block using the same config precedence as the rest of the CLI (explicit --config > env > discovery from root) and falls back to the supplied patterns (default ./...) when no config or server block is present.

func Scaffold

func Scaffold(opts ScaffoldOptions) (string, error)

Scaffold writes a starter tyche.json at Root/tyche.json. The file is round-tripped through config.Load so a malformed scaffold is caught immediately and removed. The path of the written file is returned.

func WithWorktree

func WithWorktree(opts WorktreeOptions) error

WithWorktree runs a go subcommand against a temporary copy of the project with fresh codecs generated in place, so the user's working tree is never touched by generated code. It is the implementation behind build/run/test.

Types

type ClientOptions

type ClientOptions struct {
	SpecPath   string
	OutDir     string
	Module     string
	Package    string
	GoVersion  string
	ClientName string
	TypeNaming string // "structural" or "operation-scoped"
	ConfigPath string // for spec path resolution
}

ClientOptions configures RegenerateClient. The CLI maps --spec, --out, --module, --package, --go, --client-name, and --type-naming onto these fields; values resolved from tyche.json fill any zero field.

type ClientResult

type ClientResult struct {
	OutDir    string `json:"out_dir"`
	FileCount int    `json:"file_count"`
}

ClientResult is what `tyche client` returns on success: the output dir and the file count.

func RegenerateClient

func RegenerateClient(opts ClientOptions) (*ClientResult, error)

RegenerateClient reads the OpenAPI spec at opts.SpecPath, runs clientgen, and writes the result to opts.OutDir. spec paths in tyche.json are resolved relative to the config file's directory.

type ConfigClient

type ConfigClient struct {
	Out        string `json:"out,omitempty"`
	Module     string `json:"module,omitempty"`
	Package    string `json:"package,omitempty"`
	Go         string `json:"go,omitempty"`
	ClientName string `json:"client_name,omitempty"`
	TypeNaming string `json:"type_naming,omitempty"`
}

ConfigClient mirrors config.ClientBlock for `config show` output.

type ConfigServer

type ConfigServer struct {
	Patterns []string `json:"patterns,omitempty"`
	Ignore   []string `json:"ignore,omitempty"`
}

ConfigServer mirrors config.ServerBlock for `config show` output.

type ConfigShowResult

type ConfigShowResult struct {
	Client  *ConfigClient `json:"client,omitempty"`
	Path    string        `json:"path,omitempty"`
	Server  *ConfigServer `json:"server,omitempty"`
	Spec    string        `json:"spec,omitempty"`
	Version int           `json:"version"`
}

ConfigShowResult is the rendered shape of `tyche config show`. The CLI layer formats this through its Printer; --json emits it as JSON. The nested blocks are typed (not map[string]any) so the JSON schema is statically guaranteed and a typo can't silently drop a field.

func ShowConfig

func ShowConfig(opts LoadOptions) (*ConfigShowResult, error)

ShowConfig resolves tyche.json and returns a printable representation. Returns (nil, config.ErrNoConfig) when no config file was found — the CLI decides how to phrase that to the user.

type LoadOptions

type LoadOptions struct {
	InfoCallback func(string)
	Root         string
	ConfigPath   string
	EnvConfig    string
	PrintInfo    bool // whether to emit the "using config ..." banner
}

LoadOptions is the cross-cutting input to most app functions. It captures the CLI's discovery state so the app layer does not need to know how the caller chose to discover the project root.

type ScaffoldOptions

type ScaffoldOptions struct {
	Root       string
	Module     string
	Spec       string
	TypeNaming string
	Force      bool
}

ScaffoldOptions configures Scaffold. Module, Spec, and TypeNaming become the values written into tyche.json.

type WorktreeOptions

type WorktreeOptions struct {
	Root       string
	ConfigPath string // honours --config for server pattern/ignore resolution
	EnvConfig  string // honours TYCHE_CONFIG for the same
	Patterns   []string
	GoArgs     []string // e.g. ["build", "-o", "./bin/api", "./cmd/api"]
}

WorktreeOptions configures WithWorktree. The function is the implementation behind `tyche build|run|test`: it copies the project into a tmpdir, regenerates codecs there, runs the go subcommand, and cleans up.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL