general

package
v2.7.3 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package general provides the general utilities for the client.

Package general provides the general utilities for the client.

Package general provides the general utilities for the client.

Package general provides the general utilities for the client.

Package general provides the general utilities for the client.

Package general provides the general utilities for the client.

Package general provides the general utilities for the client.

Index

Constants

View Source
const (
	// JSONFormat is the json format.
	JSONFormat string = "json"
	// YamlFormat is the yaml format.
	YamlFormat string = "yaml"
	// DefaultFormat is the default format.
	DefaultFormat string = "default"
)
View Source
const (
	// APIURL is the prefix of all API URLs.
	APIURL = "/apis/v2"

	// HealthURL is the URL of health check.
	HealthURL = APIURL + "/healthz"

	// MembersURL is the URL of members.
	MembersURL = APIURL + "/status/members"
	// MemberItemURL is the URL of a member.
	MemberItemURL = APIURL + "/status/members/%s"

	// ObjectsURL is the URL of objects.
	ObjectsURL = APIURL + "/objects"
	// ObjectItemURL is the URL of a object.
	ObjectItemURL = APIURL + "/objects/%s"
	// ObjectKindsURL is the URL of object kinds.
	ObjectKindsURL = APIURL + "/object-kinds"
	// ObjectTemplateURL is the URL of object template.
	ObjectTemplateURL = APIURL + "/objects-yaml/%s/%s"
	// ObjectAPIResources is the URL of object api resources.
	ObjectAPIResources = APIURL + "/object-api-resources"

	// StatusObjectsURL is the URL of status objects.
	StatusObjectsURL = APIURL + "/status/objects"
	// StatusObjectItemURL is the URL of a status object.
	StatusObjectItemURL = APIURL + "/status/objects/%s"

	// WasmCodeURL is the URL of wasm code.
	WasmCodeURL = APIURL + "/wasm/code"
	// WasmDataURL is the URL of wasm data.
	WasmDataURL = APIURL + "/wasm/data/%s/%s"

	// CustomDataKindURL is the URL of custom data kinds.
	CustomDataKindURL = APIURL + "/customdatakinds"
	// CustomDataKindItemURL is the URL of a custom data kind.
	CustomDataKindItemURL = APIURL + "/customdatakinds/%s"
	// CustomDataURL is the URL of custom data.
	CustomDataURL = APIURL + "/customdata/%s"
	// CustomDataItemURL is the URL of a custom data.
	CustomDataItemURL = APIURL + "/customdata/%s/%s"

	// ProfileURL is the URL of profile.
	ProfileURL = APIURL + "/profile"
	// ProfileStartURL is the URL of start profile.
	ProfileStartURL = APIURL + "/profile/start/%s"
	// ProfileStopURL is the URL of stop profile.
	ProfileStopURL = APIURL + "/profile/stop"

	// LogsURL is the URL of logs.
	LogsURL = APIURL + "/logs"
	// LogsLevelURL is the URL of logs level.
	LogsLevelURL = APIURL + "/logs/level"

	// MetricsURL is the URL of metrics.
	MetricsURL = APIURL + "/metrics"

	// HTTPProtocol is prefix for HTTP protocol
	HTTPProtocol = "http://"
	// HTTPSProtocol is prefix for HTTPS protocol
	HTTPSProtocol = "https://"
)

Variables

This section is empty.

Functions

func Capitalize

func Capitalize(str string) string

Capitalize capitalizes the first letter of the string.

func CompareYamlNameKind added in v2.6.1

func CompareYamlNameKind(oldYaml, newYaml string) (*Spec, *Spec, error)

CompareYamlNameKind compares the name and kind of two YAML strings

func CreateExample

func CreateExample(desc, command string) string

CreateExample creates cobra example by using one line example.

func CreateMultiExample

func CreateMultiExample(examples []Example) string

CreateMultiExample creates cobra example by using multiple examples.

func CreateMultiLineExample added in v2.6.1

func CreateMultiLineExample(example string) string

CreateMultiLineExample creates cobra example by using multiple lines.

func DurationMostSignificantUnit

func DurationMostSignificantUnit(d time.Duration) string

DurationMostSignificantUnit returns the most significant unit of the duration.

func ErrorMsg

func ErrorMsg(action CmdType, err error, values ...string) error

ErrorMsg returns the error message.

func ExitWithError

func ExitWithError(err error)

ExitWithError exits with self-defined message not the one of cobra(such as usage).

func ExitWithErrorf

func ExitWithErrorf(format string, a ...interface{})

ExitWithErrorf wraps ExitWithError with format.

func Filter

func Filter[T any](array []T, filter func(value T) bool) []T

Filter filters the array by using filter function.

func Find

func Find[T any](array []T, find func(value T) bool) (*T, bool)

Find finds the first element in the array by using find function.

func GenerateExampleFromChild

