rkcommon

package
v1.2.4 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2022 License: Apache-2.0 Imports: 20 Imported by: 16

Documentation

Overview

Package rkcommon defines utility functions for rk series of packages.

Use of this source code is governed by an Apache-style license that can be found in the LICENSE file.

Copied from https://github.com/helm/helm/tree/master/pkg/strvals

Index

Constants

View Source
const (
	BootConfigPathFlagKey = "rkboot"
	BootConfigOverrideKey = "rkset"
)
View Source
const (
	// RkMetaFilePath used in rk cli
	RkMetaFilePath = ".rk/rk.yaml"
	// RkDepFilePath used in rk cli
	RkDepFilePath = ".rk/dep/go.mod"
	// RkUtHtmlFilePath used in rk cli
	RkUtHtmlFilePath = ".rk/ut/cov.html"
	// RkUtOutFilepath used in rk cli
	RkUtOutFilepath = ".rk/ut/cov.out"
	// RkLicenseFilePath used in rk cli
	RkLicenseFilePath = ".rk/LICENSE"
	// RkReadmeFilePath used in rk cli
	RkReadmeFilePath = ".rk/README.md"
)

Variables

View Source
var ErrNotList = errors.New("not a list")

ErrNotList indicates that a non-list was treated as a list.

View Source
var (
	// GlobalFlags will read pflags passed while starting main entry
	GlobalFlags *pflag.FlagSet
)

Functions

func ConvertJSONToMap

func ConvertJSONToMap(str string) map[string]interface{}

ConvertJSONToMap convert JSON style string to map[string]interface{}. Return empty map if length of input parameter is less than 2 which can not construct a valid JSON string.

func ConvertStructToBytes

func ConvertStructToBytes(src interface{}) []byte

ConvertStructToBytes marshal struct to bytes. Return empty byte slice if input parameter is nil.

func ConvertStructToJSON

func ConvertStructToJSON(src interface{}) string

ConvertStructToJSON marshal struct to json string. Return empty string if input parameter is nil.

func ConvertStructToJSONPretty

func ConvertStructToJSONPretty(src interface{}) string

ConvertStructToJSONPretty marshal struct to pretty json string. Return empty string if input parameter is nil.

func ConvertStructToMap

func ConvertStructToMap(src interface{}) map[string]interface{}

ConvertStructToMap convert struct to map. Return empty map if input parameter is nil.

func ConvertStructToZapFields

func ConvertStructToZapFields(src interface{}) []zap.Field

ConvertStructToZapFields convert struct to zap fields. Return empty zap.Field array if input parameter is nil.

func ExtractSchemeFromURL added in v1.0.8

func ExtractSchemeFromURL(url string) string

ExtractSchemeFromURL extract scheme from endpoint

func FileExists

func FileExists(filePath string) bool

FileExists checks File existence, file path should be full path.

func GeneralizeMapKeyToString added in v1.2.0

func GeneralizeMapKeyToString(input interface{}) interface{}

GeneralizeMapKeyToString convert map key to printable one.

func GenerateRequestId

func GenerateRequestId() string

GenerateRequestId generate request id based on google/uuid. UUIDs are based on RFC 4122 and DCE 1.1: Authentication and Security Services.

A UUID is a 16 byte (128 bit) array. UUIDs may be used as keys to maps or compared directly.

func GenerateRequestIdWithPrefix

func GenerateRequestIdWithPrefix(prefix string) string

GenerateRequestIdWithPrefix generate request id based on google/uuid. UUIDs are based on RFC 4122 and DCE 1.1: Authentication and Security Services.

A UUID is a 16 byte (128 bit) array. UUIDs may be used as keys to maps or compared directly.

func GetBootConfigOriginal

func GetBootConfigOriginal(configFilePath string) map[interface{}]interface{}

GetBootConfigOriginal read config file content and unmarshal into map.

func GetBootConfigOverrides

func GetBootConfigOverrides() map[interface{}]interface{}

GetBootConfigOverrides this function will read user provided config content overrides and construct it into a map.

func GetBootConfigPath

func GetBootConfigPath(configFilePath string) string

GetBootConfigPath this function will do the following things. First, override config file path if --rkboot <config file path> was provided by user. Second, join path with current working directory if user provided path is relative path. Finally, validate file existence, shutdown process if file is missing.

func GetDefaultIfEmptyString

func GetDefaultIfEmptyString(origin, def string) string

GetDefaultIfEmptyString returns default value if original string is empty.

func GetEnvValueOrDefault

func GetEnvValueOrDefault(key, defaultValue string) string

GetEnvValueOrDefault returns default value if environment variable is empty or not exist.

func GetLocalHostname

func GetLocalHostname() string

GetLocalHostname returns hostname of localhost, return "" if error occurs or hostname is empty.

func GetLocalIP

func GetLocalIP() string

GetLocalIP This is a tricky function. We will iterate through all the network interfaces,but will choose the first one since we are assuming that eth0 will be the default one to use in most of the case.

Currently, we do not have any interfaces for selecting the network interface yet.

func GetLocale added in v1.2.0

func GetLocale() string

GetLocale returns locale from environment variable

func GetPasswordFromBasicAuthString added in v1.0.8

func GetPasswordFromBasicAuthString(basicAuth string) string

GetPasswordFromBasicAuthString extract password from basic auth formed as <username>:<password>

