Skip to content

Commit

Permalink
runPostRun() was never called in case of an error
Browse files Browse the repository at this point in the history
This removes almost all os.Exit() call in the middle of the flow. It
allows us to catch error more easily.
  • Loading branch information
guillaumerose authored and praveenkumar committed Jan 4, 2021
1 parent 447c6ca commit 78915b7
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 115 deletions.
7 changes: 2 additions & 5 deletions cmd/crc/cmd/cleanup.go
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"os"

"github.com/code-ready/crc/pkg/crc/exit"
"github.com/code-ready/crc/pkg/crc/preflight"
"github.com/spf13/cobra"
)
Expand All @@ -20,10 +19,8 @@ var cleanupCmd = &cobra.Command{
Use: "cleanup",
Short: "Undo config changes",
Long: "Undo all the configuration changes done by 'crc setup' command",
Run: func(cmd *cobra.Command, args []string) {
if err := runCleanup(); err != nil {
exit.WithMessage(1, err.Error())
}
RunE: func(cmd *cobra.Command, args []string) error {
return runCleanup()
},
}

Expand Down
11 changes: 6 additions & 5 deletions cmd/crc/cmd/config/get.go
@@ -1,10 +1,10 @@
package config

import (
"errors"
"fmt"

"github.com/code-ready/crc/pkg/crc/config"
"github.com/code-ready/crc/pkg/crc/exit"
"github.com/code-ready/crc/pkg/crc/output"
"github.com/spf13/cobra"
)
Expand All @@ -14,20 +14,21 @@ func configGetCmd(config config.Storage) *cobra.Command {
Use: "get CONFIG-KEY",
Short: "Get a crc configuration property",
Long: `Gets a crc configuration property.`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
exit.WithMessage(1, "Please provide a configuration property to get")
return errors.New("Please provide a configuration property to get")
}
key := args[0]
v := config.Get(key)
switch {
case v.Invalid:
exit.WithMessage(1, fmt.Sprintf("Configuration property '%s' does not exist", key))
return fmt.Errorf("Configuration property '%s' does not exist", key)
case v.IsDefault:
exit.WithMessage(1, fmt.Sprintf("Configuration property '%s' is not set. Default value is '%s'", key, v.AsString()))
return fmt.Errorf("Configuration property '%s' is not set. Default value is '%s'", key, v.AsString())
default:
output.Outln(key, ":", v.AsString())
}
return nil
},
}
}
10 changes: 6 additions & 4 deletions cmd/crc/cmd/config/set.go
@@ -1,8 +1,9 @@
package config

import (
"errors"

"github.com/code-ready/crc/pkg/crc/config"
"github.com/code-ready/crc/pkg/crc/exit"
"github.com/code-ready/crc/pkg/crc/output"
"github.com/spf13/cobra"
)
Expand All @@ -12,18 +13,19 @@ func configSetCmd(config config.Storage) *cobra.Command {
Use: "set CONFIG-KEY VALUE",
Short: "Set a crc configuration property",
Long: `Sets a crc configuration property.`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
exit.WithMessage(1, "Please provide a configuration property and its value as in 'crc config set KEY VALUE'")
return errors.New("Please provide a configuration property and its value as in 'crc config set KEY VALUE'")
}
setMessage, err := config.Set(args[0], args[1])
if err != nil {
exit.WithMessage(1, err.Error())
return err
}

if setMessage != "" {
output.Outln(setMessage)
}
return nil
},
}
}
10 changes: 6 additions & 4 deletions cmd/crc/cmd/config/unset.go
@@ -1,8 +1,9 @@
package config

