Documentation
¶
Overview ¶
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 CheckEntityForDefine(entityType, outputPath string, force bool, logr logger.LoggerInterface)
- func CheckEntityForNew(entityType, entityName, outputPath string, force bool, ...)
- 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 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
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 CheckEntityForDefine ¶
func CheckEntityForDefine(entityType, outputPath string, force bool, logr logger.LoggerInterface)
CheckEntityForDefine ensures that an entity exists before defining it.
func CheckEntityForNew ¶
func CheckEntityForNew(entityType, entityName, outputPath string, force bool, logr logger.LoggerInterface)
CheckEntityForNew ensures that an entity does not already exist before creating a new one.
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 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 ¶
This section is empty.