util

package
v2.11.1 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: Apache-2.0 Imports: 33 Imported by: 0

Documentation

Index

Constants

View Source
const Any = ""

Network address which represent any address. This is the default that we should use when checking if port is free.

View Source
const Loopback = "127.0.0.1"

Loopback network address. Skaffold should not bind to 0.0.0.0 unless we really want to expose something to the network.

Variables

Fs is the underlying filesystem to use for reading skaffold project files & configuration. OS FS by default

View Source
var (
	OSEnviron = os.Environ
)

For testing

View Source
var SkipWrapperCheck = false

Functions

func AbsFile

func AbsFile(workspace string, filename string) (string, error)

AbsFile resolves the absolute path of the file named filename in directory workspace, erroring if it is not a file

func AbsolutePaths

func AbsolutePaths(workspace string, paths []string) []string

AbsolutePaths prepends each path in paths with workspace if the path isn't absolute

func AllocatePort

func AllocatePort(isPortAvailable func(int32) bool, desiredPort int32) int32

AllocatePort looks for a port close to desiredPort, using the provided implementation of isPortAvailable to determine what ports can be used. We deal with wrapping and avoid allocating ports < 1024 TODO(nkubala)09/14/21: plumb through context from callers

func CloneThroughJSON

func CloneThroughJSON(old interface{}, new interface{})

CloneThroughJSON clones an `old` object into a `new` one using json marshalling and unmarshalling. Since the object can be marshalled, it's almost sure it can be unmarshalled. So we prefer to panic instead of returning an error that would create an untestable branch on the call site.

func CloneThroughYAML

func CloneThroughYAML(old interface{}, new interface{})

CloneThroughYAML clones an `old` object into a `new` one using yaml marshalling and unmarshalling. Since the object can be marshalled, it's almost sure it can be unmarshalled. So we prefer to panic instead of returning an error that would create an untestable branch on the call site.

func ConvertFromV1Platform

func ConvertFromV1Platform(platform v1.Platform) specs.Platform

func ConvertToV1Platform

func ConvertToV1Platform(platform specs.Platform) v1.Platform

func CreateMappedTar

func CreateMappedTar(ctx context.Context, w io.Writer, root string, pathMap map[string][]string) error

func CreateTar

func CreateTar(ctx context.Context, w io.Writer, root string, paths []string) error

func CreateTarGz

func CreateTarGz(ctx context.Context, w io.Writer, root string, paths []string) error

func CreateTarWithParents

func CreateTarWithParents(ctx context.Context, w io.Writer, root string, paths []string, uid, gid int, modTime time.Time) error

func Download

func Download(url string) ([]byte, error)

func EnvMapToSlice

func EnvMapToSlice(m map[string]string, separator string) []string

EnvMapToSlice converts map of (string,string) to string slice

func EnvPtrMapToSlice

func EnvPtrMapToSlice(m map[string]*string, separator string) []string

EnvPtrMapToSlice converts map of (string,*string) to string slice

func EnvSliceToMap

func EnvSliceToMap(slice []string, separator string) map[string]string

EnvSliceToMap converts a string slice into a map of (string,string) using the given separator

func EvaluateEnvTemplateMap

func EvaluateEnvTemplateMap(args map[string]*string) (map[string]*string, error)

EvaluateEnvTemplateMap parses and executes all map values as templates based on OS environment variables

func EvaluateEnvTemplateMapWithEnv

func EvaluateEnvTemplateMapWithEnv(args map[string]*string, env map[string]string) (map[string]*string, error)

EvaluateEnvTemplateMapWithEnv parses and executes all map values as templates based on OS and custom environment variables

func ExecuteEnvTemplate

func ExecuteEnvTemplate(envTemplate *template.Template, customMap map[string]string) (string, error)

ExecuteEnvTemplate executes an envTemplate based on OS environment variables and a custom map

func Expand

func Expand(text, key, value string) string