func GenerateExampleFromChild(cmd *cobra.Command)

GenerateExampleFromChild generates cobra example from child commands.

func GetHTTPClient

func GetHTTPClient() (*http.Client, error)

GetHTTPClient is used to get HTTP client.

func HandleReqWithStreamResp added in v2.6.1

func HandleReqWithStreamResp(httpMethod string, path string, yamlBody []byte) (io.ReadCloser, error)

func HandleRequest

func HandleRequest(httpMethod string, path string, yamlBody []byte) (body []byte, err error)

HandleRequest used in cmd/client/resources. It will return the response body in yaml or json format.

func InAPIResource

func InAPIResource(arg string, r *api.APIResource) bool

InAPIResource returns true if the arg is in the api resource.

func Infof added in v2.6.3

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

Infof prints the info message.

func MakePath

func MakePath(urlTemplate string, a ...interface{}) string

MakePath is used to make path for given template.

func MakeURL

func MakeURL(path string) (string, error)

MakeURL is used to make URL for given path.

func Map

func Map[T1 any, T2 any](array []T1, mapF func(value T1) T2) []T2

Map maps the array by using map function.

func NewTabWriter

func NewTabWriter() *tabwriter.Writer

NewTabWriter returns a tabwriter.Writer. Remember to call Flush() after using it.

func PrintBody

func PrintBody(body []byte)

PrintBody prints the response body in yaml or json format.

func PrintMapInterface

func PrintMapInterface(maps []map[string]interface{}, fronts []string, backs []string)

PrintMapInterface prints the []map[string]interface{} in yaml format. Specials are the keys print in front of other part. Use "" in specials to print a blank line. For example, if specials is ["name", "kind", "", "filters"] then, "name", "kind" will in group one, and "filters" will in group two, others will in group three.

func PrintTable

func PrintTable(table [][]string)

PrintTable prints the table with empty prefix.

func PrintTableWithPrefix

func PrintTableWithPrefix(table [][]string, prefix string)

PrintTableWithPrefix prints the table with prefix.

func SuccessMsg

func SuccessMsg(action CmdType, values ...string) string

SuccessMsg returns the success message.

func SuccessfulStatusCode

func SuccessfulStatusCode(code int) bool

SuccessfulStatusCode returns true if the status code is successful.

func UnmarshalMapInterface

func UnmarshalMapInterface(body []byte, listBody bool) ([]map[string]interface{}, error)

UnmarshalMapInterface unmarshals the body to []map[string]interface{}.

func Warnf added in v2.6.3

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

Warnf prints the warning message.

func WriteConfig

func WriteConfig(config *Config) error

WriteConfig writes the config to file.

Types

type APIErr

type APIErr struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

APIErr is the standard return of error.

type ArgInfo

type ArgInfo struct {
	Resource string
	Name     string
	Other    string
}

ArgInfo is used to store the information of arguments.

func ParseArgs

func ParseArgs(args []string) *ArgInfo

ParseArgs parses the arguments and returns the ArgInfo.

func (*ArgInfo) ContainName

func (a *ArgInfo) ContainName() bool

ContainName returns true if the arguments contain name.

func (*ArgInfo) ContainOther

func (a *ArgInfo) ContainOther() bool

ContainOther returns true if the arguments contain other.

func (*ArgInfo) ContainResource

func (a *ArgInfo) ContainResource() bool

ContainResource returns true if the arguments contain resource.

type AuthInfo

type AuthInfo struct {
	ClientCertificate     string `json:"client-certificate,omitempty"`
	ClientCertificateData []byte `json:"client-certificate-data,omitempty"`
	ClientKey             string `json:"client-key,omitempty"`
	ClientKeyData         []byte `json:"client-key-data,omitempty"`
	Username              string `json:"username,omitempty"`
	Password              string `json:"password,omitempty"`
}

AuthInfo is the user configuration.

type Cluster

type Cluster struct {
	Server                   string `json:"server"`
	CertificateAuthority     string `json:"certificate-authority,omitempty"`
	CertificateAuthorityData []byte `json:"certificate-authority-data,omitempty"`
}

Cluster is the cluster configuration.

type CmdType

type CmdType string

CmdType is the type of command.

const (
	// GetCmd is the get command.
	GetCmd CmdType = "get"
	// EditCmd is the edit command.
	EditCmd CmdType = "edit"
	// CreateCmd is the create command.
	CreateCmd CmdType = "create"
	// ApplyCmd is the apply command.
	ApplyCmd CmdType = "apply"
	// DeleteCmd is the delete command.
	DeleteCmd CmdType = "delete"
	// DescribeCmd is the describe command.
	DescribeCmd CmdType = "describe"
	// StartCmd is the start command.
	StartCmd CmdType = "start"
	// StopCmd is the stop command.
	StopCmd CmdType = "stop"
)

type Config

