...

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 2021 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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package cmd
    17  
    18  import (
    19  	"fmt"
    20  	"log"
    21  	"os"
    22  
    23  	"github.com/spf13/cobra"
    24  
    25  	cm "github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/common"
    26  )
    27  
    28  // rootCmd represents the base command when called without any subcommands
    29  var rootCmd = &cobra.Command{
    30  	Use:   "chaosctl [command] [options]",
    31  	Short: "Interacting with chaos mesh",
    32  	Long: `
    33  Interacting with chaos mesh
    34  
    35    # show debug info
    36    chaosctl debug networkchaos
    37  
    38    # show logs of all chaos-mesh components
    39    chaosctl logs`,
    40  }
    41  
    42  // Execute adds all child commands to the root command and sets flags appropriately.
    43  // This is called by main.main(). It only needs to happen once to the rootCmd.
    44  func Execute() {
    45  	err := cm.SetupKlog()
    46  	if err != nil {
    47  		log.Fatal("failed to setup klog", err)
    48  	}
    49  	rootLogger, flushFunc, err := cm.NewStderrLogger()
    50  	if err != nil {
    51  		log.Fatal("failed to initialize logger", err)
    52  	}
    53  	if flushFunc != nil {
    54  		defer flushFunc()
    55  	}
    56  	cm.SetupGlobalLogger(rootLogger.WithName("global-logger"))
    57  
    58  	logsCmd, err := NewLogsCmd(rootLogger.WithName("cmd-logs"))
    59  	if err != nil {
    60  		rootLogger.Error(err, "failed to initialize cmd",
    61  			"cmd", "logs",
    62  			"errorVerbose", fmt.Sprintf("%+v", err),
    63  		)
    64  		os.Exit(1)
    65  	}
    66  	rootCmd.AddCommand(logsCmd)
    67  
    68  	debugCommand, err := NewDebugCommand(rootLogger.WithName("cmd-debug"))
    69  	if err != nil {
    70  		rootLogger.Error(err, "failed to initialize cmd",
    71  			"cmd", "debug",
    72  			"errorVerbose", fmt.Sprintf("%+v", err),
    73  		)
    74  		os.Exit(1)
    75  	}
    76  
    77  	rootCmd.AddCommand(debugCommand)
    78  	rootCmd.AddCommand(completionCmd)
    79  	rootCmd.AddCommand(forwardCmd)
    80  	if err := rootCmd.Execute(); err != nil {
    81  		rootLogger.Error(err, "failed to execute cmd",
    82  			"errorVerbose", fmt.Sprintf("%+v", err),
    83  		)
    84  	}
    85  }
    86  
    87  func noCompletions(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    88  	return nil, cobra.ShellCompDirectiveNoFileComp
    89  }
    90