github.com/kubeshop/testkube@v1.17.23/cmd/tcl/testworkflow-toolkit/commands/root.go (about)

     1  // Copyright 2024 Testkube.
     2  //
     3  // Licensed as a Testkube Pro file under the Testkube Community
     4  // License (the "License"); you may not use this file except in compliance with
     5  // the License. You may obtain a copy of the License at
     6  //
     7  //	https://github.com/kubeshop/testkube/blob/main/licenses/TCL.txt
     8  
     9  package commands
    10  
    11  import (
    12  	"context"
    13  	"fmt"
    14  	"os"
    15  	"os/signal"
    16  	"syscall"
    17  
    18  	"github.com/pkg/errors"
    19  	"github.com/spf13/cobra"
    20  
    21  	"github.com/kubeshop/testkube/cmd/tcl/testworkflow-toolkit/env"
    22  
    23  	"golang.org/x/sync/errgroup"
    24  )
    25  
    26  func init() {
    27  	RootCmd.AddCommand(NewCloneCmd())
    28  	RootCmd.AddCommand(NewExecuteCmd())
    29  	RootCmd.AddCommand(NewArtifactsCmd())
    30  }
    31  
    32  var RootCmd = &cobra.Command{
    33  	Use:   "testworkflow-toolkit",
    34  	Short: "Orchestrating Testkube workflows",
    35  	CompletionOptions: cobra.CompletionOptions{
    36  		DisableDefaultCmd:   true,
    37  		DisableNoDescFlag:   true,
    38  		DisableDescriptions: true,
    39  		HiddenDefaultCmd:    true,
    40  	},
    41  }
    42  
    43  func Execute() {
    44  	// Run services within an errgroup to propagate errors between services.
    45  	g, ctx := errgroup.WithContext(context.Background())
    46  
    47  	// Cancel the errgroup context on SIGINT and SIGTERM,
    48  	// which shuts everything down gracefully.
    49  	// Kill on recurring signal.
    50  	stopSignal := make(chan os.Signal, 1)
    51  	signal.Notify(stopSignal, syscall.SIGINT, syscall.SIGTERM)
    52  	g.Go(func() error {
    53  		select {
    54  		case <-ctx.Done():
    55  			return nil
    56  		case sig := <-stopSignal:
    57  			go func() {
    58  				<-stopSignal
    59  				os.Exit(137)
    60  			}()
    61  			return errors.Errorf("received signal: %v", sig)
    62  		}
    63  	})
    64  
    65  	RootCmd.PersistentFlags().BoolVar(&env.UseProxyValue, "proxy", false, "use Kubernetes proxy for TK access")
    66  
    67  	if err := RootCmd.ExecuteContext(ctx); err != nil {
    68  		fmt.Fprintln(os.Stderr, err)
    69  		os.Exit(1)
    70  	}
    71  }