type Config struct {
	Kind           string          `json:"kind"`
	Clusters       []NamedCluster  `json:"clusters"`
	AuthInfos      []NamedAuthInfo `json:"users"`
	Contexts       []NamedContext  `json:"contexts"`
	CurrentContext string          `json:"current-context"`
}

Config is the configuration of egctl.

func GetConfig

func GetConfig() (*Config, error)

GetConfig returns the config.

func GetRedactedConfig

func GetRedactedConfig(c *Config) *Config

GetRedactedConfig returns the config with sensitive data redacted.

type Context

type Context struct {
	Cluster  string `json:"cluster"`
	AuthInfo string `json:"user"`
}

Context is the context configuration.

type CurrentConfig

type CurrentConfig struct {
	CurrentContext string        `json:"current-context"`
	Context        NamedContext  `json:"context"`
	Cluster        NamedCluster  `json:"cluster"`
	AuthInfo       NamedAuthInfo `json:"user"`
}

CurrentConfig is config contains current used cluster and user.

func GetCurrentConfig

func GetCurrentConfig() (*CurrentConfig, error)

GetCurrentConfig returns the current config.

func (*CurrentConfig) GetCertificateAuthority

func (c *CurrentConfig) GetCertificateAuthority() string

GetCertificateAuthority returns the current used certificate authority file name.

func (*CurrentConfig) GetCertificateAuthorityData

func (c *CurrentConfig) GetCertificateAuthorityData() []byte

GetCertificateAuthorityData returns the current used certificate authority data.

func (*CurrentConfig) GetClientCertificate

func (c *CurrentConfig) GetClientCertificate() string

GetClientCertificate returns the current used client certificate file name.

func (*CurrentConfig) GetClientCertificateData

func (c *CurrentConfig) GetClientCertificateData() []byte

GetClientCertificateData returns the current used client certificate data.

func (*CurrentConfig) GetClientKey

func (c *CurrentConfig) GetClientKey() string

GetClientKey returns the current used client key file name.

func (*CurrentConfig) GetClientKeyData

func (c *CurrentConfig) GetClientKeyData() []byte

GetClientKeyData returns the current used client key data.

func (*CurrentConfig) GetPassword

func (c *CurrentConfig) GetPassword() string

GetPassword returns the current used password.

func (*CurrentConfig) GetServer

func (c *CurrentConfig) GetServer() string

GetServer returns the current used server.

func (*CurrentConfig) GetUsername

func (c *CurrentConfig) GetUsername() string

GetUsername returns the current used username.

func (*CurrentConfig) UseHTTPS

func (c *CurrentConfig) UseHTTPS() bool

UseHTTPS returns whether the current used server is HTTPS.

type Example

type Example struct {
	Desc    string
	Command string
}

Example is used to create cobra example.

type GlobalFlags

type GlobalFlags struct {
	Server             string
	ForceTLS           bool
	InsecureSkipVerify bool
	OutputFormat       string

	// following are some general flags. Can be used by all commands. But not all commands use them.
	Verbose bool
}

GlobalFlags is the global flags for the whole client.

var CmdGlobalFlags GlobalFlags

CmdGlobalFlags is the singleton of GlobalFlags.

func (*GlobalFlags) DefaultFormat

func (g *GlobalFlags) DefaultFormat() bool

DefaultFormat is the default output format.

type NamedAuthInfo

type NamedAuthInfo struct {
	Name     string   `json:"name"`
	AuthInfo AuthInfo `json:"user"`
}

NamedAuthInfo is the user with name.

type NamedCluster

type NamedCluster struct {
	Name    string  `json:"name"`
	Cluster Cluster `json:"cluster"`
}

NamedCluster is the cluster with name.

type NamedContext

type NamedContext struct {
	Name    string  `json:"name"`
	Context Context `json:"context"`
}

NamedContext is the context with name.

type Spec

type Spec struct {
	Kind string
	Name string
	// contains filtered or unexported fields
}

Spec is the spec of a resource

func GetSpecFromYaml added in v2.6.1

func GetSpecFromYaml(yamlStr string) (*Spec, error)

func (*Spec) Doc

func (s *Spec) Doc() string

type SpecVisitor

type SpecVisitor interface {
	Visit(func(*Spec) error) error
	Close()
}

SpecVisitor walk through multiple specs

func BuildSpecVisitor

func BuildSpecVisitor(yamlFile string, cmd *cobra.Command) SpecVisitor

BuildSpecVisitor builds a SpecVisitor

type YAMLVisitor

type YAMLVisitor interface {
	Visit(func(yamlDoc []byte) error) error
	Close()
}

YAMLVisitor walk through multiple YAML documents

func BuildYAMLVisitor

func BuildYAMLVisitor(yamlFile string, cmd *cobra.Command) YAMLVisitor

BuildYAMLVisitor builds a YAMLVisitor

Jump to

Keyboard shortcuts

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