func GetUsernameFromBasicAuthString added in v1.0.8

func GetUsernameFromBasicAuthString(basicAuth string) string

GetUsernameFromBasicAuthString extract username from basic auth formed as <username>:<password>

func MatchLocaleWithEnv added in v1.0.8

func MatchLocaleWithEnv(locale string) bool

MatchLocaleWithEnv mainly used in entry config. RK use <realm>::<region>::<az>::<domain> to distinguish different environment. Variable of <locale> could be composed as form of <realm>::<region>::<az>::<domain>

  • realm: It could be a company, department and so on, like RK-Corp. Environment variable: REALM Eg: RK-Corp Wildcard: supported
  • domain: Stands for different environment, like dev, test, prod and so on, users can define it by themselves. Environment variable: DOMAIN Eg: prod Wildcard: supported

How it works? First, we will split locale with "::" and extract realm, region, az and domain. Second, get environment variable named as REALM, REGION, AZ and DOMAIN. Finally, compare every element in locale variable and environment variable. If variables in locale represented as wildcard(*), we will ignore comparison step.

Example: # let's assuming we are going to define DB address which is different based on environment. # Then, user can distinguish DB address based on locale. # We recommend to include locale with wildcard. --- DB:

  • name: redis-default locale: "*::*::*::*" addr: "192.0.0.1:6379"
  • name: redis-in-test locale: "*::*::*::test" addr: "192.0.0.1:6379"
  • name: redis-in-prod locale: "*::*::*::prod" addr: "176.0.0.1:6379"

func MustReadFile

func MustReadFile(filePath string) []byte

MustReadFile read files with provided path, use working directory if given path is relative path. Shutdown process if any error occurs, this should be used for MUST SUCCESS scenario like reading config files.

func OverrideLumberjackConfig

func OverrideLumberjackConfig(origin *lumberjack.Logger, override *lumberjack.Logger)

OverrideLumberjackConfig override lumberjack config. This function will override fields of non empty and non-nil.

func OverrideMap

func OverrideMap(src map[interface{}]interface{}, override map[interface{}]interface{})

OverrideMap override source map with new map items. It will iterate through all items in map and check map and slice types of item to recursively override values

Mainly used for unmarshalling YAML to map.

func OverrideSlice

func OverrideSlice(src []interface{}, override []interface{})

OverrideSlice override source slice with new slice items. It will iterate through all items in slice and check map and slice types of item to recursively override values

Mainly used for unmarshalling YAML to map.

func OverrideZapConfig

func OverrideZapConfig(origin *zap.Config, override *zap.Config)

OverrideZapConfig overrides zap config. This function will override fields of non empty and non-nil.

func ParseBootConfigOverrides

func ParseBootConfigOverrides(s string) (map[interface{}]interface{}, error)

ParseBootConfigOverrides parses a set line.

A set line is of the form name1=value1,name2=value2

func RandString

func RandString(n int) string

RandString generate random string.

func ShutdownWithError

func ShutdownWithError(err error)

ShutdownWithError shuts down and panic.

func TryReadFile added in v1.2.0

func TryReadFile(filePath string) []byte

TryReadFile reads files with provided path, use working directory if given path is relative path. Ignoring error while reading.

func UnmarshalBootConfig

func UnmarshalBootConfig(configFilePath string, config interface{})

UnmarshalBootConfig this function is combination of GetBootConfigPath, GetBootConfigOverrides and GetBootConfigOriginal. User who want to implement his/her own entry, may use this function to parse YAML config into struct. This function would also parse --rkset flags.

This function would do the following: First, read config file and unmarshal content into a map (--rkboot flag would be read). Second, read --rkset flags and override values in map unmarshalled at above step. Finally, unmarshal map into user provided struct.

Types

type Commit added in v1.2.0

type Commit struct {
	// Id of current commit
	Id string `yaml:"id" json:"id"`
	// Date of current commit
	Date string `yaml:"date" json:"date"`
	// IdAbbr is abbreviation of id of current commit
	IdAbbr string `yaml:"idAbbr" json:"idAbbr"`
	// Sub is subject of current commit
	Sub string `yaml:"sub" json:"sub"`
	// Committer of current commit
	Committer *Committer `yaml:"committer" json:"committer"`
}

Commit of git from local machine

type Committer added in v1.2.0

type Committer struct {
	// Name of committer
	Name string `yaml:"name" json:"name"`
	// Email of committer
	Email string `yaml:"email" json:"email"`
}

Committer info of current commit

type Git added in v1.2.0

type Git struct {
	// Url of git repo
	Url string `yaml:"url" json:"url"`
	// Branch of git repo
	Branch string `yaml:"branch" json:"branch"`
	// Tag of git repo
	Tag string `yaml:"tag" json:"tag"`
	// Commit info of git repo
	Commit *Commit `yaml:"commit" json:"commit"`
}

Git metadata info on local machine

type RkMeta added in v1.2.0

type RkMeta struct {
	// Name of application
	Name string `json:"name" yaml:"name"`
	// Version of application
	Version string `json:"version" yaml:"version"`
	// Git meta info
	Git *Git `json:"git" yaml:"git"`
}

RkMeta would be extracted by rk cli

func NewRkMeta added in v1.2.3

func NewRkMeta() *RkMeta

NewRkMeta returns empty rk meta instance

Jump to

Keyboard shortcuts

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