Documentation ¶
Overview ¶
Package rkcommon defines utility functions for rk series of packages.
Copyright (c) 2021 rookie-ninja ¶
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
- Variables
- func ConvertJSONToMap(str string) map[string]interface{}
- func ConvertStructToBytes(src interface{}) []byte
- func ConvertStructToJSON(src interface{}) string
- func ConvertStructToJSONPretty(src interface{}) string
- func ConvertStructToMap(src interface{}) map[string]interface{}
- func ConvertStructToZapFields(src interface{}) []zap.Field
- func ExtractSchemeFromURL(url string) string
- func FileExists(filePath string) bool
- func GeneralizeMapKeyToString(input interface{}) interface{}
- func GenerateRequestId() string
- func GenerateRequestIdWithPrefix(prefix string) string
- func GetBootConfigOriginal(configFilePath string) map[interface{}]interface{}
- func GetBootConfigOverrides() map[interface{}]interface{}
- func GetBootConfigPath(configFilePath string) string
- func GetDefaultIfEmptyString(origin, def string) string
- func GetEnvValueOrDefault(key, defaultValue string) string
- func GetLocalHostname() string
- func GetLocalIP() string
- func GetLocale() string
- func GetPasswordFromBasicAuthString(basicAuth string) string
- func GetUsernameFromBasicAuthString(basicAuth string) string
- func MatchLocaleWithEnv(locale string) bool
- func MustReadFile(filePath string) []byte
- func OverrideLumberjackConfig(origin *lumberjack.Logger, override *lumberjack.Logger)
- func OverrideMap(src map[interface{}]interface{}, override map[interface{}]interface{})
- func OverrideSlice(src []interface{}, override []interface{})
- func OverrideZapConfig(origin *zap.Config, override *zap.Config)
- func ParseBootConfigOverrides(s string) (map[interface{}]interface{}, error)
- func RandString(n int) string
- func ShutdownWithError(err error)
- func TryReadFile(filePath string) []byte
- func UnmarshalBootConfig(configFilePath string, config interface{})
- type Commit
- type Committer
- type Git
- type RkMeta
Constants ¶
const ( BootConfigPathFlagKey = "rkboot" BootConfigOverrideKey = "rkset" )
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 ¶
var ErrNotList = errors.New("not a list")
ErrNotList indicates that a non-list was treated as a list.
var ( // GlobalFlags will read pflags passed while starting main entry GlobalFlags *pflag.FlagSet )
Functions ¶
func ConvertJSONToMap ¶
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 ¶
ConvertStructToZapFields convert struct to zap fields. Return empty zap.Field array if input parameter is nil.
func ExtractSchemeFromURL ¶ added in v1.0.8
ExtractSchemeFromURL extract scheme from endpoint
func FileExists ¶
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 ¶
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 ¶
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 ¶
GetDefaultIfEmptyString returns default value if original string is empty.
func GetEnvValueOrDefault ¶
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
GetPasswordFromBasicAuthString extract password from basic auth formed as <username>:<password>
func GetUsernameFromBasicAuthString ¶ added in v1.0.8
GetUsernameFromBasicAuthString extract username from basic auth formed as <username>:<password>
func MatchLocaleWithEnv ¶ added in v1.0.8
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
- region: Please see AWS web site: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html Environment variable: REGION Eg: us-east Wildcard: supported
- az: Availability zone, please see AWS web site for details: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html Environment variable: AZ Eg: us-east-1 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 ¶
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 ¶
OverrideZapConfig overrides zap config. This function will override fields of non empty and non-nil.
func ParseBootConfigOverrides ¶
ParseBootConfigOverrides parses a set line.
A set line is of the form name1=value1,name2=value2
func TryReadFile ¶ added in v1.2.0
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