shared

package
v0.1.159 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: MIT Imports: 20 Imported by: 6

Documentation

Overview

Package shared collects small, dependency-free utilities used across the codefly stack: case conversions, file & path helpers, the Must pattern, generics helpers, and concurrency-safe writers used in tests and runners.

Nothing in here imports other codefly packages — it sits at the bottom of the import graph so any package can depend on it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckDirectoryOrCreate

func CheckDirectoryOrCreate(ctx context.Context, dir string) (bool, error)

CheckDirectoryOrCreate checks if a directory exists or create it if it doesn't, with default 0o755 perms. Use for project / config / build artifacts where world-readable is fine.

bool: created
err:  only for unexpected behavior

func CheckDirectoryOrCreateSecure added in v0.1.157

func CheckDirectoryOrCreateSecure(ctx context.Context, dir string) (bool, error)

CheckDirectoryOrCreateSecure is the 0o700 variant. Use for any directory holding tokens, signing keys, or per-user state under $HOME/.codefly. Same return semantics as CheckDirectoryOrCreate.

On macOS / Linux 0o700 means owner-only read+write+execute. Other users (and processes running under different uids) cannot list or traverse the directory — important when codefly stores Vault tokens or refresh-token signing keys on disk.

func CheckEmptyDirectory added in v0.1.84

func CheckEmptyDirectory(ctx context.Context, dir string) (bool, error)

CheckEmptyDirectory checks if a directory exists and is empty

func CheckEmptyDirectoryOrCreate

func CheckEmptyDirectoryOrCreate(ctx context.Context, dir string) (bool, error)

CheckEmptyDirectoryOrCreate checks if a directory exists and is empty bool if created err only for unexpected behavior or if exists

func CopyFile

func CopyFile(_ context.Context, from string, to string) error

func CreateFile added in v0.1.103

func CreateFile(ctx context.Context, path string) error

Create is equivalent of "touch"

func DefaultTo added in v0.0.51

func DefaultTo(s string, defaultValue string) string

func DeleteFile added in v0.1.1

func DeleteFile(ctx context.Context, file string) error

func DirectoryExists added in v0.0.13

func DirectoryExists(ctx context.Context, dir string) (bool, error)

DirectoryExists checks for existence of folder err only for unexpected behavior

func EmptyDir added in v0.1.20

func EmptyDir(ctx context.Context, dir string) error

EmptyDir delete the content of a directory

func Exists added in v0.1.89

func Exists(ctx context.Context, p string) (bool, error)

Exists checks for existence of file/folder err only for unexpected behavior

func FileExists

func FileExists(ctx context.Context, file string) (bool, error)

FileExists checks for existence of folder err only for unexpected behavior

func GenerateTree

func GenerateTree(p, indent string) (string, error)

GenerateTree recursively generates a string representation of the directory tree

func Must added in v0.0.42

func Must[T any](t T, err error) T

Must collapses an `(T, error)` return into `T`, panicking on error.

USE SPARINGLY. Appropriate at:

  • init() / package-level globals where failure means the program can't start (e.g. embed.FS parsing, regexp.MustCompile-style).
  • test code, where panic == test failure.

DO NOT use in request-path or runtime code — wrap with wool.Wrapf and propagate the error instead. A panic in an agent's gRPC handler is harder to debug than a clean error return, and `defer Wool.Catch()` only converts panics to opaque internal-server-errors.

func MustSolvePath added in v0.1.84

func MustSolvePath(p string) string

MustSolvePath panics on path-resolution error. Test-only helper — runtime code (agents, CLI commands, anything that handles user input or env-dependent paths) should call SolvePath and propagate the error via wool.Wrapf instead.

func Pointer added in v0.0.51

func Pointer[T any](t T) *T

func PointerEqual added in v0.0.51

func PointerEqual[T comparable](t *T, target T) bool

PointerEqual returns true if pointer is not nil and value is equal

func ProtoType added in v0.0.51

func ProtoType(m proto.Message) string

func RenameDir added in v0.1.103

func RenameDir(ctx context.Context, from string, to string) error

RenameDir renames a directory

func Retry added in v0.1.127

func Retry(interval time.Duration, maxAttempts int, task func() error) error

Retry attempts to execute a task function until it succeeds or reaches a maximum number of attempts.

