<commit_before>/*
Copyright 2017 the Heptio Ark contributors.

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 backup

import (
	"bufio"
	"encoding/json"
	"fmt"
	"os"
	"strings"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/types"

	"github.com/heptio/ark/pkg/apis/ark/v1"
	"github.com/heptio/ark/pkg/controller"
	clientset "github.com/heptio/ark/pkg/generated/clientset/versioned"
	kubeutil "github.com/heptio/ark/pkg/util/kube"
	"github.com/heptio/ark/pkg/util/stringslice"
	"github.com/pkg/errors"
	"github.com/spf13/cobra"
	"github.com/spf13/pflag"

	"github.com/heptio/ark/pkg/client"
	"github.com/heptio/ark/pkg/cmd"
)

func NewDeleteCommand(f client.Factory, use string) *cobra.Command {
	o := &DeleteOptions{}

	c := &cobra.Command{
		Use:   fmt.Sprintf("%s NAME", use),
		Short: "Delete a backup",
		Run: func(c *cobra.Command, args []string) {
			cmd.CheckError(o.Validate(c, args, f))
			cmd.CheckError(o.Complete(f, args))
			cmd.CheckError(o.Run())
		},
	}

	o.BindFlags(c.Flags())

	return c
}

type DeleteOptions struct {
	Name    string
	Force   bool
	Confirm bool

	client    clientset.Interface
	namespace string
}

func (o *DeleteOptions) BindFlags(flags *pflag.FlagSet) {
	flags.BoolVar(&o.Force, "force", o.Force, "Forcefully delete the backup, potentially leaving orphaned cloud resources")
	flags.BoolVar(&o.Confirm, "confirm", o.Confirm, "Confirm forceful deletion")
}

func (o *DeleteOptions) Validate(c *cobra.Command, args []string, f client.Factory) error {
	if len(args) != 1 {
		return errors.New("you must specify only one argument, the backup's name")
	}

	kubeClient, err := f.KubeClient()
	if err != nil {
		return err
	}

	serverVersion, err := kubeutil.ServerVersion(kubeClient.Discovery())
	if err != nil {
		return err
	}

	if !serverVersion.AtLeast(controller.MinVersionForDelete) {
		return errors.Errorf("this command requires the Kubernetes server version to be at least %s", controller.MinVersionForDelete)
	}

	return nil
}

func (o *DeleteOptions) Complete(f client.Factory, args []string) error {
	o.Name = args[0]

	var err error
	o.client, err = f.Client()
	if err != nil {
		return nil
	}<commit_msg>Fix incorrect return when err != nil<commit_after>/*
Copyright 2017 the Heptio Ark contributors.

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 backup

import (
	"bufio"
	"encoding/json"
	"fmt"
	"os"
	"strings"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/types"

	"github.com/heptio/ark/pkg/apis/ark/v1"
	"github.com/heptio/ark/pkg/controller"
	clientset "github.com/heptio/ark/pkg/generated/clientset/versioned"
	kubeutil "github.com/heptio/ark/pkg/util/kube"
	"github.com/heptio/ark/pkg/util/stringslice"
	"github.com/pkg/errors"
	"github.com/spf13/cobra"
	"github.com/spf13/pflag"

	"github.com/heptio/ark/pkg/client"
	"github.com/heptio/ark/pkg/cmd"
)

func NewDeleteCommand(f client.Factory, use string) *cobra.Command {
	o := &DeleteOptions{}

	c := &cobra.Command{
		Use:   fmt.Sprintf("%s NAME", use),
		Short: "Delete a backup",
		Run: func(c *cobra.Command, args []string) {
			cmd.CheckError(o.Validate(c, args, f))
			cmd.CheckError(o.Complete(f, args))
			cmd.CheckError(o.Run())
		},
	}

	o.BindFlags(c.Flags())

	return c
}

type DeleteOptions struct {
	Name    string
	Force   bool
	Confirm bool

	client    clientset.Interface
	namespace string
}

func (o *DeleteOptions) BindFlags(flags *pflag.FlagSet) {
	flags.BoolVar(&o.Force, "force", o.Force, "Forcefully delete the backup, potentially leaving orphaned cloud resources")
	flags.BoolVar(&o.Confirm, "confirm", o.Confirm, "Confirm forceful deletion")
}

func (o *DeleteOptions) Validate(c *cobra.Command, args []string, f client.Factory) error {
	if len(args) != 1 {
		return errors.New("you must specify only one argument, the backup's name")
	}

	kubeClient, err := f.KubeClient()
	if err != nil {
		return err
	}

	serverVersion, err := kubeutil.ServerVersion(kubeClient.Discovery())
	if err != nil {
		return err
	}

	if !serverVersion.AtLeast(controller.MinVersionForDelete) {
		return errors.Errorf("this command requires the Kubernetes server version to be at least %s", controller.MinVersionForDelete)
	}

	return nil
}

func (o *DeleteOptions) Complete(f client.Factory, args []string) error {
	o.Name = args[0]

	var err error
	o.client, err = f.Client()
	if err != nil {
		return err
	}
{'fraction_non_alphanumeric': 0.12254810386699047, 'fraction_numerical': 0.007098823089856155, 'mean_word_length': 4.243878550440744, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2824714', 'n_tokens_mistral': 1923, 'n_tokens_neox': 1843, 'n_words': 513}