Skip to content

Commit

Permalink
fix(completion): Fix completion in deprecated projects
Browse files Browse the repository at this point in the history
Currently, when initializing in a project with deprecated plugins
the kubebuilder cli will immediately output a deprecation notice to
stdout before even parsing the command. While this is acceptable and
even desirable for interactive usage of the cli, it will "randomly"
(i.e. dependent on $PWD and not on the command) break automated usage of
the kubebuilder output. This, again is fine if kubebuilder makes no
guarantees of stable machine readable output, but this does not hold for
the completion sub commands which are only ever parsed by machines: The
output of "kubebuilder completion <shell>" must always only output the
exact script generated by cobra and the cobra internal commands
__complete and __completeNoDesc must only only return an exact list of
completion options.

In deprecated projects this will break shell completion if the shell is
started in the directory and if the completion script is correctly
initialized, all completion options will be polluted by the massive
deprecation notice.

This commit instead changes the deprecation notice to output to stderr
(while maintaining the return code of the actual command). This fixes
the completion usecase which only reads in stdout, and should not break
existing integrations, unless there exist scripts which use the length
of stderr to check for errors rather than the return code of the call.

Signed-off-by: Joel Pepper <pepper@bronze-deer.de>
  • Loading branch information
BronzeDeer committed Jun 21, 2023
1 parent 3a3d1d9 commit 0f3db00
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ func (c *CLI) addExtraCommands() error {
func (c CLI) printDeprecationWarnings() {
for _, p := range c.resolvedPlugins {
if p != nil && p.(plugin.Deprecated) != nil && len(p.(plugin.Deprecated).DeprecationWarning()) > 0 {
fmt.Printf(noticeColor, fmt.Sprintf(deprecationFmt, p.(plugin.Deprecated).DeprecationWarning()))
fmt.Fprintf(os.Stderr, noticeColor, fmt.Sprintf(deprecationFmt, p.(plugin.Deprecated).DeprecationWarning()))
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,13 +592,13 @@ plugins:
)
deprecatedPlugin := newMockDeprecatedPlugin("deprecated", "v1", deprecationWarning, projectVersion)

// Overwrite stdout to read the output and reset it afterwards
// Overwrite stderr to read the deprecation output and reset it afterwards
r, w, _ := os.Pipe()
temp := os.Stdout
temp := os.Stderr
defer func() {
os.Stdout = temp
os.Stderr = temp
}()
os.Stdout = w
os.Stderr = w

c, err = New(
WithPlugins(deprecatedPlugin),
Expand Down

0 comments on commit 0f3db00

Please sign in to comment.