util
import "github.com/ice-bergtech/kr8/pkg/util"
Package util contains various utility functions for directories and files. It includes functions for formatting JSON, writing to files, directory management, and go control-flow helpers
Index
- func BuildDirFileList(directory string) ([]string, error)
- func CalculateClusterIncludesExcludes(input map[string]string, filters PathFilterOptions) []string
- func CheckObjectMatch(input gjson.Result, filterString string) bool
- func CleanOutputDir(outputFileMap map[string]bool, componentOutputDir string) error
- func Colorize(input interface{}, colorNum int, disabled bool) string
- func ErrorIfCheck(message string, err error) error
- func FatalErrorCheck(message string, err error, logger zerolog.Logger)
- func FetchRepoUrl(url string, destination string, noop bool) error
- func Filter(vs []string, f func(string) bool) []string
- func FilterItems(input map[string]string, pFilter PathFilterOptions) []string
- func FormatJsonnetString(input string) (string, error)
- func FormatJsonnetStringCustom(input string, opts formatter.Options) (string, error)
- func GetClusterFilenames(searchDir string) ([]types.Kr8Cluster, error)
- func GetClusterParamsFilenames(basePath string, targetPath string) []string
- func GetClusterPath(searchDir string, clusterName string) (string, error)
- func GetDefaultFormatOptions() formatter.Options
- func HashFile(path string) (string, error)
- func JsonnetPrint(output string, format string, color bool) error
- func LogErrorIfCheck(message string, err error, logger zerolog.Logger) error
- func Pretty(inputJson string, colorOutput bool) (string, error)
- func ReadFile(file string) ([]byte, error)
- func ReadGzip(filename string) ([]byte, error)
- func SetupLogger(enableColor bool) zerolog.Logger
- func WriteFile(input []byte, file string) error
- func WriteGzip(input []byte, file string) error
- func WriteObjToJsonFile(filename string, path string, objStruct interface{}) (string, error)
- type PathFilterOptions
func BuildDirFileList
func BuildDirFileList(directory string) ([]string, error)
Walk a directory to build a list of all files in the tree.
func CalculateClusterIncludesExcludes
func CalculateClusterIncludesExcludes(input map[string]string, filters PathFilterOptions) []string
Using the allClusterParams variable and command flags to create a list of clusters to generate. Clusters can be filtered with "=" for equality or "\~" for regex match.
func CheckObjectMatch
func CheckObjectMatch(input gjson.Result, filterString string) bool
Checks if a input object matches a filter string. The filter string can be an equality match or a regex match.
func CleanOutputDir
func CleanOutputDir(outputFileMap map[string]bool, componentOutputDir string) error
Given a map of filenames, prunes all *.yaml files that are NOT in the map from the directory.
func Colorize
func Colorize(input interface{}, colorNum int, disabled bool) string
Colorize function from zerolog console.go file to replicate their coloring functionality. Source: https://github.com/rs/zerolog/blob/a21d6107dcda23e36bc5cfd00ce8fdbe8f3ddc23/console.go#L389 Replicated here because it's a private function.
func ErrorIfCheck
func ErrorIfCheck(message string, err error) error
If err != nil, wraps it in a Kr8Error with the message.
func FatalErrorCheck
func FatalErrorCheck(message string, err error, logger zerolog.Logger)
Logs an error and exits the program if the error is not nil.
func FetchRepoUrl
func FetchRepoUrl(url string, destination string, noop bool) error
Fetch a git repo from a url and clone it to a destination directory. If the noop flag is true, it print commands to fetch manually without doing anything.
func Filter
func Filter(vs []string, f func(string) bool) []string
Filter returns a new slice containing only the elements that satisfy the predicate function. From https://gobyexample.com/collection-functions
func FilterItems
func FilterItems(input map[string]string, pFilter PathFilterOptions) []string
Given a map of string, filter them based on the provided options. The map value is parsed as a gjson result and then checked against the provided options.
func FormatJsonnetString
func FormatJsonnetString(input string) (string, error)
Formats a jsonnet string using the default options.
func FormatJsonnetStringCustom
func FormatJsonnetStringCustom(input string, opts formatter.Options) (string, error)
Formats a jsonnet string using custom options.
func GetClusterFilenames
func GetClusterFilenames(searchDir string) ([]types.Kr8Cluster, error)
Get a list of cluster from within a directory. Walks the directory tree, creating a types.Kr8Cluster for each cluster.jsonnet file found.
func GetClusterParamsFilenames
func GetClusterParamsFilenames(basePath string, targetPath string) []string
Get all cluster parameters within a directory. Walks through the directory hierarchy and returns all paths to `params.jsonnet` files.
func GetClusterPath
func GetClusterPath(searchDir string, clusterName string) (string, error)
Get a specific cluster within a directory by name. [filepath.Walk]s the cluster directory tree searching for the given clusterName. Returns the path to the cluster.jsonnet file.
func GetDefaultFormatOptions
func GetDefaultFormatOptions() formatter.Options
Configures the default options for the jsonnet formatter.
func HashFile
func HashFile(path string) (string, error)
Calculate the sha256 hash and returns the base64 encoded result.
func JsonnetPrint
func JsonnetPrint(output string, format string, color bool) error
Print the jsonnet in the specified format. Acceptable formats are: yaml, stream, json.
func LogErrorIfCheck
func LogErrorIfCheck(message string, err error, logger zerolog.Logger) error
If the error is not nil, log an error and wrap the error in a Kr8Error.
func Pretty
func Pretty(inputJson string, colorOutput bool) (string, error)
Pretty formats the input jsonnet string with indentation and optional color output. Returns an error when the input can't properly format the json string input.
func ReadFile
func ReadFile(file string) ([]byte, error)
Read bytes from file (path included).
func ReadGzip
func ReadGzip(filename string) ([]byte, error)
Read bytes from a gzip file (path included).
func SetupLogger
func SetupLogger(enableColor bool) zerolog.Logger
Configure zerolog with some defaults and cleanup error formatting.
func WriteFile
func WriteFile(input []byte, file string) error
Write bytes to file (path included).
func WriteGzip
func WriteGzip(input []byte, file string) error
Write bytes to gzip file (path included).
func WriteObjToJsonFile
func WriteObjToJsonFile(filename string, path string, objStruct interface{}) (string, error)
Write out a struct to a specified path and file. Marshals the given interface and generates a formatted json string. All parent directories needed are created.
type PathFilterOptions
Fill with string to include and exclude, using kr8's special parsing.
type PathFilterOptions struct {
// Comma-separated list of include filters
// Filters can include:
//
// regex filters using the "~" operator. For example, "name~^myRegex$"
// equality matches using the "=" operator. For example, "name=myValue"
// substring matches using the "=" operator. For example, "name=myValue"
//
// If no operator is provided, it is treated as a substring match against the "name" field.
Includes string
// Comma-separated list of exclude filters.
// Filters can include:
//
// regex filters using the "~" operator. For example, "name~^myRegex$"
// equality matches using the "=" operator. For example, "name=myValue"
// substring matches using the "=" operator. For example, "name=myValue"
//
// If no operator is provided, it is treated as a substring match against the "name" field.
Excludes string
// Comma separated cluster names.
// Filters keys on exact match.
Clusters string
// Comma separated component names.
Components string
}