...

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  	"os"
    20  
    21  	"github.com/spf13/cobra"
    22  
    23  	cm "github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/common"
    24  	"github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/debug"
    25  	"github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/recover"
    26  	"github.com/chaos-mesh/chaos-mesh/pkg/log"
    27  )
    28  
    29  var managerNamespace, managerSvc string
    30  
    31  // rootCmd represents the base command when called without any subcommands
    32  var rootCmd = &cobra.Command{
    33  	Use:   "chaosctl [command] [options]",
    34  	Short: "Interacting with chaos mesh",
    35  	Long: `
    36  Interacting with chaos mesh
    37  
    38    # show debug info
    39    chaosctl debug networkchaos
    40  
    41    # show logs of all chaos-mesh components
    42    chaosctl logs
    43  
    44    # forcedly recover chaos from pods
    45    chaosctl recover networkchaos pod1 -n test`,
    46  }
    47  
    48  // Execute adds all child commands to the root command and sets flags appropriately.
    49  // This is called by main.main(). It only needs to happen once to the rootCmd.
    50  func Execute() {
    51  	rootLogger, err := log.NewDefaultZapLogger()
    52  	if err != nil {
    53  		cm.PrettyPrint("failed to initialize logger: ", 0, cm.Red)
    54  		cm.PrettyPrint(err.Error(), 1, cm.Red)
    55  		os.Exit(1)
    56  	}
    57  
    58  	rootCmd.PersistentFlags().StringVarP(&managerNamespace, "manager-namespace", "N", "chaos-mesh", "the namespace chaos-controller-manager in")
    59  	rootCmd.PersistentFlags().StringVarP(&managerSvc, "manager-svc", "s", "chaos-mesh-controller-manager", "the service to chaos-controller-manager")
    60  	err = rootCmd.RegisterFlagCompletionFunc("manager-namespace", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    61  		// TODO: list namespaces without ctrlserver
    62  		return nil, cobra.ShellCompDirectiveNoFileComp
    63  	})
    64  	if err != nil {
    65  		cm.PrettyPrint("failed to register completion function for flag 'manager-namespace': ", 0, cm.Red)
    66  		cm.PrettyPrint(err.Error(), 1, cm.Red)
    67  		os.Exit(1)
    68  	}
    69  	err = rootCmd.RegisterFlagCompletionFunc("manager-svc", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    70  		// TODO: list svc without ctrlserver
    71  		return nil, cobra.ShellCompDirectiveNoFileComp
    72  	})
    73  	if err != nil {
    74  		cm.PrettyPrint("failed to register completion function for flag 'manager-svc': ", 0, cm.Red)
    75  		cm.PrettyPrint(err.Error(), 1, cm.Red)
    76  		os.Exit(1)
    77  	}
    78  
    79  	logsCmd, err := NewLogsCmd()
    80  	if err != nil {
    81  		cm.PrettyPrint("failed to initialize cmd: ", 0, cm.Red)
    82  		cm.PrettyPrint("log command: "+err.Error(), 1, cm.Red)
    83  		os.Exit(1)
    84  	}
    85  
    86  	rootCmd.AddCommand(logsCmd)
    87  
    88  	debugCommand, err := NewDebugCommand(rootLogger.WithName("cmd-debug"), map[string]debug.Debug{
    89  		networkChaos: debug.NetworkDebug,
    90  		ioChaos:      debug.IODebug,
    91  		stressChaos:  debug.StressDebug,
    92  		httpChaos:    debug.HTTPDebug,
    93  	})
    94  	if err != nil {
    95  		cm.PrettyPrint("failed to initialize cmd: ", 0, cm.Red)
    96  		cm.PrettyPrint("debug command: "+err.Error(), 1, cm.Red)
    97  		os.Exit(1)
    98  	}
    99  
   100  	recoverCommand, err := NewRecoverCommand(rootLogger.WithName("cmd-recover"), map[string]recover.RecovererBuilder{
   101  		httpChaos:    recover.HTTPRecoverer,
   102  		ioChaos:      recover.IORecoverer,
   103  		stressChaos:  recover.StressRecoverer,
   104  		networkChaos: recover.NetworkRecoverer,
   105  	})
   106  	if err != nil {
   107  		cm.PrettyPrint("failed to initialize cmd: ", 0, cm.Red)
   108  		cm.PrettyPrint("recover command: "+err.Error(), 1, cm.Red)
   109  		os.Exit(1)
   110  	}
   111  
   112  	physicalMachineCommand, err := NewPhysicalMachineCommand()
   113  	if err != nil {
   114  		cm.PrettyPrint("failed to initialize cmd: ", 0, cm.Red)
   115  		cm.PrettyPrint("physicalmachine command: "+err.Error(), 1, cm.Red)
   116  		os.Exit(1)
   117  	}
   118  
   119  	rootCmd.AddCommand(debugCommand)
   120  	rootCmd.AddCommand(recoverCommand)
   121  	rootCmd.AddCommand(completionCmd)
   122  	rootCmd.AddCommand(forwardCmd)
   123  	rootCmd.AddCommand(physicalMachineCommand)
   124  
   125  	if err := rootCmd.Execute(); err != nil {
   126  		cm.PrettyPrint("failed to execute cmd: ", 0, cm.Red)
   127  		cm.PrettyPrint(err.Error(), 1, cm.Red)
   128  		os.Exit(1)
   129  	}
   130  }
   131  
   132  func noCompletions(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
   133  	return nil, cobra.ShellCompDirectiveNoFileComp
   134  }
   135