utils

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2025 License: MIT Imports: 22 Imported by: 0

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

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 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

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 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

This section is empty.

Jump to

Keyboard shortcuts

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