import (
"errors"

"github.com/code-ready/crc/pkg/crc/config"
"github.com/code-ready/crc/pkg/crc/exit"
"github.com/code-ready/crc/pkg/crc/output"
"github.com/spf13/cobra"
)
Expand All @@ -12,17 +13,18 @@ func configUnsetCmd(config config.Storage) *cobra.Command {
Use: "unset CONFIG-KEY",
Short: "Unset a crc configuration property",
Long: `Unsets a crc configuration property.`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
exit.WithMessage(1, "Please provide a configuration property to unset")
return errors.New("Please provide a configuration property to unset")
}
unsetMessage, err := config.Unset(args[0])
if err != nil {
exit.WithMessage(1, err.Error())
return err
}
if unsetMessage != "" {
output.Outln(unsetMessage)
}
return nil
},
}
}
9 changes: 3 additions & 6 deletions cmd/crc/cmd/config/view.go
Expand Up @@ -9,7 +9,6 @@ import (
"text/template"

"github.com/code-ready/crc/pkg/crc/config"
"github.com/code-ready/crc/pkg/crc/logging"
"github.com/spf13/cobra"
)

Expand All @@ -29,14 +28,12 @@ func configViewCmd(config config.Storage) *cobra.Command {
Use: "view",
Short: "Display all assigned crc configuration properties",
Long: `Displays all assigned crc configuration properties and their values.`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
tmpl, err := determineTemplate(configViewFormat)
if err != nil {
logging.Fatal(err)
}
if err := runConfigView(config.AllConfigs(), tmpl, os.Stdout); err != nil {
logging.Fatal(err)
return err
}
return runConfigView(config.AllConfigs(), tmpl, os.Stdout)
},
}
configViewCmd.Flags().StringVar(&configViewFormat, "format", DefaultConfigViewFormat,
Expand Down
7 changes: 2 additions & 5 deletions cmd/crc/cmd/console.go
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"os"

"github.com/code-ready/crc/pkg/crc/exit"
"github.com/code-ready/crc/pkg/crc/machine"
"github.com/code-ready/machine/libmachine/state"
"github.com/pkg/browser"
Expand All @@ -31,10 +30,8 @@ var consoleCmd = &cobra.Command{
Aliases: []string{"dashboard"},
Short: "Open the OpenShift Web Console in the default browser",
Long: `Open the OpenShift Web Console in the default browser or print its URL or credentials`,
Run: func(cmd *cobra.Command, args []string) {
if renderErr := runConsole(os.Stdout, newMachine(), consolePrintURL, consolePrintCredentials, outputFormat); renderErr != nil {
exit.WithMessage(1, renderErr.Error())
}
RunE: func(cmd *cobra.Command, args []string) error {
return runConsole(os.Stdout, newMachine(), consolePrintURL, consolePrintCredentials, outputFormat)
},
}

Expand Down
10 changes: 4 additions & 6 deletions cmd/crc/cmd/daemon.go
Expand Up @@ -12,7 +12,6 @@ import (

crcConfig "github.com/code-ready/crc/pkg/crc/config"
"github.com/code-ready/crc/pkg/crc/constants"
"github.com/code-ready/crc/pkg/crc/exit"
"github.com/code-ready/crc/pkg/crc/logging"
"github.com/code-ready/gvisor-tap-vsock/pkg/transport"
"github.com/code-ready/gvisor-tap-vsock/pkg/types"
Expand All @@ -32,7 +31,7 @@ var daemonCmd = &cobra.Command{
Short: "Run the crc daemon",
Long: "Run the crc daemon",
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
// setup separate logging for daemon
logging.CloseLogging()
logging.InitLogrus(logging.LogLevel, constants.DaemonLogFilePath)
Expand All @@ -50,7 +49,7 @@ var daemonCmd = &cobra.Command{
}
}

if err := run(&types.Configuration{
err := run(&types.Configuration{
Debug: false, // never log packets
CaptureFile: captureFile(),
MTU: 4000, // Large packets slightly improve the performance. Less small packets.
Expand Down Expand Up @@ -89,9 +88,8 @@ var daemonCmd = &cobra.Command{
":6443": "192.168.127.2:6443",
":443": "192.168.127.2:443",
},
}, endpoints); err != nil {
exit.WithMessage(1, err.Error())
}
}, endpoints)
return err
},
}

Expand Down
7 changes: 2 additions & 5 deletions cmd/crc/cmd/delete.go
Expand Up @@ -7,7 +7,6 @@ import (
"os"

"github.com/code-ready/crc/pkg/crc/constants"
"github.com/code-ready/crc/pkg/crc/exit"
"github.com/code-ready/crc/pkg/crc/input"
"github.com/code-ready/crc/pkg/crc/machine"
"github.com/spf13/cobra"
Expand All @@ -27,10 +26,8 @@ var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete the OpenShift cluster",
Long: "Delete the OpenShift cluster",
Run: func(cmd *cobra.Command, args []string) {
if err := runDelete(os.Stdout, newMachine(), clearCache, constants.MachineCacheDir, outputFormat != jsonFormat, globalForce, outputFormat); err != nil {
exit.WithMessage(1, err.Error())
}
RunE: func(cmd *cobra.Command, args []string) error {
return runDelete(os.Stdout, newMachine(), clearCache, constants.MachineCacheDir, outputFormat != jsonFormat, globalForce, outputFormat)
},
}

Expand Down
7 changes: 2 additions & 5 deletions cmd/crc/cmd/ip.go
@@ -1,7 +1,6 @@
package cmd

import (
"github.com/code-ready/crc/pkg/crc/exit"
"github.com/code-ready/crc/pkg/crc/output"
"github.com/spf13/cobra"
)
Expand All @@ -14,10 +13,8 @@ var ipCmd = &cobra.Command{
Use: "ip",
Short: "Get IP address of the running OpenShift cluster",
Long: "Get IP address of the running OpenShift cluster",
Run: func(cmd *cobra.Command, args []string) {
if err := runIP(args); err != nil {
exit.WithMessage(1, err.Error())
}
RunE: func(cmd *cobra.Command, args []string) error {
return runIP(args)
},
}

Expand Down
7 changes: 2 additions & 5 deletions cmd/crc/cmd/oc_env.go
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"github.com/code-ready/crc/pkg/crc/constants"
"github.com/code-ready/crc/pkg/crc/exit"
"github.com/code-ready/crc/pkg/crc/output"
"github.com/code-ready/crc/pkg/os/shell"
"github.com/spf13/cobra"
Expand All @@ -18,10 +17,8 @@ var ocEnvCmd = &cobra.Command{
Use: "oc-env",
Short: "Add the 'oc' executable to PATH",
Long: `Add the OpenShift client executable 'oc' to PATH`,
Run: func(cmd *cobra.Command, args []string) {
if err := runOcEnv(args); err != nil {
exit.WithMessage(1, err.Error())
}
RunE: func(cmd *cobra.Command, args []string) error {
return runOcEnv(args)
},
}

Expand Down
14 changes: 5 additions & 9 deletions cmd/crc/cmd/podman_env.go
@@ -1,10 +1,10 @@
package cmd

import (
"errors"
"fmt"

"github.com/code-ready/crc/pkg/crc/constants"
"github.com/code-ready/crc/pkg/crc/exit"
"github.com/code-ready/crc/pkg/crc/output"
"github.com/code-ready/crc/pkg/os/shell"
"github.com/spf13/cobra"
Expand All @@ -14,17 +14,13 @@ var podmanEnvCmd = &cobra.Command{
Use: "podman-env",
Short: "Setup podman environment",
Long: `Setup environment for 'podman' executable to access podman on CRC VM`,
Run: func(cmd *cobra.Command, args []string) {
if err := runPodmanEnv(args); err != nil {
exit.WithMessage(1, err.Error())
}
RunE: func(cmd *cobra.Command, args []string) error {
// See issue #961; Currently does not work on Windows in combination with the CRC vm.
return errors.New("currently not supported")
},
}

func runPodmanEnv(args []string) error {
// See issue #961; Currently does not work on Windows in combination with the CRC vm.
exit.WithMessage(1, "Currently not supported.")

func RunPodmanEnv(args []string) error {
userShell, err := shell.GetShell(forceShell)
if err != nil {
return fmt.Errorf("Error running the podman-env command: %s", err.Error())
Expand Down

0 comments on commit 78915b7

Please sign in to comment.