func ShortLowerUUID added in v0.1.53

func ShortLowerUUID() (string, error)

ShortLowerUUID returns a UUID of 10 characters in base26 (lowercase letters only)

func SolvePath added in v0.1.22

func SolvePath(p string) (string, error)

func ToCamelCase added in v0.0.51

func ToCamelCase(s string) string

ToCamelCase converts a string to camelCase

func ToDNSCase added in v0.0.51

func ToDNSCase(s string) string

func ToDotSeparatedCase added in v0.1.31

func ToDotSeparatedCase(s string) string

func ToKebabCase added in v0.0.51

func ToKebabCase(str string) string

ToKebabCase converts a string to kebab-case

func ToLowerCase added in v0.0.51

func ToLowerCase(s string) string

func ToSnakeCase added in v0.0.51

func ToSnakeCase(s string) string

ToSnakeCase converts a string to snake_case

func ToTitle added in v0.0.51

func ToTitle(s string) string

func TypeOf added in v0.0.51

func TypeOf[T any]() string

func WithOverride added in v0.0.51

func WithOverride(ctx context.Context, override Override) context.Context

func WriteFileSecure added in v0.1.157

func WriteFileSecure(ctx context.Context, path string, data []byte) error

WriteFileSecure writes data to path with 0o600 perms (owner read+write only). Use for secrets — refresh token signing keys, Vault tokens, dev fixture passwords, etc. The parent directory gets 0o700 if it has to be created.

Atomic via tmp-file + rename so a crash mid-write doesn't leave a truncated secret on disk.

Types

type Case added in v0.0.51

type Case struct {
	LowerCase    string
	SnakeCase    string
	CamelCase    string
	KebabCase    string
	DNSCase      string
	Title        string
	DotSeparated string
}

func ToCase added in v0.0.51

func ToCase(s string) Case

type ContextOverrideKey added in v0.0.51

type ContextOverrideKey string
const (
	OverrideKey ContextOverrideKey = "override"
)

type ContextPathSelectKey added in v0.1.20

type ContextPathSelectKey string
const (
	PathSelectKey ContextPathSelectKey = "path-select"
)

type CopyInstruction

type CopyInstruction struct {
	Name string
	Path string
}

type DirReader added in v0.0.13

type DirReader struct {
	// contains filtered or unexported fields
}

func NewDirReader added in v0.0.13

func NewDirReader() *DirReader

func (*DirReader) At added in v0.0.13

func (dr *DirReader) At(root string) *DirReader

func (*DirReader) ReadDir added in v0.0.13

func (dr *DirReader) ReadDir(relativePath string) ([]os.DirEntry, error)

func (*DirReader) ReadFile added in v0.0.13

func (dr *DirReader) ReadFile(relativePath string) ([]byte, error)

type ErrorResourceNotFound added in v0.1.89

type ErrorResourceNotFound struct {
	// contains filtered or unexported fields
}

func NewErrorResourceNotFound added in v0.1.89

func NewErrorResourceNotFound(resourceType string, resource string) *ErrorResourceNotFound

func (ErrorResourceNotFound) Error added in v0.1.89

func (e ErrorResourceNotFound) Error() string

type FSReader added in v0.0.13

type FSReader struct {
	FS fs.FS
	// contains filtered or unexported fields
}

func Embed added in v0.0.13

func Embed(fsys fs.FS) *FSReader

func (*FSReader) At added in v0.0.13

func (fr *FSReader) At(root string) *FSReader

func (*FSReader) Copy added in v0.0.13

func (fr *FSReader) Copy(relativePath string, destination string) error

func (*FSReader) ReadDir added in v0.0.13

func (fr *FSReader) ReadDir(relativePath string) ([]os.DirEntry, error)

func (*FSReader) ReadFile added in v0.0.13

func (fr *FSReader) ReadFile(relativePath string) ([]byte, error)

type FileSystem added in v0.0.13

type FileSystem interface {
	ReadDir(relativePath string) ([]os.DirEntry, error)
	ReadFile(relativePath string) ([]byte, error)
}

type IgnoreNoneHandler added in v0.0.51

type IgnoreNoneHandler struct{}

func (*IgnoreNoneHandler) Keep added in v0.1.20

