<commit_before>// Copyright 2019 Google LLC
//
// 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 run

import (
	"bytes"
	"flag"
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"

	kptcommands "github.com/GoogleContainerTools/kpt/commands"
	"github.com/GoogleContainerTools/kpt/internal/cmdcomplete"
	"github.com/GoogleContainerTools/kpt/internal/docs/generated/overview"
	"github.com/GoogleContainerTools/kpt/internal/kptfile"
	"github.com/GoogleContainerTools/kpt/internal/util/cfgflags"
	"github.com/GoogleContainerTools/kpt/internal/util/cmdutil"
	kptopenapi "github.com/GoogleContainerTools/kpt/internal/util/openapi"
	"github.com/spf13/cobra"
	"k8s.io/cli-runtime/pkg/genericclioptions"
	"k8s.io/kubectl/pkg/cmd/util"
	"sigs.k8s.io/cli-utils/pkg/util/factory"
	"sigs.k8s.io/kustomize/cmd/config/ext"
	"sigs.k8s.io/kustomize/kyaml/commandutil"
	"sigs.k8s.io/kustomize/kyaml/openapi"
)

var pgr []string

func GetMain() *cobra.Command {
	os.Setenv(commandutil.EnableAlphaCommmandsEnvName, "true")
	installComp := false
	cmd := &cobra.Command{
		Use:     "kpt",
		Short:   overview.ReferenceShort,
		Long:    overview.ReferenceLong,
		Example: overview.ReferenceExamples,
		RunE: func(cmd *cobra.Command, args []string) error {
			if installComp {
				os.Setenv("COMP_INSTALL", "1")
				os.Setenv("COMP_YES", "1")
				fmt.Fprint(cmd.OutOrStdout(), "Installing shell completion...\n")
				fmt.Fprint(cmd.OutOrStdout(),
					"This will add 'complete -C /Users/$USER/go/bin/kpt kpt' to "+
						".bashrc, .bash_profile, etc\n")
				fmt.Fprint(cmd.OutOrStdout(), "Run `COMP_INSTALL=0 kpt` to uninstall.\n")
			}
			// Complete exits if it is called in completion mode, otherwise it is a no-op
			cmdcomplete.Complete(cmd, false, nil).Complete("kpt")

			h, err := cmd.Flags().GetBool("help")
			if err != nil {
				return err
			}
			if h {
				return cmd.Help()
			}
			return cmd.Usage()
		},
	}

	f := newFactory(cmd)

	cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
		// register function to use Kptfile for OpenAPI
		ext.GetOpenAPIFile = func(args []string) (s string, err error) {
			return filepath.Join(args[0], kptfile.KptFileName), nil
		}
		err := kptopenapi.ConfigureOpenAPI(f, cmdutil.K8sSchemaSource, cmdutil.K8sSchemaPath)
		if err != nil {
			return err
		}

		// add openAPI definitions from Kptfile to configured openAPI
		if addErr := openapi.AddSchemaFromFile(filepath.Join(args[0], kptfile.KptFileName)); addErr != nil {
			// do not throw error if schema doesn't exist or not readable from Kptfile
			return nil
		}
		return nil
	}

	cmd.Flags().BoolVar(&installComp, "install-completion", false,
		"install shell completion")
	// this command will be invoked by the shell-completion code
	cmd.AddCommand(&cobra.Command{
		Use:           "kpt",
		Hidden:        true,
		SilenceErrors: true,
		SilenceUsage:  true,
		Run: func(cmd *cobra.Command, args []string) {
			// Complete exits if it is called in completion mode, otherwise it is a no-op<commit_msg>Check that we have the path argument before reading Kptfile<commit_after>// Copyright 2019 Google LLC
//
// 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 run

import (
	"bytes"
	"flag"
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"

	kptcommands "github.com/GoogleContainerTools/kpt/commands"
	"github.com/GoogleContainerTools/kpt/internal/cmdcomplete"
	"github.com/GoogleContainerTools/kpt/internal/docs/generated/overview"
	"github.com/GoogleContainerTools/kpt/internal/kptfile"
	"github.com/GoogleContainerTools/kpt/internal/util/cfgflags"
	"github.com/GoogleContainerTools/kpt/internal/util/cmdutil"
	kptopenapi "github.com/GoogleContainerTools/kpt/internal/util/openapi"
	"github.com/spf13/cobra"
	"k8s.io/cli-runtime/pkg/genericclioptions"
	"k8s.io/kubectl/pkg/cmd/util"
	"sigs.k8s.io/cli-utils/pkg/util/factory"
	"sigs.k8s.io/kustomize/cmd/config/ext"
	"sigs.k8s.io/kustomize/kyaml/commandutil"
	"sigs.k8s.io/kustomize/kyaml/openapi"
)

var pgr []string

func GetMain() *cobra.Command {
	os.Setenv(commandutil.EnableAlphaCommmandsEnvName, "true")
	installComp := false
	cmd := &cobra.Command{
		Use:     "kpt",
		Short:   overview.ReferenceShort,
		Long:    overview.ReferenceLong,
		Example: overview.ReferenceExamples,
		RunE: func(cmd *cobra.Command, args []string) error {
			if installComp {
				os.Setenv("COMP_INSTALL", "1")
				os.Setenv("COMP_YES", "1")
				fmt.Fprint(cmd.OutOrStdout(), "Installing shell completion...\n")
				fmt.Fprint(cmd.OutOrStdout(),
					"This will add 'complete -C /Users/$USER/go/bin/kpt kpt' to "+
						".bashrc, .bash_profile, etc\n")
				fmt.Fprint(cmd.OutOrStdout(), "Run `COMP_INSTALL=0 kpt` to uninstall.\n")
			}
			// Complete exits if it is called in completion mode, otherwise it is a no-op
			cmdcomplete.Complete(cmd, false, nil).Complete("kpt")

			h, err := cmd.Flags().GetBool("help")
			if err != nil {
				return err
			}
			if h {
				return cmd.Help()
			}
			return cmd.Usage()
		},
	}

	f := newFactory(cmd)

	cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
		// register function to use Kptfile for OpenAPI
		ext.GetOpenAPIFile = func(args []string) (s string, err error) {
			return filepath.Join(args[0], kptfile.KptFileName), nil
		}
		err := kptopenapi.ConfigureOpenAPI(f, cmdutil.K8sSchemaSource, cmdutil.K8sSchemaPath)
		if err != nil {
			return err
		}

		if len(args) > 0 {
			// add openAPI definitions from Kptfile to configured openAPI
			if addErr := openapi.AddSchemaFromFile(filepath.Join(args[0], kptfile.KptFileName)); addErr != nil {
				// do not throw error if schema doesn't exist or not readable from Kptfile
				return nil
			}
		}
		return nil
	}

	cmd.Flags().BoolVar(&installComp, "install-completion", false,
		"install shell completion")
	// this command will be invoked by the shell-completion code
	cmd.AddCommand(&cobra.Command{
		Use:           "kpt",
		Hidden:        true,
		SilenceErrors: true,
		SilenceUsage:  true,
		Run: func(cmd *cobra.Command, args []string) {
			// Complete exits if it is called in completion mode, otherwise it is a no-op
{'fraction_non_alphanumeric': 0.126051598429613, 'fraction_numerical': 0.006590016825574874, 'mean_word_length': 3.73953488372093, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '555820', 'n_tokens_mistral': 2645, 'n_tokens_neox': 2422, 'n_words': 619}