util

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2022 License: Apache-2.0 Imports: 20 Imported by: 70

Documentation

Overview

Copyright © 2019 Portworx

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	FORMAT_WIDE = "wide"
	FORMAT_JSON = "json"
	FORMAT_YAML = "yaml"
)
View Source
const (
	EvInKubectlPluginMode      = "PXC_KUBECTL_PLUGIN_MODE"
	EvPxcToken                 = "PXC_TOKEN"
	EvPortworxServiceNamespace = "PXC_PORTWORX_SERVICE_NAMESPACE"
	EvPortworxServiceName      = "PXC_PORTWORX_SERVICE_NAME"
	EvPortworxServicePort      = "PXC_PORTWORX_SERVICE_PORT"
)
View Source
const (
	DefaultPort = "9020"
	TimeFormat  = "Jan 2 15:04:05 UTC 2006"
)
View Source
const (
	Ki = 1024
	Mi = 1024 * Ki
	Gi = 1024 * Mi
	Ti = 1024 * Gi
)

Variables

View Source
var (
	// Stdout points to the output buffer to send screen output
	Stdout io.Writer = os.Stdout
	// Stderr points to the output buffer to send errors to the screen
	Stderr io.Writer = os.Stderr
)
View Source
var (
	ErrInvalidEndpoint = errors.New("Invalid Endpoint")
)

Functions

func AddArray added in v0.21.3

func AddArray(t *tabby.Tabby, name string, strArr []string)

Adds a full array to tabby. One element per line

func AddMap added in v0.21.3

func AddMap(t *tabby.Tabby, name string, strMap map[string]string)

Adds a full map to tabby. One key value pair per line

func CommaStringToStringMap

func CommaStringToStringMap(s string) (map[string]string, error)

CommaStringToStringMap returns a string map composed of the k=v comma separated values in the string

func Eprintf

func Eprintf(format string, args ...interface{})

Eprintf prints the errors to the output buffer Stderr. It is equal to fmt.Fprintf(util.Stderr, format, args)

func FixCommaBasedStringSliceInput added in v0.22.0

func FixCommaBasedStringSliceInput(params, args []string) []string

FixCommaBasedStringSliceInput fixes params by combining successive values such that they are recovered due to strings split on a comma during CLI parsing.

Background: Cobra CLI framework splits the input values on a string slice based on comma. https://github.com/spf13/pflag/blob/9a97c102cda95a86cec2345a6f09f55a939babf5/string_slice.go#L85 Therefore, a custom formatted input is unable to use comma in the format. This command joins the split components.

Recommendation: Do not use comma in the value to avoid use of such custom parsing logic.

func FromError added in v0.28.0

func FromError(err error) *status.Status

func GetAclFromString added in v0.21.4

func GetAclFromString(s string) (string, api.Ownership_AccessType, error)

GetAclFromString takes values like group1:r or group2:w and breaks them returning the group name and the access type.

func GetAclMapFromString added in v0.21.4

func GetAclMapFromString(s string) (map[string]api.Ownership_AccessType, error)

GetAclMapFromString takes a comma separated string of acl names and their types, and returns a map of the names to their access types

func GetFormattedOutput added in v0.21.3

func GetFormattedOutput(in FormatOutput) (string, error)

GetFormattedOutput returns the formatted output

func GetRandomName added in v0.21.3

func GetRandomName(prefix string) string

Generate random string with the given prefix, by appending random numbers.

func InKubectlPluginMode added in v0.23.0

func InKubectlPluginMode() bool

InKubectlPluginMode returns true if running as a plugin to kubectl

func IsErrorNotFound added in v0.28.0

func IsErrorNotFound(err error) bool

IsErrorNotFound returns if the given error is due to not found

func IsErrorPermissionDenied added in v0.28.0

func IsErrorPermissionDenied(err error) bool

func IsFileExists added in v0.21.3

func IsFileExists(fileName string) bool

IsFileExists returns true if the file exists else false

func ListContains

func ListContains(list []string, s string) bool

ListContains returns true when string s is found in the list

func ListContainsSubString added in v0.21.3

func ListContainsSubString(list []string, s string) bool

ListContains returns true when string s is found in the list

func ListHaveMatch

func ListHaveMatch(list, match []string) (string, bool)

ListsHaveMatch returns the string and true if any one string is found in both lists

func ListMatchGlob added in v0.31.1

func ListMatchGlob(rules []string, s string) bool

ListMatchGlob returns true if the string matches any of the rules

func MatchGlob added in v0.31.1

func MatchGlob(rule, s string) bool

MatchGlob determines if the rules apply to string s rule can be: '*' - match all '*xxx' - ends with xxx 'xxx*' - starts with xxx '*xxx*' - contains xxx

func NewTabby

func NewTabby() *tabby.Tabby

NewTabby is used to return a tabbing object set to the value of Stdout in the util package

func PrintFormatted added in v0.21.3

func PrintFormatted(in FormatOutput) error

Print the formatted output to stdout In case of any error, just return the error

func PrintJson

func PrintJson(obj interface{})

PrintJson prints the object to json to Stdout

func PrintPxErrorMessagef

func PrintPxErrorMessagef(err error, format string, args ...string)

PrintPxErrorMessagef prints the Portworx error message to Stderr

func PrintYaml

func PrintYaml(obj interface{})

PrintYaml prints the object to yaml to Stdout

func Printf

func Printf(format string, args ...interface{})

