github.com/inspektor-gadget/inspektor-gadget@v0.28.1/cmd/ig/main.go (about)

     1  // Copyright 2019-2024 The Inspektor Gadget 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  package main
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  
    21  	log "github.com/sirupsen/logrus"
    22  	"github.com/spf13/cobra"
    23  
    24  	// Import this early to set the enrivonment variable before any other package is imported
    25  	_ "github.com/inspektor-gadget/inspektor-gadget/pkg/environment/local"
    26  	"github.com/inspektor-gadget/inspektor-gadget/pkg/operators"
    27  	ocihandler "github.com/inspektor-gadget/inspektor-gadget/pkg/operators/oci-handler"
    28  
    29  	"github.com/inspektor-gadget/inspektor-gadget/cmd/common"
    30  	"github.com/inspektor-gadget/inspektor-gadget/cmd/common/image"
    31  	commonutils "github.com/inspektor-gadget/inspektor-gadget/cmd/common/utils"
    32  	"github.com/inspektor-gadget/inspektor-gadget/cmd/ig/containers"
    33  	"github.com/inspektor-gadget/inspektor-gadget/pkg/runtime/local"
    34  	"github.com/inspektor-gadget/inspektor-gadget/pkg/utils/experimental"
    35  	"github.com/inspektor-gadget/inspektor-gadget/pkg/utils/host"
    36  
    37  	// This is a blank include that actually imports all gadgets
    38  	// TODO: traceloop is imported separately because it is not in all-gadgets
    39  	_ "github.com/inspektor-gadget/inspektor-gadget/pkg/all-gadgets"
    40  	_ "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/traceloop/tracer"
    41  
    42  	// Another blank import for the used operator
    43  	_ "github.com/inspektor-gadget/inspektor-gadget/pkg/operators/btfgen"
    44  	_ "github.com/inspektor-gadget/inspektor-gadget/pkg/operators/ebpf"
    45  	_ "github.com/inspektor-gadget/inspektor-gadget/pkg/operators/formatters"
    46  	_ "github.com/inspektor-gadget/inspektor-gadget/pkg/operators/localmanager"
    47  	_ "github.com/inspektor-gadget/inspektor-gadget/pkg/operators/prometheus"
    48  	_ "github.com/inspektor-gadget/inspektor-gadget/pkg/operators/socketenricher"
    49  	_ "github.com/inspektor-gadget/inspektor-gadget/pkg/operators/uidgidresolver"
    50  )
    51  
    52  func main() {
    53  	if experimental.Enabled() {
    54  		log.Info("Experimental features enabled")
    55  	}
    56  
    57  	rootCmd := &cobra.Command{
    58  		Use:   "ig",
    59  		Short: "Collection of gadgets for containers",
    60  	}
    61  	common.AddVerboseFlag(rootCmd)
    62  
    63  	host.AddFlags(rootCmd)
    64  
    65  	rootCmd.AddCommand(
    66  		containers.NewListContainersCmd(),
    67  		common.NewVersionCmd(),
    68  	)
    69  
    70  	// evaluate flags early; this will make sure that flags for host are evaluated before
    71  	// calling host.Init()
    72  	err := commonutils.ParseEarlyFlags(rootCmd, os.Args[1:])
    73  	if err != nil {
    74  		// Analogous to cobra error message
    75  		fmt.Fprintf(os.Stderr, "Error: %v\n", err)
    76  		os.Exit(1)
    77  	}
    78  
    79  	runtime := local.New()
    80  	hiddenColumnTags := []string{"kubernetes"}
    81  	common.AddCommandsFromRegistry(rootCmd, runtime, hiddenColumnTags)
    82  
    83  	operators.RegisterDataOperator(ocihandler.OciHandler)
    84  
    85  	rootCmd.AddCommand(newDaemonCommand(runtime))
    86  	rootCmd.AddCommand(image.NewImageCmd())
    87  	rootCmd.AddCommand(common.NewLoginCmd())
    88  	rootCmd.AddCommand(common.NewLogoutCmd())
    89  	rootCmd.AddCommand(common.NewRunCommand(rootCmd, runtime, hiddenColumnTags))
    90  
    91  	if err := rootCmd.Execute(); err != nil {
    92  		os.Exit(1)
    93  	}
    94  }