util

package
v0.21.4 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2019 License: Apache-2.0 Imports: 14 Imported by: 0

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.

Index

Constants

View Source
const (
	FORMAT_WIDE = "wide"
	FORMAT_JSON = "json"
	FORMAT_YAML = "yaml"
)
View Source
const (
	DefaultPort = "9020"
	TimeFormat  = "Jan 2 15:04:05 UTC 2006"
)

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 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 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 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 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

Jump to

Keyboard shortcuts

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