github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/cmd/virtletctl/virtletctl.go (about)

     1  /*
     2  Copyright 2018 Mirantis
     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  
    17  package main
    18  
    19  import (
    20  	"flag"
    21  	"os"
    22  
    23  	"github.com/renstrom/dedent"
    24  	"github.com/spf13/cobra"
    25  
    26  	"github.com/Mirantis/virtlet/pkg/tools"
    27  )
    28  
    29  func newRootCmd() *cobra.Command {
    30  	// rootCmd represents the base command when called without any subcommands
    31  	cmd := &cobra.Command{
    32  		Use:          "virtletctl",
    33  		Short:        "Virtlet control tool",
    34  		SilenceUsage: true,
    35  		Long: dedent.Dedent(`
    36                          virtletctl provides a number of utilities for Virtet-enabled
    37                          Kubernetes cluster.`),
    38  	}
    39  	cmd.DisableAutoGenTag = true
    40  
    41  	clientCfg := tools.BindFlags(cmd.PersistentFlags())
    42  	// Fix unwanted glog warnings, see
    43  	// https://github.com/kubernetes/kubernetes/issues/17162#issuecomment-225596212
    44  	flag.CommandLine.Parse([]string{})
    45  
    46  	client := tools.NewRealKubeClient(clientCfg)
    47  	cmd.AddCommand(tools.NewVirshCmd(client, os.Stdout))
    48  	cmd.AddCommand(tools.NewSSHCmd(client, os.Stdout, ""))
    49  	cmd.AddCommand(tools.NewVNCCmd(client, os.Stdout, true))
    50  	cmd.AddCommand(tools.NewInstallCmd(cmd, "", ""))
    51  	cmd.AddCommand(tools.NewGenDocCmd(cmd, os.Stdout))
    52  	cmd.AddCommand(tools.NewGenCmd(os.Stdout))
    53  	cmd.AddCommand(tools.NewVersionCommand(client, os.Stdout, nil))
    54  	cmd.AddCommand(tools.NewDiagCommand(client, os.Stdin, os.Stdout))
    55  	cmd.AddCommand(tools.NewValidateCommand(client, os.Stdin))
    56  
    57  	for _, c := range cmd.Commands() {
    58  		c.PreRunE = func(*cobra.Command, []string) error {
    59  			return tools.SetLocalPluginFlags(c)
    60  		}
    61  	}
    62  
    63  	return cmd
    64  }
    65  
    66  func main() {
    67  	cmd := newRootCmd()
    68  	if tools.InPlugin() && len(os.Args) > 1 {
    69  		// in case of a kubectl plugin, all the options are
    70  		// already removed from the command line
    71  		args := []string{os.Args[1]}
    72  		args = append(args, "--")
    73  		args = append(args, os.Args[2:]...)
    74  		cmd.SetArgs(args)
    75  	}
    76  	if err := cmd.Execute(); err != nil {
    77  		os.Exit(1)
    78  	}
    79  }