Documentation
¶
Overview ¶
Package utils provides shared utility functions for the Tempo CLI.
This package contains utilities organized by domain:
File System Operations (fs.go) ¶
Functions for file and directory management:
- FileOrDirExists, FileExists, DirExists - Check existence
- EnsureDirExists, RemoveIfExists - Create/remove
- ReadFileAsString, WriteToFile, WriteStringToFile, WriteJSONToFile - Read/write
- CheckMissingFolders, ValidateFoldersExistence - Validation
- FileSystemOperations interface for dependency injection
Path Utilities (fs.go) ¶
Functions for path manipulation:
- GetCWD, ResolvePath - Current directory and path resolution
- ToTemplFilename, RebasePathToOutput - Template path conversion
- RemoveTemplatingExtension - Extension handling
- GetModuleName - Go module detection
Embedded Resources (embed.go) ¶
Functions for working with embedded filesystems:
- IsEmbedded - Check if path is embedded
- ReadEmbeddedFile, ReadEmbeddedDir - Read embedded content
- CopyDirFromEmbed, CopyFileFromEmbed - Copy embedded resources
Template Rendering (renderer.go) ¶
Functions for template processing:
- RenderTemplate - Render Go templates with custom functions
String Utilities (strings.go) ¶
Functions for string manipulation:
- ContainsSubstring - Case-insensitive substring check
- ExtractNameFromURL, ExtractNameFromPath - Name extraction
Type Conversion (numbers.go) ¶
Functions for type conversion:
- Int64ToInt - Safe int64 to int conversion
GoPackage utils provides utility functions and helpers for common operations such as string manipulation and template rendering. It integrates custom template functions with third-party libraries to enhance templating capabilities.
Index ¶
- Variables
- func CheckMissingFolders(folders map[string]string) (map[string]string, error)
- func ContainsSubstring(str, substr string) bool
- func CopyDirFromEmbed(source, destination string) error
- func CopyFileFromEmbed(source, destination string) error
- func DirExists(path string) (bool, error)
- func EnsureDirExists(dir string) error
- func ErrorContains(err error, substr string) bool
- func ExtractNameFromPath(path string) string
- func ExtractNameFromURL(url string) string
- func FileExists(path string) (bool, error)
- func FileOrDirExists(path string) (exists bool, isDir bool, err error)
- func GetCWD() string
- func GetModuleName(goModPath string) (string, error)
- func Int64ToInt(i64 int64) (int, error)
- func IsEmbedded(path string) bool
- func ReadEmbeddedDir(path string) ([]os.DirEntry, error)
- func ReadEmbeddedFile(path string) ([]byte, error)
- func ReadFileAsString(filePath string) (string, error)
- func RebasePathToOutput(inputPath, inputDir, outputDir string) string
- func RemoveIfExists(path string) error
- func RemoveTemplatingExtension(filename string, extensions []string) string
- func RenderTemplate(templateContent string, data any) (string, error)
- func ResolvePath(path string) (string, error)
- func ToTemplFilename(fileName string) string
- func ValidateFoldersExistence(folders []string, errorMessage string) error
- func WriteJSONToFile(filePath string, data any) error
- func WriteStringToFile(path string, content string) error
- func WriteToFile(path string, content []byte) error
- type DefaultFileSystem
- func (fs *DefaultFileSystem) CheckMissingFolders(folders map[string]string) (map[string]string, error)
- func (fs *DefaultFileSystem) DirExists(path string) (bool, error)
- func (fs *DefaultFileSystem) EnsureDirExists(dir string) error
- func (fs *DefaultFileSystem) FileExists(path string) (bool, error)
- func (fs *DefaultFileSystem) FileOrDirExists(path string) (exists bool, isDir bool, err error)
- func (fs *DefaultFileSystem) ReadFileAsString(filePath string) (string, error)
- func (fs *DefaultFileSystem) WriteToFile(path string, content []byte) error
- type FileSystemOperations
Constants ¶
This section is empty.
Variables ¶
var CheckMissingFoldersFunc = CheckMissingFolders
var CopyDirFromEmbedFunc = CopyDirFromEmbed
CopyDirFromEmbedFunc is a function variable to allow testing overrides.
var CopyFileFromEmbedFunc = CopyFileFromEmbed
CopyFileFromEmbedFunc is a function variable to allow testing overrides.
var FileExistsFunc = FileExists
FileExistsFunc is a function variable to allow testing overrides.
var FileOrDirExistsFunc = FileOrDirExists
FileOrDirExistsFunc is a function variable to allow testing overrides.
var IsEmbeddedFunc = IsEmbedded
IsEmbeddedFunc is a function variable to allow testing overrides.
Functions ¶
func CheckMissingFolders ¶
CheckMissingFolders validates folder paths and returns a sorted map of missing folders.
func ContainsSubstring ¶
ContainsSubstring checks if a substring exists within a string
func CopyDirFromEmbed ¶
CopyDirFromEmbed copies all files and subdirectories from an embedded source to a destination directory.
func CopyFileFromEmbed ¶
CopyFileFromEmbed copies a single file from an embedded source to a destination file path.
func DirExists ¶
DirExists checks if a directory exists at the specified path. It wraps FileOrDirExists and ensures the path points to a directory, not a file.
func EnsureDirExists ¶
EnsureDirExists ensures that a directory exists or creates it if it does not.
func ErrorContains ¶ added in v0.3.0
ErrorContains checks if any error in the error chain contains the specified substring. It unwraps the error chain and checks each error's message.
func ExtractNameFromPath ¶
ExtractNameFromPath extracts the last folder name from a given file path.
func ExtractNameFromURL ¶
ExtractNameFromURL extracts the repository name from a URL, removing `.git` if present.
func FileExists ¶
FileExists checks if a file exists at the specified path. It wraps FileOrDirExists and ensures the path points to a file, not a directory.
func FileOrDirExists ¶
FileOrDirExists checks whether a file or directory exists at the specified path.
func GetCWD ¶
func GetCWD() string
GetCWD retrieves the current working directory. It logs an error and exits the program if the directory cannot be retrieved.
func GetModuleName ¶
GetModuleName extracts the module name from the go.mod file.
func Int64ToInt ¶
func IsEmbedded ¶
IsEmbedded checks if a file or directory is embedded in the embedded filesystem.
func ReadEmbeddedDir ¶
ReadEmbeddedDir reads the entries of a directory from embedded resources.
func ReadEmbeddedFile ¶
ReadEmbeddedFile reads a file from embedded resources and returns its content as a byte slice.
func ReadFileAsString ¶
ReadFileAsString reads a file and returns its content as a string.
func RebasePathToOutput ¶
RebasePathToOutput transforms an input file path by replacing its root directory (`inputDir`) with `outputDir` and ensuring the file extension is `.templ`.
The function performs the following steps: 1. Cleans and normalizes the input path by removing redundant slashes and "./" prefixes. 2. Ensures the input path starts with `inputDir` and removes it. 3. Constructs the new path within `outputDir` while preserving the relative structure. 4. Converts the file extension to `.templ`.
Parameters:
- inputPath: The original file path to be transformed.
- inputDir: The root directory to be replaced in `inputPath`.
- outputDir: The target directory where the transformed file should be placed.
Returns:
- A new file path within `outputDir`, using a `.templ` extension.
func RemoveIfExists ¶
RemoveIfExists removes a file or directory if it exists.
func RemoveTemplatingExtension ¶
RemoveTemplatingExtension removes specific templating extensions (e.g., ".gotxt", ".gotmpl") while retaining the rest of the filename.
func RenderTemplate ¶
RenderTemplate renders a template string with the provided data.
This function combines the `text/template` package with additional user registered functions to extend templating capabilities.
func ResolvePath ¶
ResolvePath normalizes local paths, prevents traversal, and ensures safe resolution.
func ToTemplFilename ¶
ToTemplFilename converts a filename to its `.templ` equivalent by removing its extension.
func ValidateFoldersExistence ¶
ValidateFoldersExistence checks if the specified folders exist and returns an error if any of them are missing.
func WriteJSONToFile ¶
WriteJSONToFile writes the given data as JSON to the specified file.
func WriteStringToFile ¶
WriteStringToFile writes string content to the specified file.
func WriteToFile ¶
WriteToFile writes byte content to the specified file, ensuring directories exist.
Types ¶
type DefaultFileSystem ¶ added in v0.3.0
type DefaultFileSystem struct{}
DefaultFileSystem is the default implementation of FileSystemOperations.
func (*DefaultFileSystem) CheckMissingFolders ¶ added in v0.3.0
func (fs *DefaultFileSystem) CheckMissingFolders(folders map[string]string) (map[string]string, error)
CheckMissingFolders implements FileSystemOperations.CheckMissingFolders.
func (*DefaultFileSystem) DirExists ¶ added in v0.3.0
func (fs *DefaultFileSystem) DirExists(path string) (bool, error)
DirExists implements FileSystemOperations.DirExists.
func (*DefaultFileSystem) EnsureDirExists ¶ added in v0.3.0
func (fs *DefaultFileSystem) EnsureDirExists(dir string) error
EnsureDirExists implements FileSystemOperations.EnsureDirExists.
func (*DefaultFileSystem) FileExists ¶ added in v0.3.0
func (fs *DefaultFileSystem) FileExists(path string) (bool, error)
FileExists implements FileSystemOperations.FileExists.
func (*DefaultFileSystem) FileOrDirExists ¶ added in v0.3.0
func (fs *DefaultFileSystem) FileOrDirExists(path string) (exists bool, isDir bool, err error)
FileOrDirExists implements FileSystemOperations.FileOrDirExists.
func (*DefaultFileSystem) ReadFileAsString ¶ added in v0.3.0
func (fs *DefaultFileSystem) ReadFileAsString(filePath string) (string, error)
ReadFileAsString implements FileSystemOperations.ReadFileAsString.
func (*DefaultFileSystem) WriteToFile ¶ added in v0.3.0
func (fs *DefaultFileSystem) WriteToFile(path string, content []byte) error
WriteToFile implements FileSystemOperations.WriteToFile.
type FileSystemOperations ¶ added in v0.3.0
type FileSystemOperations interface {
FileOrDirExists(path string) (exists bool, isDir bool, err error)
FileExists(path string) (bool, error)
DirExists(path string) (bool, error)
CheckMissingFolders(folders map[string]string) (map[string]string, error)
EnsureDirExists(dir string) error
ReadFileAsString(filePath string) (string, error)
WriteToFile(path string, content []byte) error
}
FileSystemOperations defines the interface for filesystem operations. This allows for dependency injection and easier testing.
func NewFileSystemOperations ¶ added in v0.3.0
func NewFileSystemOperations() FileSystemOperations
NewFileSystemOperations creates a new FileSystemOperations instance.