<commit_before>/*
Copyright 2019 The Kubernetes Authors.

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.
*/

package remove

import (
	"fmt"
	"github.com/spf13/cobra"
	"sigs.k8s.io/kustomize/pkg/commands/kustfile"
	"sigs.k8s.io/kustomize/pkg/fs"
	"sigs.k8s.io/kustomize/pkg/pgmconfig"
	"sigs.k8s.io/kustomize/pkg/types"
	"strings"
)

// kindOfAdd is the kind of metadata being added: label or annotation
type kindOfAdd int

const (
	annotation kindOfAdd = iota
	label
)

func (k kindOfAdd) String() string {
	kinds := [...]string{
		"annotation",
		"label",
	}
	if k < 0 || k > 1 {
		return "Unknown metadatakind"
	}
	return kinds[k]
}

type removeMetadataOptions struct {
	ignore         bool
	metadata       []string
	arrayValidator func([]string) error
	kind           kindOfAdd
}

// newCmdRemoveLabel removes one or more commonAnnotations from the kustomization file.
func newCmdRemoveAnnotation(fSys fs.FileSystem, v func([]string) error) *cobra.Command {
	var o removeMetadataOptions
	o.kind = label
	o.arrayValidator = v
	cmd := &cobra.Command{
		Use:   "annotation",
		Short: "Removes one or more commonAnnotations from " + pgmconfig.KustomizationFileNames[0],
		Example: `
		remove annotation {annotationKey1},{annotationKey2}`,
		RunE: func(cmd *cobra.Command, args []string) error {
			return o.runE(args, fSys, o.removeAnnotations)
		},
	}
	cmd.Flags().BoolVarP(&o.ignore, "ignore-non-existence", "i", false,
		"ignore error if the given label doesn't exist",
	)
	return cmd
}

// newCmdRemoveLabel removes one or more commonLabels from the kustomization file.
func newCmdRemoveLabel(fSys fs.FileSystem, v func([]string) error) *cobra.Command {
	var o removeMetadataOptions
	o.kind = label
	o.arrayValidator = v
	cmd := &cobra.Command{
		Use:   "label",
		Short: "Removes one or more commonLabels from " + pgmconfig.KustomizationFileNames[0],
		Example: `
		remove label {labelKey1},{labelKey2}`,
		RunE: func(cmd *cobra.Command, args []string) error {
			return o.runE(args, fSys, o.removeLabels)
		},
	}
	cmd.Flags().BoolVarP(&o.ignore, "ignore-non-existence", "i", false,
		"ignore error if the given label doesn't exist",
	)
	return cmd
}

func (o *removeMetadataOptions) runE(
	args []string, fSys fs.FileSystem, remover func(*types.Kustomization) error) error {
	err := o.validateAndParse(args)
	if err != nil {
		return err
	}
	kf, err := kustfile.NewKustomizationFile(fSys)
	if err != nil {
		return err
	}
	m, err := kf.Read()
	if err != nil {
		return err
	}
	err = remover(m)
	if err != nil {
		return err
	}
	return kf.Write(m)
}

// validateAndParse validates `remove` commands and parses them into o.metadata
func (o *removeMetadataOptions) validateAndParse(args []string) error {
	if len(args) < 1 {
		return fmt.Errorf("must specify %s", o.kind)
	}
	if len(args) > 1 {
		return fmt.Errorf("%ss must be comma-separated, with no spaces", o.kind)
	}
	m, err := o.convertToArray(args[0])
	if err != nil {
		return err
	}
	if err = o.arrayValidator(m); err != nil {
		return err
	}
	o.metadata = m
	return nil
}

func (o *removeMetadataOptions) convertToArray(arg string) ([]string, error) {
	inputs := strings.Split(arg, ",")
	result := make([]string, 0, len(inputs))

	for _, input := range inputs {
		result = append(result, input)
	}
	return result, nil
}