Printf is just like fmt.Printf except that it send the output to Stdout. It is equal to fmt.Fprintf(util.Stdout, format, args)

func PxError

func PxError(err error) error

PxError extracts and returns the message found in the gRPC error status

func PxErrorMessage

func PxErrorMessage(err error, msg string) error

PxErrorMessage returns an error composed of the gRPC error status message and the message provided.

func PxErrorMessagef

func PxErrorMessagef(err error, format string, args ...string) error

PxErrorMessagef is like PxErrorMessage, but also adds formatted string support

func RectifyErrorMessage added in v0.28.0

func RectifyErrorMessage(msg string) string

RectifyErrorMessage changes cryptic messages

func SdkStatusToPrettyString added in v0.22.0

func SdkStatusToPrettyString(status api.Status) string

SdkStatusToPrettyString returns a human readable version of the Sdk Status

func StringContainsAnyFromList added in v0.21.3

func StringContainsAnyFromList(s string, list []string) bool

func StringMapToCommaString

func StringMapToCommaString(labels map[string]string) string

StringMapToCommaString returns a comma separated k=v as a single string

func ToJson added in v0.21.3

func ToJson(obj interface{}) (string, error)

ToJson returns the json representation of obj

func ToYaml added in v0.21.3

func ToYaml(obj interface{}) (string, error)

ToYaml returns the yaml representation of obj

func ValidateEndpoint added in v0.21.3

func ValidateEndpoint(endpoint string) (string, error)

ValidateEndpoint will valid whether given endpoint is a valid. Following validation will be done on the endpoint: 1. Check whether the host part (IP or hostname) is valid 2. Check whether port number is present in the endpoint, If not add Default_port.

func WaitFor

func WaitFor(timeout time.Duration, period time.Duration, f func() (bool, error)) error

WaitFor() waits until f() returns false or err != nil f() returns <wait as bool, or err>.

Types

type BaseFormatOutput added in v0.21.3

type BaseFormatOutput struct {
	FormatType string
}

func (*BaseFormatOutput) DefaultFormat added in v0.21.3

func (bfo *BaseFormatOutput) DefaultFormat() (string, error)

DefaultFormat just returns the Desc

func (*BaseFormatOutput) GetFormat added in v0.21.3

func (bfo *BaseFormatOutput) GetFormat() string

GetFormat returns the format type set

func (*BaseFormatOutput) JsonFormat added in v0.21.3

func (bfo *BaseFormatOutput) JsonFormat() (string, error)

JsonFormat just returns the DefaultFormat

func (*BaseFormatOutput) SetFormat added in v0.21.3

func (bfo *BaseFormatOutput) SetFormat(typeOfOutput string)

SetFormat takes in as input the type of Formatting needed. Currently recoganized values are "wide", "json" and "yaml". Any other string including "" will end up with DefaultFormat.

func (*BaseFormatOutput) WideFormat added in v0.21.3

func (bfo *BaseFormatOutput) WideFormat() (string, error)

WideFormat just returns the DefaultFormat

func (*BaseFormatOutput) YamlFormat added in v0.21.3

func (bfo *BaseFormatOutput) YamlFormat() (string, error)

YamlFormat just returns the YamlFormat

type DefaultFormatOutput added in v0.21.3

type DefaultFormatOutput struct {
	BaseFormatOutput `json:"-" yaml:"-"`
	Cmd              string   `json:"cmd,omitempty" yaml:"cmd,omitempty"`
	Desc             string   `json:"desc,omitempty" yaml:"desc,omitempty"`
	Id               []string `json:"id,omitempty" yaml:"id,omitempty"`
}

func (*DefaultFormatOutput) DefaultFormat added in v0.21.3

func (dfo *DefaultFormatOutput) DefaultFormat() (string, error)

DefaultFormat returns the Desc

func (*DefaultFormatOutput) JsonFormat added in v0.21.3

func (dfo *DefaultFormatOutput) JsonFormat() (string, error)

JsonFormat returns the object in json format

func (*DefaultFormatOutput) WideFormat added in v0.21.3

func (dfo *DefaultFormatOutput) WideFormat() (string, error)

WideFormat returns the DefaultFormat

func (*DefaultFormatOutput) YamlFormat added in v0.21.3

func (dfo *DefaultFormatOutput) YamlFormat() (string, error)

YamlFormat returns the object in yaml format

type FormatOutput added in v0.21.3

type FormatOutput interface {
	// SetFormat takes in as input the type of Formatting needed.
	// Currently recoganized values are "wide", "json" and "yaml".
	// Any other string including "" will end up with DefaultFormat.
	SetFormat(typeOfFormatting string)

	// GetFormat returns the format type set
	GetFormat() string

	// DefaultFormat formats the output as a regular string
	// This is called when either "wide", "yaml" or "json" is not set
	DefaultFormat() (string, error)

	// WideFormat formats the object in the "wide" format
	WideFormat() (string, error)

	// YamlFormat formats the object in the "yaml" format
	YamlFormat() (string, error)

	// JsonFormat formats the object in the "json" format
	JsonFormat() (string, error)
}

FormatOutput is the interface used to ensure proper formatting

type SigIntManager added in v0.29.0

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

func NewSigIntManager added in v0.29.0

func NewSigIntManager(handler func()) *SigIntManager

func (*SigIntManager) Start added in v0.29.0

func (s *SigIntManager) Start() error

func (*SigIntManager) Stop added in v0.29.0

func (s *SigIntManager) Stop() error

Jump to

Keyboard shortcuts

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