func (i *IgnoreNoneHandler) Keep(string) bool

type IgnorePatterns added in v0.0.51

type IgnorePatterns struct {
	// contains filtered or unexported fields
}

func NewIgnore added in v0.0.51

func NewIgnore(patterns ...string) *IgnorePatterns

func (*IgnorePatterns) Keep added in v0.1.20

func (ign *IgnorePatterns) Keep(file string) bool

type Override added in v0.0.13

type Override interface {
	Replace(p string) bool
}

func GetOverride added in v0.0.51

func GetOverride(ctx context.Context) Override

func OverrideAll added in v0.1.18

func OverrideAll() Override

OverrideAll overrides all paths

type OverrideAllHandler added in v0.1.18

type OverrideAllHandler struct{}

func (*OverrideAllHandler) Replace added in v0.1.18

func (*OverrideAllHandler) Replace(string) bool

type OverrideExceptionHandler added in v0.1.18

type OverrideExceptionHandler struct {
	PathSelect
}

OverrideExceptionHandler replaces all paths EXCEPT the ones selected

func OverrideException added in v0.1.18

func OverrideException(sel PathSelect) *OverrideExceptionHandler

func (*OverrideExceptionHandler) Replace added in v0.1.18

func (handler *OverrideExceptionHandler) Replace(p string) bool

type PathSelect added in v0.1.20

type PathSelect interface {
	Keep(p string) bool
}

func GetPathSelect added in v0.1.20

func GetPathSelect(ctx context.Context) PathSelect

func IgnoreNone added in v0.0.51

func IgnoreNone() PathSelect

type Replacement

type Replacement struct {
	From string `yaml:"from"`
	To   string `yaml:"to"`
}

type S added in v0.0.13

type S = string

type SelectPatterns added in v0.1.4

type SelectPatterns struct {
	// contains filtered or unexported fields
}

func NewSelect added in v0.1.4

func NewSelect(patterns ...string) *SelectPatterns

func (*SelectPatterns) Keep added in v0.1.14

func (ign *SelectPatterns) Keep(file string) bool

type SignalWriter added in v0.1.84

type SignalWriter struct {
	Writer io.Writer
	// contains filtered or unexported fields
}

func NewSignalWriter added in v0.1.84

func NewSignalWriter(writer io.Writer) *SignalWriter

func (*SignalWriter) Signal added in v0.1.84

func (sw *SignalWriter) Signal() <-chan struct{}

func (*SignalWriter) Write added in v0.1.84

func (sw *SignalWriter) Write(p []byte) (n int, err error)

type SkipAllHandler added in v0.1.18

type SkipAllHandler struct{}

func SkipAll added in v0.1.18

func SkipAll() *SkipAllHandler

SkipAll skips all paths

func (*SkipAllHandler) Replace added in v0.1.18

func (*SkipAllHandler) Replace(string) bool

type SliceWriter added in v0.1.84

type SliceWriter struct {
	Data []string
	// contains filtered or unexported fields
}

SliceWriter is an io.Writer that splits its input on newlines and appends each completed line to Data. Safe for concurrent writers — the runners/base Forward fan-out spawns multiple goroutines that all write into the same SliceWriter, and the previous (mutex-less) version panicked with `slice bounds out of range` when bytes.Buffer raced on its internal cursor.

func NewSliceWriter added in v0.1.84

func NewSliceWriter() *SliceWriter

func (*SliceWriter) Close added in v0.1.84

func (sw *SliceWriter) Close() error

func (*SliceWriter) Snapshot added in v0.1.157

func (sw *SliceWriter) Snapshot() []string

Snapshot returns a copy of Data safe to read without holding the mutex. Call instead of accessing sw.Data directly when other goroutines may still be writing.

func (*SliceWriter) Write added in v0.1.84

func (sw *SliceWriter) Write(p []byte) (n int, err error)

Directories

Path Synopsis
Package leakcheck provides test-time helpers for detecting resource leaks: file descriptors, docker containers, and (via go.uber.org/goleak wired at the TestMain level) goroutines.
Package leakcheck provides test-time helpers for detecting resource leaks: file descriptors, docker containers, and (via go.uber.org/goleak wired at the TestMain level) goroutines.

Jump to

Keyboard shortcuts

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