Skip to content

Commit 28c5e29

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

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

cmd/nvidia-cdi-hook/main.go

Lines changed: 30 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,35 @@ func main() {
7273
commands.IssueUnsupportedHookWarning(logger, cmd)
7374
return nil
7475
},
76+
// Handle usage errors to catch unrecognized hooks
77+
OnUsageError: func(ctx context.Context, cmd *cli.Command, err error, isSubcommand bool) error {
78+
errMsg := err.Error()
79+
80+
// Check if this is a "No help topic for" error (unrecognized
81+
// subcommand without flags)
82+
if strings.HasPrefix(errMsg, "No help topic for") {
83+
commands.IssueUnsupportedHookWarning(logger, cmd)
84+
return nil // Suppress the error and exit cleanly
85+
}
86+
87+
// Check if this is a "flag provided but not defined: -" error
88+
// (unrecognized subcommand with flags)
89+
if strings.HasPrefix(errMsg, "flag provided but not defined: -") {
90+
// Get the arguments to check if the first one is an unrecognized command
91+
args := cmd.Args().Slice()
92+
if len(args) > 0 {
93+
potentialCmd := args[0]
94+
// Check if it's not a recognized subcommand
95+
if cmd.Command(potentialCmd) == nil {
96+
// This is an unrecognized hook with flags
97+
commands.IssueUnsupportedHookWarning(logger, cmd)
98+
return nil // Suppress the error and exit cleanly
99+
}
100+
}
101+
}
102+
// For other errors, return the error as-is
103+
return err
104+
},
75105
// Define the subcommands
76106
Commands: commands.New(logger),
77107
Flags: []cli.Flag{

0 commit comments

Comments
 (0)