Skip to content

Commit 9bc97f8

Browse files
Print a warning on an unrecognised CDI hook
Signed-off-by: Carlos Eduardo Arango Gutierrez <[email protected]>
1 parent 6363f07 commit 9bc97f8

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

cmd/nvidia-cdi-hook/main.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package main
1919
import (
2020
"context"
2121
"os"
22+
"strings"
2223

2324
"github.com/sirupsen/logrus"
2425

@@ -72,6 +73,26 @@ func main() {
7273
commands.IssueUnsupportedHookWarning(logger, cmd)
7374
return nil
7475
},
76+
// Handle usage errors to catch unrecognized hooks with flags
77+
OnUsageError: func(ctx context.Context, cmd *cli.Command, err error, isSubcommand bool) error {
78+
// Check if this is a "flag provided but not defined" error
79+
errMsg := err.Error()
80+
if strings.HasPrefix(errMsg, "flag provided but not defined: ") {
81+
// Get the arguments to check if the first one is an unrecognized command
82+
args := cmd.Args().Slice()
83+
if len(args) > 0 {
84+
potentialCmd := args[0]
85+
// Check if it's not a recognized subcommand
86+
if cmd.Command(potentialCmd) == nil {
87+
// This is an unrecognized hook with flags
88+
commands.IssueUnsupportedHookWarning(logger, cmd)
89+
return nil // Suppress the error and exit cleanly
90+
}
91+
}
92+
}
93+
// For other errors, return the error as-is
94+
return err
95+
},
7596
// Define the subcommands
7697
Commands: commands.New(logger),
7798
Flags: []cli.Flag{

0 commit comments

Comments
 (0)