Output redirection was fixed for several cobra commands
Fix
MODIFY
pkg/cmd/describe_integration.go
pkg/cmd/describe_integration.go
@@ -43,11 +43,11 @@ func newDescribeIntegrationCmd(rootCmdOptions *RootCmdOptions) (*cobra.Command,
Short: "Describe an Integration",
Long: `Describe an Integration.`,
PreRunE: decode(&options),
- RunE: func(_ *cobra.Command, args []string) error {
- if err := options.validate(args); err != nil {
+ RunE: func(cmd *cobra.Command, args []string) error {
+ if err := options.validate(cmd, args); err != nil {
return err
}
- if err := options.run(args); err != nil {
+ if err := options.run(cmd, args); err != nil {
fmt.Println(err.Error())
}
@@ -65,14 +65,14 @@ type describeIntegrationCommandOptions struct {
showSourceContent bool `mapstructure:"show-source-content"`
}
-func (command *describeIntegrationCommandOptions) validate(args []string) error {
+func (command *describeIntegrationCommandOptions) validate(_ *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("describe expects an integration name argument")
}
return nil
}
-func (command *describeIntegrationCommandOptions) run(args []string) error {
+func (command *describeIntegrationCommandOptions) run(cmd *cobra.Command, args []string) error {
c, err := command.GetCmdClient()
if err != nil {
return err
@@ -85,21 +85,21 @@ func (command *describeIntegrationCommandOptions) run(args []string) error {
}
if err := c.Get(command.Context, key, &ctx); err == nil {
- if desc, err := command.describeIntegration(ctx); err == nil {
+ if desc, err := command.describeIntegration(cmd, ctx); err == nil {
fmt.Print(desc)
} else {
fmt.Println(err)
}
} else {
- fmt.Printf("Integration '%s' does not exist.\n", args[0])
+ fmt.Fprintf(cmd.OutOrStdout(), "Integration '%s' does not exist.\n", args[0])
}
return nil
}
-func (command *describeIntegrationCommandOptions) describeIntegration(i v1.Integration) (string, error) {
+func (command *describeIntegrationCommandOptions) describeIntegration(cmd *cobra.Command, i v1.Integration) (string, error) {
return indentedwriter.IndentedString(func(out io.Writer) error {
- w := indentedwriter.NewWriter(out)
+ w := indentedwriter.NewWriter(cmd.OutOrStdout())
describeObjectMeta(w, i.ObjectMeta)

MODIFY
pkg/cmd/describe_kamelet.go
pkg/cmd/describe_kamelet.go
@@ -42,11 +42,11 @@ func newDescribeKameletCmd(rootCmdOptions *RootCmdOptions) (*cobra.Command, *des
Short: "Describe a Kamelet",
Long: `Describe a Kamelet.`,
PreRunE: decode(&options),
- RunE: func(_ *cobra.Command, args []string) error {
- if err := options.validate(args); err != nil {
+ RunE: func(cmd *cobra.Command, args []string) error {
+ if err := options.validate(cmd, args); err != nil {
return err
}
- if err := options.run(args); err != nil {
+ if err := options.run(cmd, args); err != nil {
fmt.Println(err.Error())
}
@@ -61,14 +61,14 @@ type describeKameletCommandOptions struct {
*RootCmdOptions
}
-func (command *describeKameletCommandOptions) validate(args []string) error {
+func (command *describeKameletCommandOptions) validate(_ *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("describe expects a Kamelet name argument")
}
return nil
}
-func (command *describeKameletCommandOptions) run(args []string) error {
+func (command *describeKameletCommandOptions) run(cmd *cobra.Command, args []string) error {
c, err := command.GetCmdClient()
if err != nil {
return err
@@ -81,7 +81,7 @@ func (command *describeKameletCommandOptions) run(args []string) error {
}
if err := c.Get(command.Context, kameletKey, &kamelet); err == nil {
- if desc, err := command.describeKamelet(kamelet); err == nil {
+ if desc, err := command.describeKamelet(cmd, kamelet); err == nil {
fmt.Print(desc)
} else {
fmt.Println(err)
@@ -93,9 +93,9 @@ func (command *describeKameletCommandOptions) run(args []string) error {
return nil
}
-func (command *describeKameletCommandOptions) describeKamelet(kamelet v1alpha1.Kamelet) (string, error) {
+func (command *describeKameletCommandOptions) describeKamelet(cmd *cobra.Command, kamelet v1alpha1.Kamelet) (string, error) {
return indentedwriter.IndentedString(func(out io.Writer) error {
- w := indentedwriter.NewWriter(out)
+ w := indentedwriter.NewWriter(cmd.OutOrStdout())
describeObjectMeta(w, kamelet.ObjectMeta)

MODIFY
pkg/cmd/describe_kit.go
pkg/cmd/describe_kit.go
@@ -41,11 +41,11 @@ func newDescribeKitCmd(rootCmdOptions *RootCmdOptions) (*cobra.Command, *describ
Short: "Describe an Integration Kit",
Long: `Describe an Integration Kit.`,
PreRunE: decode(&options),
- RunE: func(_ *cobra.Command, args []string) error {
- if err := options.validate(args); err != nil {
+ RunE: func(cmd *cobra.Command, args []string) error {
+ if err := options.validate(cmd, args); err != nil {
return err
}
- if err := options.run(args); err != nil {
+ if err := options.run(cmd, args); err != nil {
fmt.Println(err.Error())
}
@@ -60,14 +60,14 @@ type describeKitCommandOptions struct {
*RootCmdOptions
}
-func (command *describeKitCommandOptions) validate(args []string) error {
+func (command *describeKitCommandOptions) validate(_ *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("describe expects a kit name argument")
}
return nil
}
-func (command *describeKitCommandOptions) run(args []string) error {
+func (command *describeKitCommandOptions) run(cmd *cobra.Command, args []string) error {
c, err := command.GetCmdClient()
if err != nil {
return err
@@ -80,7 +80,7 @@ func (command *describeKitCommandOptions) run(args []string) error {
}
if err := c.Get(command.Context, kitKey, kit); err == nil {
- if desc, err := command.describeIntegrationKit(kit); err == nil {
+ if desc, err := command.describeIntegrationKit(cmd, kit); err == nil {
fmt.Print(desc)
} else {
fmt.Println(err)
@@ -92,9 +92,9 @@ func (command *describeKitCommandOptions) run(args []string) error {
return nil
}
-func (command *describeKitCommandOptions) describeIntegrationKit(kit *v1.IntegrationKit) (string, error) {
+func (command *describeKitCommandOptions) describeIntegrationKit(cmd *cobra.Command, kit *v1.IntegrationKit) (string, error) {
return indentedwriter.IndentedString(func(out io.Writer) error {
- w := indentedwriter.NewWriter(out)
+ w := indentedwriter.NewWriter(cmd.OutOrStdout())
describeObjectMeta(w, kit.ObjectMeta)

MODIFY
pkg/cmd/describe_platform.go
pkg/cmd/describe_platform.go
@@ -41,11 +41,11 @@ func newDescribePlatformCmd(rootCmdOptions *RootCmdOptions) (*cobra.Command, *de
Short: "Describe an Integration Platform",
Long: `Describe an Integration Platform.`,
PreRunE: decode(&options),
- RunE: func(_ *cobra.Command, args []string) error {
- if err := options.validate(args); err != nil {
+ RunE: func(cmd *cobra.Command, args []string) error {
+ if err := options.validate(cmd, args); err != nil {
return err
}
- if err := options.run(args); err != nil {
+ if err := options.run(cmd, args); err != nil {
fmt.Println(err.Error())
}
@@ -60,14 +60,14 @@ type describePlatformCommandOptions struct {
*RootCmdOptions
}
-func (command *describePlatformCommandOptions) validate(args []string) error {
+func (command *describePlatformCommandOptions) validate(_ *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("describe expects a platform name argument")
}
return nil
}
-func (command *describePlatformCommandOptions) run(args []string) error {
+func (command *describePlatformCommandOptions) run(cmd *cobra.Command, args []string) error {
c, err := command.GetCmdClient()
if err != nil {
return err
@@ -80,21 +80,21 @@ func (command *describePlatformCommandOptions) run(args []string) error {
}
if err := c.Get(command.Context, platformKey, &platform); err == nil {
- if desc, err := command.describeIntegrationPlatform(platform); err == nil {
+ if desc, err := command.describeIntegrationPlatform(cmd, platform); err == nil {
fmt.Print(desc)
} else {
fmt.Println(err)
}
} else {
- fmt.Printf("IntegrationPlatform '%s' does not exist.\n", args[0])
+ fmt.Fprintf(cmd.OutOrStdout(), "IntegrationPlatform '%s' does not exist.\n", args[0])
}
return nil
}
-func (command *describePlatformCommandOptions) describeIntegrationPlatform(platform v1.IntegrationPlatform) (string, error) {
+func (command *describePlatformCommandOptions) describeIntegrationPlatform(cmd *cobra.Command, platform v1.IntegrationPlatform) (string, error) {
return indentedwriter.IndentedString(func(out io.Writer) error {
- w := indentedwriter.NewWriter(out)
+ w := indentedwriter.NewWriter(cmd.OutOrStdout())
describeObjectMeta(w, platform.ObjectMeta)
w.Write(0, "Phase:\t%s\n", platform.Status.Phase)
w.Write(0, "Version:\t%s\n", platform.Status.Version)

MODIFY
pkg/cmd/dump.go
pkg/cmd/dump.go
@@ -21,6 +21,7 @@ import (
"bufio"
"context"
"fmt"
+ "io"
"os"
"github.com/spf13/cobra"
@@ -53,7 +54,7 @@ type dumpCmdOptions struct {
LogLines int `mapstructure:"logLines"`
}
-func (o *dumpCmdOptions) dump(_ *cobra.Command, args []string) error {
+func (o *dumpCmdOptions) dump(cmd *cobra.Command, args []string) error {
c, err := o.GetCmdClient()
if err != nil {
return err
@@ -67,12 +68,12 @@ func (o *dumpCmdOptions) dump(_ *cobra.Command, args []string) error {
dumpNamespace(o.Context, c, o.Namespace, writer, o.LogLines)
defer writer.Close()
} else {
- dumpNamespace(o.Context, c, o.Namespace, os.Stdout, o.LogLines)
+ dumpNamespace(o.Context, c, o.Namespace, cmd.OutOrStdout(), o.LogLines)
}
return nil
}
-func dumpNamespace(ctx context.Context, c client.Client, ns string, out *os.File, logLines int) error {
+func dumpNamespace(ctx context.Context, c client.Client, ns string, out io.Writer, logLines int) error {
camelClient, err := versioned.NewForConfig(c.GetConfig())
if err != nil {
@@ -173,13 +174,13 @@ func dumpNamespace(ctx context.Context, c client.Client, ns string, out *os.File
return nil
}
-func dumpConditions(prefix string, conditions []v1.PodCondition, out *os.File) {
+func dumpConditions(prefix string, conditions []v1.PodCondition, out io.Writer) {
for _, cond := range conditions {
fmt.Fprintf(out, "%scondition type=%s, status=%s, reason=%s, message=%q\n", prefix, cond.Type, cond.Status, cond.Reason, cond.Message)
}
}
-func dumpLogs(ctx context.Context, c client.Client, prefix string, ns string, name string, container string, out *os.File, logLines int) error {
+func dumpLogs(ctx context.Context, c client.Client, prefix string, ns string, name string, container string, out io.Writer, logLines int) error {
lines := int64(logLines)
stream, err := c.CoreV1().Pods(ns).GetLogs(name, &v1.PodLogOptions{
Container: container,

MODIFY
pkg/cmd/version.go
pkg/cmd/version.go
@@ -20,7 +20,6 @@ package cmd
import (
"context"
"fmt"
-
"github.com/Masterminds/semver"
"github.com/spf13/cobra"
@@ -67,36 +66,36 @@ func (o *versionCmdOptions) preRunE(cmd *cobra.Command, args []string) error {
return o.RootCmdOptions.preRun(cmd, args)
}
-func (o *versionCmdOptions) run(_ *cobra.Command, _ []string) error {
+func (o *versionCmdOptions) run(cmd *cobra.Command, _ []string) error {
if o.Operator {
c, err := o.GetCmdClient()
if err != nil {
return err
}
- displayOperatorVersion(o.Context, c, o.Namespace)
+ displayOperatorVersion(cmd, o.Context, c, o.Namespace)
} else {
- displayClientVersion()
+ displayClientVersion(cmd)
}
return nil
}
-func displayClientVersion() {
+func displayClientVersion(cmd *cobra.Command) {
if VersionVariant != "" {
- fmt.Printf("Camel K Client %s %s\n", VersionVariant, defaults.Version)
+ fmt.Fprintf(cmd.OutOrStdout(), "Camel K Client %s %s\n", VersionVariant, defaults.Version)
} else {
- fmt.Printf("Camel K Client %s\n", defaults.Version)
+ fmt.Fprintf(cmd.OutOrStdout(), "Camel K Client %s\n", defaults.Version)
}
}
-func displayOperatorVersion(ctx context.Context, c client.Client, namespace string) {
+func displayOperatorVersion(cmd *cobra.Command, ctx context.Context, c client.Client, namespace string) {
operatorVersion, err := operatorVersion(ctx, c, namespace)
if err != nil {
- fmt.Printf("Unable to retrieve operator version: %s\n", err)
+ fmt.Fprintf(cmd.OutOrStdout(), "Unable to retrieve operator version: %s\n", err)
} else {
if operatorVersion == "" {
- fmt.Printf("Unable to retrieve operator version: The IntegrationPlatform resource hasn't been reconciled yet!")
+ fmt.Fprintf(cmd.OutOrStdout(), "Unable to retrieve operator version: The IntegrationPlatform resource hasn't been reconciled yet!")
} else {
- fmt.Printf("Camel K Operator %s\n", operatorVersion)
+ fmt.Fprintf(cmd.OutOrStdout(), "Camel K Operator %s\n", operatorVersion)
}
}
}

Output redirection was fixed for several cobra commands

Fix #2495