utils

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2026 License: MIT Imports: 22 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var CheckMissingFoldersFunc = CheckMissingFolders
View Source
var CopyDirFromEmbedFunc = CopyDirFromEmbed

CopyDirFromEmbedFunc is a function variable to allow testing overrides.

View Source
var CopyFileFromEmbedFunc = CopyFileFromEmbed

CopyFileFromEmbedFunc is a function variable to allow testing overrides.

View Source
var FileExistsFunc = FileExists

FileExistsFunc is a function variable to allow testing overrides.

View Source
var FileOrDirExistsFunc = FileOrDirExists

FileOrDirExistsFunc is a function variable to allow testing overrides.

View Source
var IsEmbeddedFunc = IsEmbedded

IsEmbeddedFunc is a function variable to allow testing overrides.

Functions

func CheckMissingFolders

func CheckMissingFolders(folders map[string]string) (map[string]string, error)

CheckMissingFolders validates folder paths and returns a sorted map of missing folders.

func ContainsSubstring

func ContainsSubstring(str, substr string) bool

ContainsSubstring checks if a substring exists within a string

func CopyDirFromEmbed

func CopyDirFromEmbed(source, destination string) error

CopyDirFromEmbed copies all files and subdirectories from an embedded source to a destination directory.

func CopyFileFromEmbed

func CopyFileFromEmbed(source, destination string) error

CopyFileFromEmbed copies a single file from an embedded source to a destination file path.

func DirExists

func DirExists(path string) (bool, error)

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

func EnsureDirExists(dir string) error

EnsureDirExists ensures that a directory exists or creates it if it does not.

func ErrorContains added in v0.3.0

func ErrorContains(err error, substr string) bool

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

func ExtractNameFromPath(path string) string

ExtractNameFromPath extracts the last folder name from a given file path.

func ExtractNameFromURL

func ExtractNameFromURL(url string) string

ExtractNameFromURL extracts the repository name from a URL, removing `.git` if present.

func FileExists

func FileExists(path string) (bool, error)

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

func FileOrDirExists(path string) (exists bool, isDir bool, err error)

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

func GetModuleName(goModPath string) (string, error)

GetModuleName extracts the module name from the go.mod file.

func Int64ToInt

func Int64ToInt(i64 int64) (int, error)

func IsEmbedded

func IsEmbedded(path string) bool

IsEmbedded checks if a file or directory is embedded in the embedded filesystem.

func ReadEmbeddedDir

func ReadEmbeddedDir(path string) ([]os.DirEntry, error)

ReadEmbeddedDir reads the entries of a directory from embedded resources.

func ReadEmbeddedFile

func ReadEmbeddedFile(path string) ([]byte, error)

ReadEmbeddedFile reads a file from embedded resources and returns its content as a byte slice.

func ReadFileAsString

func ReadFileAsString(filePath string) (string, error)

ReadFileAsString reads a file and returns its content as a string.

func RebasePathToOutput

func RebasePathToOutput(inputPath, inputDir, outputDir string) string

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

func RemoveIfExists(path string) error

RemoveIfExists removes a file or directory if it exists.

func RemoveTemplatingExtension

func RemoveTemplatingExtension(filename string, extensions []string) string

RemoveTemplatingExtension removes specific templating extensions (e.g., ".gotxt", ".gotmpl") while retaining the rest of the filename.

func RenderTemplate

func RenderTemplate(templateContent string, data any) (string, error)

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

func ResolvePath(path string) (string, error)

ResolvePath normalizes local paths, prevents traversal, and ensures safe resolution.

func ToTemplFilename

func ToTemplFilename(fileName string) string

ToTemplFilename converts a filename to its `.templ` equivalent by removing its extension.

func ValidateFoldersExistence

func ValidateFoldersExistence(folders []string, errorMessage string) error

ValidateFoldersExistence checks if the specified folders exist and returns an error if any of them are missing.

func WriteJSONToFile

func WriteJSONToFile(filePath string, data any) error

WriteJSONToFile writes the given data as JSON to the specified file.

func WriteStringToFile

func WriteStringToFile(path string, content string) error

WriteStringToFile writes string content to the specified file.

func WriteToFile

func WriteToFile(path string, content []byte) error

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.

Jump to

Keyboard shortcuts

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