Files

126 lines
4.4 KiB
Go
Raw Permalink Normal View History

// Package core defines the shared Application model that the rest of the
// yetano.dev/yetano/core modules (paths, config, and future ones) build on:
// what an application is called, what kind of program it is, and in what
// scope it is installed/running. See paths.For and config.For for how this
// model turns into concrete filesystem locations.
package core
import (
"fmt"
"strings"
)
// ApplicationKind describes what shape a Yetano application takes. It
// controls platform default behavior (most visibly on macOS and Windows,
// see the paths package), never installation privilege -- that is Scope's
// job.
type ApplicationKind int
const (
// CLI is a command-line/terminal application. On macOS it prefers the
// same XDG-like layout as Linux, for consistent cross-platform admin.
CLI ApplicationKind = iota
// Desktop is a native GUI application. On macOS and Windows it prefers
// native per-OS directory conventions (Library/Application Support,
// %APPDATA%, ...) over the Unix-y XDG layout.
Desktop
// Service is a long-running background process (daemon/agent). Its
// directory layout follows the same convention as CLI; what makes a
// service a service (install as a launchd/systemd/Windows service
// unit) is outside this library's scope.
Service
)
// String returns the lowercase name used in Explain output and error
// messages.
func (k ApplicationKind) String() string {
switch k {
case CLI:
return "cli"
case Desktop:
return "desktop"
case Service:
return "service"
default:
return fmt.Sprintf("ApplicationKind(%d)", int(k))
}
}
// Scope describes the installation/execution context an Application runs
// in -- independent of ApplicationKind. The same Application (e.g. a
// Service) can run as UserScope or SystemScope; both are legitimate and
// this library resolves different directories for each (see paths).
//
// This is deliberately not "UserService"/"SystemService": Scope applies
// equally to CLI, Desktop, and Service kinds.
type Scope int
const (
// UserScope resolves per-user directories (the zero value, and the
// default for "yet install foo").
UserScope Scope = iota
// SystemScope resolves system-wide directories (e.g. "yet install
// --system foo"), following each platform's system location
// convention (/etc, /var/lib, %PROGRAMDATA%, /Library, ...).
SystemScope
)
// String returns the lowercase name used in Explain output and error
// messages.
func (s Scope) String() string {
switch s {
case UserScope:
return "user"
case SystemScope:
return "system"
default:
return fmt.Sprintf("Scope(%d)", int(s))
}
}
// Application identifies a Yetano application to the paths and config
// resolvers. Name becomes both the directory name under each resolved
// base directory (e.g. ~/.config/<Name>) and, unless overridden, the
// basis for the application's environment variable prefix (see
// paths.WithEnvPrefix).
type Application struct {
// Name is the application's short, stable identifier: e.g. "yet",
// "yasta". It must not be empty and must not contain path separators,
// "..", or whitespace -- see Validate.
Name string
// Kind is the application's shape. The zero value is CLI.
Kind ApplicationKind
// Scope is the installation/execution context. The zero value is
// UserScope.
Scope Scope
}
// Validate reports whether a is safe to turn into filesystem paths and
// environment variable names. core intentionally does not keep a fixed
// list of known Yetano application names (see AGENTS.md); any name that
// passes Validate is accepted.
func (a Application) Validate() error {
if a.Name == "" {
return fmt.Errorf("core: application name must not be empty")
}
if strings.ContainsAny(a.Name, "/\\") {
return fmt.Errorf("core: application name %q must not contain path separators", a.Name)
}
if a.Name == "." || a.Name == ".." {
return fmt.Errorf("core: application name %q is not a valid directory name", a.Name)
}
if strings.TrimSpace(a.Name) != a.Name {
return fmt.Errorf("core: application name %q must not have leading/trailing whitespace", a.Name)
}
return nil
}
// EnvPrefix derives the default application-specific environment variable
// prefix from a.Name: upper-cased, with any '-' replaced by '_' (e.g.
// "yet" -> "YET", "yapl-oci" -> "YAPL_OCI"). paths.WithEnvPrefix overrides
// this; paths.WithoutAppEnv disables it entirely.
func (a Application) EnvPrefix() string {
return strings.ToUpper(strings.ReplaceAll(a.Name, "-", "_"))
}