Do not necessarily error out if odo dev is stopped via Ctrl+C (#6917)

* Add test highlighting the expectations

* Propagate errors and call os.Exit only in 'main' functions

See ExitInMain[1] and ExitInMain_ExitOnce[2] coding convention guidelines.

[1] https://github.com/redhat-developer/odo/wiki/Dev:-Coding-Conventions#exit-in-main
[2] https://github.com/redhat-developer/odo/wiki/Dev:-Coding-Conventions#exit-once

* Handle errors returned by Cleanuper#Cleanup

This makes sure the exit code of the command
is mapped onto any error returned by Cleanup

* Do not return an error when the watch loop in 'odo dev' is interrupted

* Test that the exit code of 'odo dev' matches the error returned by the cleanup logic
This commit is contained in:
Armel Soro
2023-06-22 13:14:07 +02:00
committed by GitHub
parent 28ed064133
commit 147542db69
20 changed files with 272 additions and 86 deletions

View File

@@ -6,10 +6,12 @@ import (
"fmt"
"os"
"github.com/redhat-developer/odo/pkg/odo/cli"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/redhat-developer/odo/pkg/odo/cli"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
"github.com/redhat-developer/odo/pkg/odo/util"
)
/*
@@ -169,21 +171,34 @@ func main() {
Args: cobra.OnlyValidArgs,
ValidArgs: []string{"help", "reference", "structure"},
Run: func(command *cobra.Command, args []string) {
RunE: func(command *cobra.Command, args []string) error {
if len(args) == 0 {
fmt.Print(command.Usage())
} else {
ctx := context.Background()
testClientset := clientset.Clientset{}
switch args[0] {
case "reference":
fmt.Print(referencePrinter(cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName, testClientset), 0))
case "structure":
fmt.Print(commandPrinter(cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName, testClientset), 0))
default:
fmt.Print(command.Usage())
}
return nil
}
ctx := context.Background()
testClientset := clientset.Clientset{}
switch args[0] {
case "reference":
cmdOdo, err := cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName, func(err error) {
util.LogErrorAndExit(err, "")
}, testClientset)
if err != nil {
return err
}
fmt.Print(referencePrinter(cmdOdo, 0))
case "structure":
cmdOdo, err := cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName, func(err error) {
util.LogErrorAndExit(err, "")
}, testClientset)
if err != nil {
return err
}
fmt.Print(commandPrinter(cmdOdo, 0))
default:
fmt.Print(command.Usage())
}
return nil
},
}

View File

@@ -7,10 +7,11 @@ import (
"os"
"testing"
"github.com/sethvargo/go-envconfig"
"github.com/spf13/pflag"
"k8s.io/klog"
"github.com/sethvargo/go-envconfig"
"github.com/redhat-developer/odo/pkg/config"
envcontext "github.com/redhat-developer/odo/pkg/config/context"
"github.com/redhat-developer/odo/pkg/odo/cli"
@@ -77,7 +78,10 @@ func runCommand(
clientset.Stdout = &stdoutB
clientset.Stderr = &stderrB
root := cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName, clientset)
root, err := cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName, nil, clientset)
if err != nil {
t.Fatal(err)
}
root.SetOut(&stdoutB)
root.SetErr(&stderrB)

View File

@@ -72,7 +72,10 @@ func TestOdoHelp(t *testing.T) {
resetGlobalFlags()
root := cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName, clientset.Clientset{})
root, err := cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName, nil, clientset.Clientset{})
if err != nil {
t.Fatal(err)
}
var stdoutB, stderrB bytes.Buffer
root.SetOut(&stdoutB)

View File

@@ -38,7 +38,12 @@ func main() {
// create the complete command
klog.InitFlags(nil)
root := cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName, clientset.Clientset{})
root, err := cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName, func(err error) {
util.LogErrorAndExit(err, "")
}, clientset.Clientset{})
if err != nil {
util.LogErrorAndExit(err, "")
}
rootCmp := createCompletion(root)
cmp := complete.New("odo", rootCmp)
@@ -59,7 +64,7 @@ func main() {
args := os.Args[1:]
if err = flag.CommandLine.Parse(args); err != nil {
if err == flag.ErrHelp {
os.Exit(0)
return
}
}