github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/cmd/tools/wait.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2021-Present The Jackal Authors
     3  
     4  // Package tools contains the CLI commands for Jackal.
     5  package tools
     6  
     7  import (
     8  	"time"
     9  
    10  	"github.com/Racer159/jackal/src/config/lang"
    11  	"github.com/Racer159/jackal/src/pkg/message"
    12  	"github.com/Racer159/jackal/src/pkg/utils"
    13  	"github.com/spf13/cobra"
    14  
    15  	// Import to initialize client auth plugins.
    16  	_ "k8s.io/client-go/plugin/pkg/client/auth"
    17  )
    18  
    19  var (
    20  	waitTimeout   string
    21  	waitNamespace string
    22  )
    23  
    24  var waitForCmd = &cobra.Command{
    25  	Use:     "wait-for { KIND | PROTOCOL } { NAME | SELECTOR | URI } { CONDITION | HTTP_CODE }",
    26  	Aliases: []string{"w", "wait"},
    27  	Short:   lang.CmdToolsWaitForShort,
    28  	Long:    lang.CmdToolsWaitForLong,
    29  	Example: lang.CmdToolsWaitForExample,
    30  	Args:    cobra.MinimumNArgs(1),
    31  	Run: func(_ *cobra.Command, args []string) {
    32  		// Parse the timeout string
    33  		timeout, err := time.ParseDuration(waitTimeout)
    34  		if err != nil {
    35  			message.Fatalf(err, lang.CmdToolsWaitForErrTimeoutString, waitTimeout)
    36  		}
    37  
    38  		kind := args[0]
    39  
    40  		// identifier is optional to allow for commands like `jackal tools wait-for storageclass` without specifying a name.
    41  		identifier := ""
    42  		if len(args) > 1 {
    43  			identifier = args[1]
    44  		}
    45  
    46  		// Condition is optional, default to "exists".
    47  		condition := ""
    48  		if len(args) > 2 {
    49  			condition = args[2]
    50  		}
    51  
    52  		// Execute the wait command.
    53  		if err := utils.ExecuteWait(waitTimeout, waitNamespace, condition, kind, identifier, timeout); err != nil {
    54  			message.Fatal(err, err.Error())
    55  		}
    56  	},
    57  }
    58  
    59  func init() {
    60  	toolsCmd.AddCommand(waitForCmd)
    61  	waitForCmd.Flags().StringVar(&waitTimeout, "timeout", "5m", lang.CmdToolsWaitForFlagTimeout)
    62  	waitForCmd.Flags().StringVarP(&waitNamespace, "namespace", "n", "", lang.CmdToolsWaitForFlagNamespace)
    63  	waitForCmd.Flags().BoolVar(&message.NoProgress, "no-progress", false, lang.RootCmdFlagNoProgress)
    64  }