Expand replaces placeholders for a given key with a given value. It supports the ${key} and the $key syntax.

func ExpandEnvTemplate

func ExpandEnvTemplate(s string, envMap map[string]string) (string, error)

ExpandEnvTemplate parses and executes template s with an optional environment map

func ExpandEnvTemplateOrFail

func ExpandEnvTemplateOrFail(s string, envMap map[string]string) (string, error)

ExpandEnvTemplateOrFail parses and executes template s with an optional environment map, and errors if a reference cannot be satisfied.

func ExpandHomePath added in v2.2.0

func ExpandHomePath(path string) string

func ExpandPathsGlob

func ExpandPathsGlob(workingDir string, paths []string) ([]string, error)

ExpandPathsGlob expands paths according to filepath.Glob patterns Returns a list of unique files that match the glob patterns passed in.

func GetAvailablePort

func GetAvailablePort(address string, port int, usedPorts *PortSet) int

GetAvailablePort returns an available port that is near the requested port when possible. First, check if the provided port is available on the specified address and INADDR_ANY. If so, use it. If not, check if any of the next 10 subsequent ports are available. If not, check if any of ports 4503-4533 are available. If not, return a random port, which hopefully won't collide with any future containers

See https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.txt

func IsDir

func IsDir(path string) bool

func IsEmptyDir

func IsEmptyDir(path string) bool

IsEmptyDir returns true for empty directories otherwise false

func IsFile

func IsFile(path string) bool

func IsHiddenDir

func IsHiddenDir(filename string) bool

IsHiddenDir returns if a directory is hidden.

func IsHiddenFile

func IsHiddenFile(filename string) bool

IsHiddenFile returns if a file is hidden. File is hidden if it starts with prefix "."

func IsPortFree

func IsPortFree(address string, p int) bool

func IsSubPath

func IsSubPath(basepath string, targetpath string) bool

IsSubPath return true if targetpath is sub-path of basepath; doesn't check for symlinks

func IsURL

func IsURL(s string) bool

func MapToFlag

func MapToFlag(m map[string]*string, flag string) ([]string, error)

MapToFlag parses all map values and returns them as `key=value` with the given flag Example: --my-flag key0=value0 --my-flag key1=value1 --my-flag key2=value2

func NonEmptyLines

func NonEmptyLines(input []byte) []string

NonEmptyLines scans the provided input and returns the non-empty strings found as an array

func ParseEnvTemplate

func ParseEnvTemplate(t string) (*template.Template, error)

ParseEnvTemplate is a simple wrapper to parse an env template

func ParseEnvVariablesFromFile added in v2.4.0

func ParseEnvVariablesFromFile(fp string) (map[string]string, error)

func ParseNamespaceFromFlags added in v2.2.0

func ParseNamespaceFromFlags(flgs []string) string

func Ptr

func Ptr[T any](t T) *T

func RandomID

func RandomID() string

func ReadConfiguration

func ReadConfiguration(filePath string) ([]byte, error)

ReadConfiguration reads a `skaffold.yaml` configuration and returns its content.

func ReadFile

func ReadFile(filename string) ([]byte, error)

func RealWorkDir

func RealWorkDir() (string, error)

On macOS, $PWD can have a different case that the actual current working directory. That happens for example if one does: `cd /home/me/MY-PROJECT` before running Skaffold, instead of `cd /home/me/my-project`. In such a situation, the file watcher will fail to detect changes. To solve that, we force `os.Getwd()` not to use $PWD. See: https://github.com/rjeczalik/notify/issues/96

func RegexEqual

func RegexEqual(expected, actual string) bool

RegexEqual matches the string 'actual' against a regex compiled from 'expected' If 'expected' is not a valid regex, string comparison is used as fallback

func RunCmd

func RunCmd(ctx context.Context, cmd *exec.Cmd) error

func RunCmdOut

func RunCmdOut(ctx context.Context, cmd *exec.Cmd) ([]byte, error)

