volcano.sh/volcano@v1.9.0/cmd/cli/vcctl.go (about)

     1  /*
     2  Copyright 2017 The Volcano Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  package main
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  	"time"
    22  
    23  	"github.com/spf13/cobra"
    24  	"github.com/spf13/pflag"
    25  	"k8s.io/apimachinery/pkg/util/wait"
    26  	_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
    27  	"k8s.io/klog/v2"
    28  
    29  	"volcano.sh/volcano/pkg/version"
    30  )
    31  
    32  var logFlushFreq = pflag.Duration("log-flush-frequency", 5*time.Second, "Maximum number of seconds between log flushes")
    33  
    34  func main() {
    35  	// flag.InitFlags()
    36  
    37  	klog.InitFlags(nil)
    38  
    39  	// The default klog flush interval is 30 seconds, which is frighteningly long.
    40  	go wait.Until(klog.Flush, *logFlushFreq, wait.NeverStop)
    41  	defer klog.Flush()
    42  
    43  	rootCmd := cobra.Command{
    44  		Use: "vcctl",
    45  	}
    46  
    47  	// tell Cobra not to provide the default completion command
    48  	rootCmd.CompletionOptions.DisableDefaultCmd = true
    49  
    50  	rootCmd.AddCommand(buildJobCmd())
    51  	rootCmd.AddCommand(buildQueueCmd())
    52  	rootCmd.AddCommand(versionCommand())
    53  
    54  	if err := rootCmd.Execute(); err != nil {
    55  		fmt.Printf("Failed to execute command: %v\n", err)
    56  		os.Exit(2)
    57  	}
    58  }
    59  
    60  func checkError(cmd *cobra.Command, err error) {
    61  	if err != nil {
    62  		msg := "Failed to"
    63  
    64  		// Ignore the root command.
    65  		for cur := cmd; cur.Parent() != nil; cur = cur.Parent() {
    66  			msg += fmt.Sprintf(" %s", cur.Name())
    67  		}
    68  
    69  		fmt.Printf("%s: %v\n", msg, err)
    70  		os.Exit(2)
    71  	}
    72  }
    73  
    74  var versionExample = `vcctl version`
    75  
    76  func versionCommand() *cobra.Command {
    77  	var command = &cobra.Command{
    78  		Use:     "version",
    79  		Short:   "Print the version information",
    80  		Long:    "Print the version information",
    81  		Example: versionExample,
    82  		Run: func(cmd *cobra.Command, args []string) {
    83  			version.PrintVersionAndExit()
    84  		},
    85  	}
    86  	return command
    87  }