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

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2021-Present The Jackal Authors
     3  
     4  // Package common handles command configuration across all commands
     5  package common
     6  
     7  import (
     8  	"os"
     9  	"strings"
    10  
    11  	"slices"
    12  
    13  	"github.com/Racer159/jackal/src/config"
    14  	"github.com/spf13/cobra"
    15  )
    16  
    17  var vendorCmds = []string{
    18  	"kubectl",
    19  	"k",
    20  	"syft",
    21  	"sbom",
    22  	"s",
    23  	"k9s",
    24  	"monitor",
    25  	"m",
    26  	"wait-for",
    27  	"wait",
    28  	"w",
    29  	"crane",
    30  	"registry",
    31  	"r",
    32  	"helm",
    33  	"h",
    34  	"yq",
    35  }
    36  
    37  // CheckVendorOnlyFromArgs checks if the command being run is a vendor-only command
    38  func CheckVendorOnlyFromArgs() bool {
    39  	// Check for "jackal tools|t <cmd>" where <cmd> is in the vendorCmd list
    40  	return IsVendorCmd(os.Args, vendorCmds)
    41  }
    42  
    43  // CheckVendorOnlyFromPath checks if the cobra command is a vendor-only command
    44  func CheckVendorOnlyFromPath(cmd *cobra.Command) bool {
    45  	args := strings.Split(cmd.CommandPath(), " ")
    46  	// Check for "jackal tools|t <cmd>" where <cmd> is in the vendorCmd list
    47  	return IsVendorCmd(args, vendorCmds)
    48  }
    49  
    50  // IsVendorCmd checks if the command is a vendor command.
    51  func IsVendorCmd(args []string, vendoredCmds []string) bool {
    52  	if config.ActionsCommandJackalPrefix != "" {
    53  		args = args[1:]
    54  	}
    55  
    56  	if len(args) > 2 {
    57  		if args[1] == "tools" || args[1] == "t" {
    58  			if slices.Contains(vendoredCmds, args[2]) {
    59  				return true
    60  			}
    61  		}
    62  	}
    63  
    64  	return false
    65  }