Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix golint errors for k8s.io/cli-runtime/pkg/genericclioptions/ #98003

Merged
merged 1 commit into from Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion hack/.golint_failures
Expand Up @@ -344,7 +344,6 @@ staging/src/k8s.io/apiserver/plugin/pkg/authenticator/token/oidc
staging/src/k8s.io/apiserver/plugin/pkg/authenticator/token/tokentest
staging/src/k8s.io/apiserver/plugin/pkg/authenticator/token/webhook
staging/src/k8s.io/apiserver/plugin/pkg/authorizer/webhook
staging/src/k8s.io/cli-runtime/pkg/genericclioptions
staging/src/k8s.io/cli-runtime/pkg/printers
staging/src/k8s.io/cli-runtime/pkg/resource
staging/src/k8s.io/client-go/discovery
Expand Down
Expand Up @@ -52,6 +52,9 @@ func NewResourceBuilderFlags() *ResourceBuilderFlags {
}
}

// WithFile sets the FileNameFlags.
// If recurse is set, it will process directory recursively. Useful when you want to manage related manifests
// organized within the same directory.
func (o *ResourceBuilderFlags) WithFile(recurse bool, files ...string) *ResourceBuilderFlags {
o.FileNameFlags = &FileNameFlags{
Usage: "identifying the resource.",
Expand All @@ -62,41 +65,49 @@ func (o *ResourceBuilderFlags) WithFile(recurse bool, files ...string) *Resource
return o
}

// WithLabelSelector sets the LabelSelector flag
func (o *ResourceBuilderFlags) WithLabelSelector(selector string) *ResourceBuilderFlags {
o.LabelSelector = &selector
return o
}

// WithFieldSelector sets the FieldSelector flag
func (o *ResourceBuilderFlags) WithFieldSelector(selector string) *ResourceBuilderFlags {
o.FieldSelector = &selector
return o
}

// WithAllNamespaces sets the AllNamespaces flag
func (o *ResourceBuilderFlags) WithAllNamespaces(defaultVal bool) *ResourceBuilderFlags {
o.AllNamespaces = &defaultVal
return o
}

// WithAll sets the All flag
func (o *ResourceBuilderFlags) WithAll(defaultVal bool) *ResourceBuilderFlags {
o.All = &defaultVal
return o
}

// WithLocal sets the Local flag
func (o *ResourceBuilderFlags) WithLocal(defaultVal bool) *ResourceBuilderFlags {
o.Local = &defaultVal
return o
}

// WithScheme sets the Scheme flag
func (o *ResourceBuilderFlags) WithScheme(scheme *runtime.Scheme) *ResourceBuilderFlags {
o.Scheme = scheme
return o
}

// WithLatest sets the Latest flag
func (o *ResourceBuilderFlags) WithLatest() *ResourceBuilderFlags {
o.Latest = true
return o
}

// StopOnError sets the StopOnFirstError flag
func (o *ResourceBuilderFlags) StopOnError() *ResourceBuilderFlags {
o.StopOnFirstError = true
return o
Expand Down
Expand Up @@ -20,7 +20,7 @@ import (
"k8s.io/cli-runtime/pkg/resource"
)

// NewSimpleResourceFinder builds a super simple ResourceFinder that just iterates over the objects you provided
// NewSimpleFakeResourceFinder builds a super simple ResourceFinder that just iterates over the objects you provided
func NewSimpleFakeResourceFinder(infos ...*resource.Info) ResourceFinder {
return &fakeResourceFinder{
Infos: infos,
Expand Down
Expand Up @@ -23,6 +23,7 @@ import (
)

var (
// ErrEmptyConfig is the error message to be displayed if the configuration info is missing or incomplete
ErrEmptyConfig = clientcmd.NewEmptyConfigError(`Missing or incomplete configuration info. Please point to an existing, complete config file:


Expand Down
Expand Up @@ -27,27 +27,39 @@ import (
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

// TestConfigFlags contains clientConfig struct
// and interfaces that implements RESTClientGetter
type TestConfigFlags struct {
clientConfig clientcmd.ClientConfig
discoveryClient discovery.CachedDiscoveryInterface
restMapper meta.RESTMapper
}

// ToRawKubeConfigLoader implements RESTClientGetter
// Returns a clientconfig if it's set
func (f *TestConfigFlags) ToRawKubeConfigLoader() clientcmd.ClientConfig {
if f.clientConfig == nil {
panic("attempt to obtain a test RawKubeConfigLoader with no clientConfig specified")
}
return f.clientConfig
}

// ToRESTConfig implements RESTClientGetter.
// Returns a REST client configuration based on a provided path
// to a .kubeconfig file, loading rules, and config flag overrides.
// Expects the AddFlags method to have been called.
func (f *TestConfigFlags) ToRESTConfig() (*rest.Config, error) {
return f.ToRawKubeConfigLoader().ClientConfig()
}

// ToDiscoveryClient implements RESTClientGetter.
// Returns a CachedDiscoveryInterface
func (f *TestConfigFlags) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
return f.discoveryClient, nil
}

// ToRESTMapper implements RESTClientGetter.
// Returns a mapper.
func (f *TestConfigFlags) ToRESTMapper() (meta.RESTMapper, error) {
if f.restMapper != nil {
return f.restMapper, nil
Expand All @@ -60,21 +72,25 @@ func (f *TestConfigFlags) ToRESTMapper() (meta.RESTMapper, error) {
return nil, fmt.Errorf("no restmapper")
}

// WithClientConfig sets the clientConfig flag
func (f *TestConfigFlags) WithClientConfig(clientConfig clientcmd.ClientConfig) *TestConfigFlags {
f.clientConfig = clientConfig
return f
}

// WithRESTMapper sets the restMapper flag
func (f *TestConfigFlags) WithRESTMapper(mapper meta.RESTMapper) *TestConfigFlags {
f.restMapper = mapper
return f
}

// WithDiscoveryClient sets the discoveryClient flag
func (f *TestConfigFlags) WithDiscoveryClient(c discovery.CachedDiscoveryInterface) *TestConfigFlags {
f.discoveryClient = c
return f
}

// WithNamespace sets the clientConfig flag by modifying delagate and namespace
func (f *TestConfigFlags) WithNamespace(ns string) *TestConfigFlags {
if f.clientConfig == nil {
panic("attempt to obtain a test RawKubeConfigLoader with no clientConfig specified")
Expand All @@ -86,6 +102,7 @@ func (f *TestConfigFlags) WithNamespace(ns string) *TestConfigFlags {
return f
}

// NewTestConfigFlags builds a TestConfigFlags struct to test ConfigFlags
func NewTestConfigFlags() *TestConfigFlags {
return &TestConfigFlags{}
}
Expand Down
Expand Up @@ -14,6 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// Package genericclioptions contains flags which can be added to you command, bound, completed, and produce
// Package genericclioptions contains flags which can be added to your command, bound, completed, and produce
// useful helper functions. Nothing in this package can depend on kube/kube
package genericclioptions // import "k8s.io/cli-runtime/pkg/genericclioptions"
Expand Up @@ -25,6 +25,7 @@ import (
"k8s.io/cli-runtime/pkg/resource"
)

// FileNameFlags are flags for processing files.
// Usage of this struct by itself is discouraged.
// These flags are composed by ResourceBuilderFlags
// which should be used instead.
Expand All @@ -36,6 +37,7 @@ type FileNameFlags struct {
Recursive *bool
}

// ToOptions creates a new FileNameOptions struct and sets FilenameOptions based on FileNameflags
func (o *FileNameFlags) ToOptions() resource.FilenameOptions {
options := resource.FilenameOptions{}

Expand All @@ -56,6 +58,7 @@ func (o *FileNameFlags) ToOptions() resource.FilenameOptions {
return options
}

// AddFlags binds file name flags to a given flagset
func (o *FileNameFlags) AddFlags(flags *pflag.FlagSet) {
if o == nil {
return
Expand Down
Expand Up @@ -24,6 +24,7 @@ import (
"k8s.io/cli-runtime/pkg/printers"
)

// AllowedFormats returns slice of string of allowed JSONYaml printing format
func (f *JSONYamlPrintFlags) AllowedFormats() []string {
if f == nil {
return []string{}
Expand Down
Expand Up @@ -46,6 +46,7 @@ type JSONPathPrintFlags struct {
TemplateArgument *string
}

// AllowedFormats returns slice of string of allowed JSONPath printing format
func (f *JSONPathPrintFlags) AllowedFormats() []string {
formats := make([]string, 0, len(jsonFormats))
for format := range jsonFormats {
Expand Down Expand Up @@ -89,15 +90,15 @@ func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (printers.Resource
if templateFormat == "jsonpath-file" {
data, err := ioutil.ReadFile(templateValue)
if err != nil {
return nil, fmt.Errorf("error reading --template %s, %v\n", templateValue, err)
return nil, fmt.Errorf("error reading --template %s, %v", templateValue, err)
}

templateValue = string(data)
}

p, err := printers.NewJSONPathPrinter(templateValue)
if err != nil {
return nil, fmt.Errorf("error parsing jsonpath %s, %v\n", templateValue, err)
return nil, fmt.Errorf("error parsing jsonpath %s, %v", templateValue, err)
}

allowMissingKeys := true
Expand Down
Expand Up @@ -33,13 +33,18 @@ type KubeTemplatePrintFlags struct {
TemplateArgument *string
}

// AllowedFormats returns slice of string of allowed GoTemplete and JSONPathPrint printing formats
func (f *KubeTemplatePrintFlags) AllowedFormats() []string {
if f == nil {
return []string{}
}
return append(f.GoTemplatePrintFlags.AllowedFormats(), f.JSONPathPrintFlags.AllowedFormats()...)
}

// ToPrinter receives an outputFormat and returns a printer capable of
// handling --template printing.
// Returns false if the specified outputFormat does not match a supported format.
// Supported Format types can be found in pkg/printers/printers.go
func (f *KubeTemplatePrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrinter, error) {
if f == nil {
return nil, NoCompatiblePrinterError{}
Expand Down
Expand Up @@ -35,11 +35,13 @@ type NamePrintFlags struct {
Operation string
}

// Complete sets NamePrintFlags operation flag from sucessTemplate
func (f *NamePrintFlags) Complete(successTemplate string) error {
f.Operation = fmt.Sprintf(successTemplate, f.Operation)
return nil
}

// AllowedFormats returns slice of string of allowed Name printing format
func (f *NamePrintFlags) AllowedFormats() []string {
if f == nil {
return []string{}
Expand Down
Expand Up @@ -27,6 +27,8 @@ import (
"k8s.io/cli-runtime/pkg/printers"
)

// NoCompatiblePrinterError is a struct that contains error information.
// It will be constructed when a invalid printing format is provided
type NoCompatiblePrinterError struct {
OutputFormat *string
AllowedFormats []string
Expand All @@ -43,6 +45,8 @@ func (e NoCompatiblePrinterError) Error() string {
return fmt.Sprintf("unable to match a printer suitable for the output format %q, allowed formats are: %s", output, strings.Join(e.AllowedFormats, ","))
}

// IsNoCompatiblePrinterError returns true if it is a not a compatible printer
// otherwise it will return false
func IsNoCompatiblePrinterError(err error) bool {
if err == nil {
return false
Expand All @@ -69,10 +73,12 @@ type PrintFlags struct {
OutputFlagSpecified func() bool
}

// Complete sets NamePrintFlags operation flag from sucessTemplate
func (f *PrintFlags) Complete(successTemplate string) error {
return f.NamePrintFlags.Complete(successTemplate)
}

// AllowedFormats returns slice of string of allowed JSONYaml/Name/Template printing format
func (f *PrintFlags) AllowedFormats() []string {
ret := []string{}
ret = append(ret, f.JSONYamlPrintFlags.AllowedFormats()...)
Expand All @@ -81,6 +87,10 @@ func (f *PrintFlags) AllowedFormats() []string {
return ret
}

// ToPrinter returns a printer capable of
// handling --output or --template printing.
// Returns false if the specified outputFormat does not match a supported format.
// Supported format types can be found in pkg/printers/printers.go
func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) {
outputFormat := ""
if f.OutputFormat != nil {
Expand Down Expand Up @@ -118,6 +128,8 @@ func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) {
return nil, NoCompatiblePrinterError{OutputFormat: f.OutputFormat, AllowedFormats: f.AllowedFormats()}
}

// AddFlags receives a *cobra.Command reference and binds
// flags related to JSON/Yaml/Name/Template printing to it
func (f *PrintFlags) AddFlags(cmd *cobra.Command) {
f.JSONYamlPrintFlags.AddFlags(cmd)
f.NamePrintFlags.AddFlags(cmd)
Expand Down Expand Up @@ -145,6 +157,7 @@ func (f *PrintFlags) WithTypeSetter(scheme *runtime.Scheme) *PrintFlags {
return f
}

// NewPrintFlags returns a default *PrintFlags
func NewPrintFlags(operation string) *PrintFlags {
outputFormat := ""

Expand Down
Expand Up @@ -21,7 +21,7 @@ import (
"path/filepath"
"strings"

"github.com/evanphx/json-patch"
jsonpatch "github.com/evanphx/json-patch"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

Expand Down Expand Up @@ -74,6 +74,7 @@ func (f *RecordFlags) Complete(cmd *cobra.Command) error {
return nil
}

// CompleteWithChangeCause alters changeCause value with a new cause
func (f *RecordFlags) CompleteWithChangeCause(cause string) error {
if f == nil {
return nil
Expand Down
Expand Up @@ -47,6 +47,7 @@ type GoTemplatePrintFlags struct {
TemplateArgument *string
}

// AllowedFormats returns slice of string of allowed GoTemplatePrint printing format
func (f *GoTemplatePrintFlags) AllowedFormats() []string {
formats := make([]string, 0, len(templateFormats))
for format := range templateFormats {
Expand Down Expand Up @@ -90,15 +91,15 @@ func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (printers.Resour
if templateFormat == "templatefile" || templateFormat == "go-template-file" {
data, err := ioutil.ReadFile(templateValue)
if err != nil {
return nil, fmt.Errorf("error reading --template %s, %v\n", templateValue, err)
return nil, fmt.Errorf("error reading --template %s, %v", templateValue, err)
}

templateValue = string(data)
}

p, err := printers.NewGoTemplatePrinter([]byte(templateValue))
if err != nil {
return nil, fmt.Errorf("error parsing template %s, %v\n", templateValue, err)
return nil, fmt.Errorf("error parsing template %s, %v", templateValue, err)
}

allowMissingKeys := true
Expand Down