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 ¶
- func CheckDirectoryOrCreate(ctx context.Context, dir string) (bool, error)
- func CheckDirectoryOrCreateSecure(ctx context.Context, dir string) (bool, error)
- func CheckEmptyDirectory(ctx context.Context, dir string) (bool, error)
- func CheckEmptyDirectoryOrCreate(ctx context.Context, dir string) (bool, error)
- func CopyFile(_ context.Context, from string, to string) error
- func CreateFile(ctx context.Context, path string) error
- func DefaultTo(s string, defaultValue string) string
- func DeleteFile(ctx context.Context, file string) error
- func DirectoryExists(ctx context.Context, dir string) (bool, error)
- func EmptyDir(ctx context.Context, dir string) error
- func Exists(ctx context.Context, p string) (bool, error)
- func FileExists(ctx context.Context, file string) (bool, error)
- func GenerateTree(p, indent string) (string, error)
- func Must[T any](t T, err error) T
- func MustSolvePath(p string) string
- func Pointer[T any](t T) *T
- func PointerEqual[T comparable](t *T, target T) bool
- func ProtoType(m proto.Message) string
- func RenameDir(ctx context.Context, from string, to string) error
- func Retry(interval time.Duration, maxAttempts int, task func() error) error
- func ShortLowerUUID() (string, error)
- func SolvePath(p string) (string, error)
- func ToCamelCase(s string) string
- func ToDNSCase(s string) string
- func ToDotSeparatedCase(s string) string
- func ToKebabCase(str string) string
- func ToLowerCase(s string) string
- func ToSnakeCase(s string) string
- func ToTitle(s string) string
- func TypeOf[T any]() string
- func WithOverride(ctx context.Context, override Override) context.Context
- func WriteFileAtomic(ctx context.Context, path string, data []byte, mode os.FileMode) error
- func WriteFileSecure(ctx context.Context, path string, data []byte) error
- type Case
- type ContextOverrideKey
- type ContextPathSelectKey
- type CopyInstruction
- type DirReader
- type ErrorResourceNotFound
- type FSReader
- type FileSystem
- type IgnoreNoneHandler
- type IgnorePatterns
- type Override
- type OverrideAllHandler
- type OverrideExceptionHandler
- type PathSelect
- type Replacement
- type S
- type SelectPatterns
- type SignalWriter
- type SkipAllHandler
- type SliceWriter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckDirectoryOrCreate ¶
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
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
CheckEmptyDirectory checks if a directory exists and is empty
func CheckEmptyDirectoryOrCreate ¶
CheckEmptyDirectoryOrCreate ensures dir exists and is empty. It reports whether the directory was created.
func CreateFile ¶ added in v0.1.103
Create is equivalent of "touch"
func DirectoryExists ¶ added in v0.0.13
DirectoryExists checks for existence of folder err only for unexpected behavior
func Exists ¶ added in v0.1.89
Exists checks for existence of file/folder err only for unexpected behavior
func FileExists ¶
FileExists checks for existence of folder err only for unexpected behavior
func GenerateTree ¶
GenerateTree recursively generates a string representation of the directory tree
func Must ¶ added in v0.0.42
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
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 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 Retry ¶ added in v0.1.127
Retry attempts to execute a task function until it succeeds or reaches a maximum number of attempts.
func ShortLowerUUID ¶ added in v0.1.53
ShortLowerUUID returns a UUID of 10 characters in base26 (lowercase letters only)
func ToCamelCase ¶ added in v0.0.51
ToCamelCase converts a string to camelCase
func ToDotSeparatedCase ¶ added in v0.1.31
func ToKebabCase ¶ added in v0.0.51
ToKebabCase converts a string to kebab-case
func ToLowerCase ¶ added in v0.0.51
func ToSnakeCase ¶ added in v0.0.51
ToSnakeCase converts a string to snake_case
func WithOverride ¶ added in v0.0.51
func WriteFileAtomic ¶ added in v0.2.15
WriteFileAtomic writes data to a temporary file in the destination directory, flushes it, and renames it over path. Keeping the temporary file on the same filesystem makes the rename atomic, so readers observe either the old file or the complete new file rather than a truncated intermediate state.
func WriteFileSecure ¶ added in v0.1.157
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 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 DirReader ¶ added in v0.0.13
type DirReader struct {
// contains filtered or unexported fields
}
func NewDirReader ¶ added in v0.0.13
func NewDirReader() *DirReader
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 FileSystem ¶ added in v0.0.13
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
func GetOverride ¶ added in v0.0.51
type OverrideAllHandler ¶ added in v0.1.17
type OverrideAllHandler struct{}
func (*OverrideAllHandler) Replace ¶ added in v0.1.17
func (*OverrideAllHandler) Replace(string) bool
type OverrideExceptionHandler ¶ added in v0.1.17
type OverrideExceptionHandler struct {
PathSelect
}
OverrideExceptionHandler replaces all paths EXCEPT the ones selected
func OverrideException ¶ added in v0.1.17
func OverrideException(sel PathSelect) *OverrideExceptionHandler
func (*OverrideExceptionHandler) Replace ¶ added in v0.1.17
func (handler *OverrideExceptionHandler) Replace(p string) bool
type PathSelect ¶ added in v0.1.20
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 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
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{}
type SkipAllHandler ¶ added in v0.1.17
type SkipAllHandler struct{}
func (*SkipAllHandler) Replace ¶ added in v0.1.17
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.
Source Files
¶
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. |