func RunCmdOutOnce added in v2.2.0

func RunCmdOutOnce(ctx context.Context, cmd *exec.Cmd) ([]byte, error)

func SanitizeHelmTemplateValue

func SanitizeHelmTemplateValue(s string) string

func VerifyOrCreateFile

func VerifyOrCreateFile(path string) error

VerifyOrCreateFile checks if a file exists at the given path, and if not, creates all parent directories and creates the file.

Types

type Command

type Command interface {
	RunCmdOut(ctx context.Context, cmd *exec.Cmd) ([]byte, error)
	RunCmd(ctx context.Context, cmd *exec.Cmd) error
	RunCmdOutOnce(ctx context.Context, cmd *exec.Cmd) ([]byte, error)
}

Command is an interface used to run commands. All packages should use this interface instead of calling exec.Cmd directly.

var DefaultExecCommand Command = newCommander()

DefaultExecCommand runs commands using exec.Cmd

type CommandWrapper

type CommandWrapper struct {
	// Executable is the base name of the command, like `gradle`
	Executable string

	// Wrapper is the optional base name of a command wrapper, like `gradlew`
	Wrapper string
}

CommandWrapper defines an association between an executable command (like `gradle`) and possible command wrappers (like `gradlew`). `CreateCommand` uses this definition to create a `Cmd` object. Maven and Gradle projects often provide a wrapper script to ensure a particular version of their builder is used.

func (CommandWrapper) CreateCommand

func (cw CommandWrapper) CreateCommand(ctx context.Context, workingDir string, args []string) exec.Cmd

CreateCommand creates an `exec.Cmd` that is configured to call the executable (possibly using a wrapper in `workingDir`, when found) with the given arguments, with working directory set to `workingDir`.

type Commander

type Commander struct {
	// contains filtered or unexported fields
}

Commander is the exec.Cmd implementation of the Command interface

func (*Commander) RunCmd

func (*Commander) RunCmd(ctx context.Context, cmd *exec.Cmd) error

RunCmd runs an exec.Command.

func (*Commander) RunCmdOut

func (*Commander) RunCmdOut(ctx context.Context, cmd *exec.Cmd) ([]byte, error)

RunCmdOut runs an exec.Command and returns the stdout and error.

func (*Commander) RunCmdOutOnce added in v2.2.0

func (c *Commander) RunCmdOutOnce(ctx context.Context, cmd *exec.Cmd) ([]byte, error)

type PortSet

type PortSet struct {
	// contains filtered or unexported fields
}

func (*PortSet) Delete

func (f *PortSet) Delete(port int)

func (*PortSet) Length

func (f *PortSet) Length() int

func (*PortSet) List

func (f *PortSet) List() []int

func (*PortSet) LoadOrSet

func (f *PortSet) LoadOrSet(port int) bool

func (*PortSet) Set

func (f *PortSet) Set(port int)

type StoreError

type StoreError struct {
	// contains filtered or unexported fields
}

StoreError represent any error that when retrieving errors from the store.

func (StoreError) Error

func (e StoreError) Error() string

type SyncStore

type SyncStore[T any] struct {
	// contains filtered or unexported fields
}

SyncStore exports a single method `Exec` to ensure single execution of a function and share the result between all callers of the function.

func NewSyncStore

func NewSyncStore[T any]() *SyncStore[T]

NewSyncStore returns a new instance of `SyncStore`

func (*SyncStore[T]) Exec

func (o *SyncStore[T]) Exec(key string, f func() (T, error)) (T, error)

Exec executes the function f if and only if it's being called the first time for a specific key. If it's called multiple times for the same key only the first call will execute and store the result of f. All other calls will be blocked until the running instance of f returns and all of them receive the same result.

func (*SyncStore[T]) Store

func (o *SyncStore[T]) Store(key string, r T, err error)

Store will store the results for a key in a cache This function is not safe to use if multiple subroutines store the result for the same key.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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