func (o *removeMetadataOptions) removeAnnotations(m *types.Kustomization) error {
	if m.CommonAnnotations == nil && !o.ignore {
		return fmt.Errorf("commonAnnotations is not defined in kustomization file")<commit_msg>Fix lint<commit_after>/*
Copyright 2019 The Kubernetes Authors.

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.
*/

package remove

import (
	"fmt"
	"github.com/spf13/cobra"
	"sigs.k8s.io/kustomize/pkg/commands/kustfile"
	"sigs.k8s.io/kustomize/pkg/fs"
	"sigs.k8s.io/kustomize/pkg/pgmconfig"
	"sigs.k8s.io/kustomize/pkg/types"
	"strings"
)

// kindOfAdd is the kind of metadata being added: label or annotation
type kindOfAdd int

const (
	annotation kindOfAdd = iota
	label
)

func (k kindOfAdd) String() string {
	kinds := [...]string{
		"annotation",
		"label",
	}
	if k < 0 || k > 1 {
		return "Unknown metadatakind"
	}
	return kinds[k]
}

type removeMetadataOptions struct {
	ignore         bool
	metadata       []string
	arrayValidator func([]string) error
	kind           kindOfAdd
}

// newCmdRemoveLabel removes one or more commonAnnotations from the kustomization file.
func newCmdRemoveAnnotation(fSys fs.FileSystem, v func([]string) error) *cobra.Command {
	var o removeMetadataOptions
	o.kind = label
	o.arrayValidator = v
	cmd := &cobra.Command{
		Use:   "annotation",
		Short: "Removes one or more commonAnnotations from " + pgmconfig.KustomizationFileNames[0],
		Example: `
		remove annotation {annotationKey1},{annotationKey2}`,
		RunE: func(cmd *cobra.Command, args []string) error {
			return o.runE(args, fSys, o.removeAnnotations)
		},
	}
	cmd.Flags().BoolVarP(&o.ignore, "ignore-non-existence", "i", false,
		"ignore error if the given label doesn't exist",
	)
	return cmd
}

// newCmdRemoveLabel removes one or more commonLabels from the kustomization file.
func newCmdRemoveLabel(fSys fs.FileSystem, v func([]string) error) *cobra.Command {
	var o removeMetadataOptions
	o.kind = label
	o.arrayValidator = v
	cmd := &cobra.Command{
		Use:   "label",
		Short: "Removes one or more commonLabels from " + pgmconfig.KustomizationFileNames[0],
		Example: `
		remove label {labelKey1},{labelKey2}`,
		RunE: func(cmd *cobra.Command, args []string) error {
			return o.runE(args, fSys, o.removeLabels)
		},
	}
	cmd.Flags().BoolVarP(&o.ignore, "ignore-non-existence", "i", false,
		"ignore error if the given label doesn't exist",
	)
	return cmd
}

func (o *removeMetadataOptions) runE(
	args []string, fSys fs.FileSystem, remover func(*types.Kustomization) error) error {
	err := o.validateAndParse(args)
	if err != nil {
		return err
	}
	kf, err := kustfile.NewKustomizationFile(fSys)
	if err != nil {
		return err
	}
	m, err := kf.Read()
	if err != nil {
		return err
	}
	err = remover(m)
	if err != nil {
		return err
	}
	return kf.Write(m)
}

// validateAndParse validates `remove` commands and parses them into o.metadata
func (o *removeMetadataOptions) validateAndParse(args []string) error {
	if len(args) < 1 {
		return fmt.Errorf("must specify %s", o.kind)
	}
	if len(args) > 1 {
		return fmt.Errorf("%ss must be comma-separated, with no spaces", o.kind)
	}
	m, err := o.convertToArray(args[0])
	if err != nil {
		return err
	}
	if err = o.arrayValidator(m); err != nil {
		return err
	}
	o.metadata = m
	return nil
}

func (o *removeMetadataOptions) convertToArray(arg string) ([]string, error) {
	inputs := strings.Split(arg, ",")
	result := make([]string, 0, len(inputs))

	for _, input := range inputs {
		if len(input) == 0 {
			return nil, o.makeError(input, "name is empty")
		}
		result = append(result, input)
	}
	return result, nil
}

func (o *removeMetadataOptions) removeAnnotations(m *types.Kustomization) error {
	if m.CommonAnnotations == nil && !o.ignore {
		return fmt.Errorf("commonAnnotations is not defined in kustomization file")
{'fraction_non_alphanumeric': 0.11841613746731416, 'fraction_numerical': 0.006599427219524343, 'mean_word_length': 4.096446700507614, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2482374', 'n_tokens_mistral': 2822, 'n_tokens_neox': 2698, 'n_words': 831}