...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/cmd/root.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/cmd

     1  // Copyright 2020 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package cmd
    15  
    16  import (
    17  	"fmt"
    18  	"log"
    19  	"os"
    20  
    21  	"github.com/spf13/cobra"
    22  
    23  	cm "github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/common"
    24  )
    25  
    26  // rootCmd represents the base command when called without any subcommands
    27  var rootCmd = &cobra.Command{
    28  	Use:   "chaosctl [command] [options]",
    29  	Short: "Interacting with chaos mesh",
    30  	Long: `
    31  Interacting with chaos mesh
    32  
    33    # show debug info
    34    chaosctl debug networkchaos
    35  
    36    # show logs of all chaos-mesh components
    37    chaosctl logs`,
    38  }
    39  
    40  // Execute adds all child commands to the root command and sets flags appropriately.
    41  // This is called by main.main(). It only needs to happen once to the rootCmd.
    42  func Execute() {
    43  	err := cm.SetupKlog()
    44  	if err != nil {
    45  		log.Fatal("failed to setup klog", err)
    46  	}
    47  	rootLogger, flushFunc, err := cm.NewStderrLogger()
    48  	if err != nil {
    49  		log.Fatal("failed to initialize logger", err)
    50  	}
    51  	if flushFunc != nil {
    52  		defer flushFunc()
    53  	}
    54  	cm.SetupGlobalLogger(rootLogger.WithName("global-logger"))
    55  
    56  	logsCmd, err := NewLogsCmd(rootLogger.WithName("cmd-logs"))
    57  	if err != nil {
    58  		rootLogger.Error(err, "failed to initialize cmd",
    59  			"cmd", "logs",
    60  			"errorVerbose", fmt.Sprintf("%+v", err),
    61  		)
    62  		os.Exit(1)
    63  	}
    64  	rootCmd.AddCommand(logsCmd)
    65  
    66  	debugCommand, err := NewDebugCommand(rootLogger.WithName("cmd-debug"))
    67  	if err != nil {
    68  		rootLogger.Error(err, "failed to initialize cmd",
    69  			"cmd", "debug",
    70  			"errorVerbose", fmt.Sprintf("%+v", err),
    71  		)
    72  		os.Exit(1)
    73  	}
    74  
    75  	rootCmd.AddCommand(debugCommand)
    76  	rootCmd.AddCommand(completionCmd)
    77  	if err := rootCmd.Execute(); err != nil {
    78  		rootLogger.Error(err, "failed to execute cmd",
    79  			"errorVerbose", fmt.Sprintf("%+v", err),
    80  		)
    81  	}
    82  }
    83  
    84  func noCompletions(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    85  	return nil, cobra.ShellCompDirectiveNoFileComp
    86  }
    87