envutil

package
v2.11.26 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeduplicateEnvVars

func DeduplicateEnvVars(env []corev1.EnvVar) []corev1.EnvVar

DeduplicateEnvVars deduplicates environment variables and returns the canonical version of them (last environment variable takes preccidence).

Example
package main

import (
	"fmt"

	"github.com/google/kf/v2/pkg/internal/envutil"

	corev1 "k8s.io/api/core/v1"
)

func main() {
	envs := []corev1.EnvVar{
		{Name: "FOO", Value: "2"},
		{Name: "BAR", Value: "0"},
		{Name: "BAZZ", Value: "1"},
		{Name: "BAZZ", Value: "1.5"},
	}

	out := envutil.DeduplicateEnvVars(envs)
	for _, e := range out {
		fmt.Println("Key", e.Name, "Value", e.Value)
	}

}
Output:

Key BAR Value 0
Key BAZZ Value 1.5
Key FOO Value 2

func EnvVarsToMap

func EnvVarsToMap(envs []corev1.EnvVar) map[string]string

EnvVarsToMap constructs a map of environment name to value from a slice of env vars. Vars with duplicate names will be resolved to the latest one in the list.

func GetAppEnvVars

func GetAppEnvVars(app *v1alpha1.App) []corev1.EnvVar

GetAppEnvVars reads the environment variables off a app. Prefer using this function directly rather than accessing nested objects on app so kf can adapt to future changes.

Example
package main

import (
	"fmt"

	v1alpha1 "github.com/google/kf/v2/pkg/apis/kf/v1alpha1"
	"github.com/google/kf/v2/pkg/internal/envutil"

	corev1 "k8s.io/api/core/v1"
)

func main() {
	var app v1alpha1.App
	envutil.SetAppEnvVars(&app, []corev1.EnvVar{
		{Name: "FOO", Value: "2"},
		{Name: "BAR", Value: "0"},
	})

	env := envutil.GetAppEnvVars(&app)

	for _, e := range env {
		fmt.Println("Key", e.Name, "Value", e.Value)
	}

}
Output:

Key FOO Value 2
Key BAR Value 0
Example (EmptyApp)
package main

import (
	"fmt"

	"github.com/google/kf/v2/pkg/internal/envutil"
)

func main() {
	env := envutil.GetAppEnvVars(nil)

	fmt.Println(env)

}
Output:

[]

func MapToEnvVars

func MapToEnvVars(envMap map[string]string) []corev1.EnvVar

MapToEnvVars converts a map of name/value pairs into environment variables. The list will be sorted lexicographically based on name.

func NewJSONEnvVar

func NewJSONEnvVar(key string, value interface{}) (corev1.EnvVar, error)

NewJSONEnvVar converts a value to a JSON string and sets it on the environment variable.

Example
package main

import (
	"fmt"

	"github.com/google/kf/v2/pkg/internal/envutil"
)

func main() {
	env, err := envutil.NewJSONEnvVar("INVENTORY", map[string]bool{
		"Apples": true,
		"Bread":  false,
	})
	if err != nil {
		panic(err)
	}

	fmt.Println(env.Name, env.Value)

}
Output:

INVENTORY {"Apples":true,"Bread":false}

func ParseCLIEnvVars

func ParseCLIEnvVars(cliEnv []string) ([]corev1.EnvVar, error)

ParseCLIEnvVars turns a slice of strings formatted as NAME=VALUE into a map. The logic is taken from os/exec.dedupEnvCase with a few differences: malformed strings create an error, and case insensitivity is always assumed false.

func RemoveEnvVars

func RemoveEnvVars(varsToRemove []string, envs []corev1.EnvVar) []corev1.EnvVar

RemoveEnvVars removes the environment variables with the given names from the list.

Example
package main

import (
	"fmt"

	"github.com/google/kf/v2/pkg/internal/envutil"

	corev1 "k8s.io/api/core/v1"
)

func main() {
	envs := []corev1.EnvVar{
		{Name: "FOO", Value: "2"},
		{Name: "BAR", Value: "0"},
		{Name: "BAZZ", Value: "1"},
		{Name: "BAZZ", Value: "1.5"},
	}

	out := envutil.RemoveEnvVars([]string{"MISSING", "BAZZ"}, envs)
	for _, e := range out {
		fmt.Println("Key", e.Name, "Value", e.Value)
	}

}
Output:

Key BAR Value 0
Key FOO Value 2

func SetAppEnvVars

func SetAppEnvVars(app *v1alpha1.App, env []corev1.EnvVar)

SetAppEnvVars sets environment variables on a app. Prefer using this function directly rather than accessing nested objects on app so kf can adapt to future changes.

func SortEnvVars

func SortEnvVars(toSort []corev1.EnvVar)

SortEnvVars sorts the environment variable list in order by name.

Types

This section is empty.

Jump to

Keyboard shortcuts

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