application lifecycle toolkit. tracks resources and tears them down in LIFO order on close. handles signal-based context cancellation for graceful shutdown.
New(opts ...Option) *App — create an app with functional optionsApp.Track(c io.Closer) error — register a resource for cleanupApp.Close() error — tear down all tracked resources in LIFO orderSignalContext(parent context.Context) (context.Context, context.CancelFunc) — context canceled on SIGINT/SIGTERMCloserFunc — adapts a func() error into an io.CloserErrClosed — returned by Track when the app has already been closedapp := zapp.New()
ctx, cancel := zapp.SignalContext(context.Background())
defer cancel()
db := openDB()
app.Track(db)
srv := startServer(ctx, db)
app.Track(zapp.CloserFunc(func() error {
return srv.Shutdown(context.Background())
}))
<-ctx.Done()
if err := app.Close(); err != nil {
slog.Error("shutdown", "err", err)
os.Exit(1)
}
generic caching with pluggable backends. provides memory, file, and redis implementations behind a common interface. all operations are thread-safe and context-aware.
Cache[K, V] — complete cache interface (Get, Set, Delete, Clear, Len, Healthy)Reader[K, V] — read-only interface (Get, Len)Writer[K, V] — write-only interface (Set, Delete, Clear)NewMemoryCache[K, V](opts ...MemoryOption) *MemoryCache — in-memory cache with optional TTLNewFileCache[K, V](opts ...Option) *FileCache — file-backed persistent cacheNewRedisCache[K, V](opts ...RedisOption) *RedisCache — redis-backed distributed cacheWithMemoryTTL[K, V](ttl time.Duration) — set expiry for memory cache entriesErrNotFound — returned when a key does not existctx := context.Background()
c := zcache.NewMemoryCache[string, int]()
c.Set(ctx, "key", 42)
value, err := c.Get(ctx, "key")
if err != nil {
// handle ErrNotFound or context.Canceled
}
existed, err := c.Delete(ctx, "key")
encryption primitives for zarlcorp privacy tools. composes proven Go stdlib and x/crypto primitives — no custom cryptography. supports AES-256-GCM, argon2id key derivation, HKDF expansion, age-compatible encryption, and secure memory erasure.
Encrypt(key, plaintext []byte) ([]byte, error) — AES-256-GCM encryption with random nonceDecrypt(key, ciphertext []byte) ([]byte, error) — AES-256-GCM decryptionDeriveKey(password, salt []byte) (key, salt []byte, error) — argon2id key derivationExpandKey(secret, salt, info []byte) ([]byte, error) — HKDF-SHA256 key expansionEncryptFile(key []byte, src io.Reader, dst io.Writer) error — encrypt a streamDecryptFile(key []byte, src io.Reader, dst io.Writer) error — decrypt a streamEncryptAge(password string, src io.Reader, dst io.Writer) error — age password encryptionDecryptAge(password string, src io.Reader, dst io.Writer) error — age password decryptionEncryptAgeKey(recipients []string, src, dst) error — age X25519 key encryptionDecryptAgeKey(identity string, src, dst) error — age X25519 key decryptionErase(b []byte) — zero out sensitive data in memoryRandBytes(n int) ([]byte, error) — cryptographic random bytesRandHex(n int) (string, error) — hex-encoded random stringkey, salt, err := zcrypto.DeriveKey([]byte("passphrase"), nil)
if err != nil {
// handle error
}
ciphertext, err := zcrypto.Encrypt(key, []byte("secret"))
if err != nil {
// handle error
}
plaintext, err := zcrypto.Decrypt(key, ciphertext)
if err != nil {
// handle error
}
defer zcrypto.Erase(key)
filesystem abstraction with OS and in-memory implementations. enables dependency injection for testing while supporting production use cases with real file operations.
ReadWriteFileFS — complete filesystem interface (read, write, remove, mkdir, open, walkdir)NewMemFS() *MemFS — in-memory filesystem for testing, thread-safeNewOSFileSystem(baseDir string) *OSFileSystem — OS-backed filesystem rooted at a base directoryReadFileFS — read-only file interfaceWriteFileFS — write-only file interfaceRemoveFS — file removal interfaceMkdirFS — directory creation interfaceOpenFileFS — file open with flags interfaceWalkDirFS — directory tree walking interface// in-memory filesystem for testing
memfs := zfilesystem.NewMemFS()
memfs.WriteFile("test.txt", []byte("data"), 0644)
data, err := memfs.ReadFile("test.txt")
// OS filesystem for production
osfs := zfilesystem.NewOSFileSystem("/path/to/data")
osfs.WriteFile("config.json", []byte("{}"), 0644)
generic functional options pattern. a single type used across all zarlcorp packages to configure structs consistently.
Option[T any] func(*T) — generic functional option typetype Config struct {
Name string
Timeout time.Duration
}
func WithName(name string) zoptions.Option[Config] {
return func(cfg *Config) {
cfg.Name = name
}
}
cfg := NewConfig(
WithName("production"),
WithTimeout(60 * time.Second),
)
catppuccin mocha palette, lipgloss style presets, and standard keybindings for TUI applications. every zarlcorp tool imports zstyle to share the same visual identity.
Base, Mantle, Crust, Surface0..2, Overlay0..2, Text — base colorsRosewater, Flamingo, Pink, Mauve, Red, Peach, Yellow, Green, Teal, Sky, Blue, Lavender — accent colorsSuccess, Error, Warning, Info — semantic color aliasesZburnAccent, ZvaultAccent, ZshieldAccent — per-tool accent colorsTitle, Subtitle, Highlight, MutedText — text stylesStatusOK, StatusErr, StatusWarn — status indicator stylesBorder, ActiveBorder — structural styles with rounded bordersLogo — zarlcorp ASCII art wordmarkStyledLogo(s lipgloss.Style) string — render logo with a given styleKeyQuit, KeyHelp, KeyUp, KeyDown, KeyEnter, KeyBack, KeyTab, KeyFilter — standard keybindingsCSSVariables — full palette as CSS custom propertiesimport "github.com/zarlcorp/core/pkg/zstyle"
fmt.Println(zstyle.Title.Render("my tool"))
fmt.Println(zstyle.StatusOK.Render("done"))
fmt.Println(zstyle.StyledLogo(
lipgloss.NewStyle().Foreground(zstyle.Lavender),
))
thread-safe concurrent data structures. provides generic map, set, and blocking FIFO queue with read-write locking for optimal performance.
NewZMap[K, V]() *ZMap — thread-safe generic map (Set, Get, Delete, Keys, Len, Clear)NewZSet[T]() *ZSet — thread-safe generic set (Add, Contains, Remove, Values, Len, Clear)Ordered[T cmp.Ordered](s *ZSet[T]) []T — sorted set values for ordered typesZSet.Ordered(compare func(a, b T) int) []T — sorted set values with custom comparatorNewZQueue[T]() *ZQueue — thread-safe blocking FIFO queueZQueue.Push(item T) error — add item to back of queueZQueue.Pop() (T, error) — blocking pop from frontZQueue.PopContext(ctx context.Context) (T, error) — blocking pop with context cancellationZQueue.TryPop() (T, error) — non-blocking popZQueue.Close() error — signal shutdown to waiting consumersErrQueueClosed, ErrQueueEmpty, ErrCanceled — sentinel errorsm := zsync.NewZMap[string, int]()
m.Set("key", 42)
value, ok := m.Get("key")
s := zsync.NewZSet[string]()
s.Add("value")
if s.Contains("value") {
s.Remove("value")
}
q := zsync.NewZQueue[string]()
q.Push("item")
item, err := q.Pop() // blocks until item available
q.Close() // wake waiting consumers