Skip to content

Commit 70ed4ae

Browse files
guillaumerosepraveenkumar
authored andcommitted
Remove os.Exit() usage from most commands
Don't break the flow with os.Exit() but return an error to the upper layer.
1 parent d35b749 commit 70ed4ae

File tree

23 files changed

+185
-145
lines changed

23 files changed

+185
-145
lines changed

cmd/crc/cmd/config/get.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package config
22

33
import (
4-
"os"
5-
64
"github.com/code-ready/crc/pkg/crc/config"
75
"github.com/code-ready/crc/pkg/crc/exit"
86
"github.com/code-ready/crc/pkg/crc/output"
@@ -19,8 +17,7 @@ var configGetCmd = &cobra.Command{
1917
Long: `Gets a crc configuration property.`,
2018
Run: func(cmd *cobra.Command, args []string) {
2119
if len(args) < 1 {
22-
output.Outln("Please provide a configuration property to get")
23-
os.Exit(1)
20+
exit.WithMessage(1, "Please provide a configuration property to get")
2421
}
2522
runConfigGet(args[0])
2623
},

cmd/crc/cmd/console.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package cmd
22

33
import (
4+
"errors"
5+
"fmt"
6+
47
"github.com/code-ready/crc/pkg/crc/constants"
58
"github.com/code-ready/crc/pkg/crc/exit"
69
"github.com/code-ready/crc/pkg/crc/machine"
@@ -27,20 +30,24 @@ var consoleCmd = &cobra.Command{
2730
Short: "Open the OpenShift Web Console in the default browser",
2831
Long: `Open the OpenShift Web Console in the default browser or print its URL or credentials`,
2932
Run: func(cmd *cobra.Command, args []string) {
30-
runConsole(args)
33+
if err := runConsole(args); err != nil {
34+
exit.WithMessage(1, err.Error())
35+
}
3136
},
3237
}
3338

34-
func runConsole(arguments []string) {
39+
func runConsole(arguments []string) error {
3540
consoleConfig := machine.ConsoleConfig{
3641
Name: constants.DefaultName,
3742
}
3843

39-
exitIfMachineMissing(consoleConfig.Name)
44+
if err := checkIfMachineMissing(consoleConfig.Name); err != nil {
45+
return err
46+
}
4047

4148
result, err := machine.GetConsoleURL(consoleConfig)
4249
if err != nil {
43-
exit.WithoutMessage(1)
50+
return err
4451
}
4552

4653
if consolePrintURL {
@@ -51,15 +58,16 @@ func runConsole(arguments []string) {
5158
output.Outf("To login as an admin, run 'oc login -u kubeadmin -p %s %s'\n", result.ClusterConfig.KubeAdminPass, result.ClusterConfig.ClusterAPI)
5259
}
5360
if consolePrintURL || consolePrintCredentials {
54-
return
61+
return nil
5562
}
5663

5764
if !machine.IsRunning(result.State) {
58-
exit.WithMessage(1, "The OpenShift cluster is not running, cannot open the OpenShift Web Console.")
65+
return errors.New("The OpenShift cluster is not running, cannot open the OpenShift Web Console")
5966
}
6067
output.Outln("Opening the OpenShift Web Console in the default browser...")
6168
err = browser.OpenURL(result.ClusterConfig.WebConsoleURL)
6269
if err != nil {
63-
exit.WithMessage(1, "Failed to open the OpenShift Web Console, you can access it by opening %s in your web browser.", result.ClusterConfig.WebConsoleURL)
70+
return fmt.Errorf("Failed to open the OpenShift Web Console, you can access it by opening %s in your web browser", result.ClusterConfig.WebConsoleURL)
6471
}
72+
return nil
6573
}

cmd/crc/cmd/delete.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ var deleteCmd = &cobra.Command{
2323
Short: "Delete the OpenShift cluster",
2424
Long: "Delete the OpenShift cluster",
2525
Run: func(cmd *cobra.Command, args []string) {
26-
runDelete(args)
26+
if err := runDelete(args); err != nil {
27+
exit.WithMessage(1, err.Error())
28+
}
2729
},
2830
}
2931

3032
var clearCache bool
3133

32-
func runDelete(arguments []string) {
34+
func runDelete(arguments []string) error {
3335
deleteConfig := machine.DeleteConfig{
3436
Name: constants.DefaultName,
3537
}
@@ -38,16 +40,19 @@ func runDelete(arguments []string) {
3840
deleteCache()
3941
}
4042

41-
exitIfMachineMissing(deleteConfig.Name)
43+
if err := checkIfMachineMissing(deleteConfig.Name); err != nil {
44+
return err
45+
}
4246

4347
yes := input.PromptUserForYesOrNo("Do you want to delete the OpenShift cluster", globalForce)
4448
if yes {
4549
_, err := machine.Delete(deleteConfig)
4650
if err != nil {
47-
exit.WithoutMessage(1)
51+
return err
4852
}
4953
output.Outln("Deleted the OpenShift cluster")
5054
}
55+
return nil
5156
}
5257

5358
func deleteCache() {

cmd/crc/cmd/ip.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,26 @@ var ipCmd = &cobra.Command{
1717
Short: "Get IP address of the running OpenShift cluster",
1818
Long: "Get IP address of the running OpenShift cluster",
1919
Run: func(cmd *cobra.Command, args []string) {
20-
runIP(args)
20+
if err := runIP(args); err != nil {
21+
exit.WithMessage(1, err.Error())
22+
}
2123
},
2224
}
2325

24-
func runIP(arguments []string) {
26+
func runIP(arguments []string) error {
2527
ipConfig := machine.IPConfig{
2628
Name: constants.DefaultName,
2729
Debug: isDebugLog(),
2830
}
2931

30-
exitIfMachineMissing(ipConfig.Name)
32+
if err := checkIfMachineMissing(ipConfig.Name); err != nil {
33+
return err
34+
}
3135

3236
result, err := machine.IP(ipConfig)
3337
if err != nil {
34-
exit.WithoutMessage(1)
38+
return err
3539
}
3640
output.Outln(result.IP)
41+
return nil
3742
}

cmd/crc/cmd/oc_env.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cmd
22

33
import (
4+
"fmt"
5+
46
"github.com/code-ready/crc/pkg/crc/constants"
57
"github.com/code-ready/crc/pkg/crc/exit"
68
"github.com/code-ready/crc/pkg/crc/machine"
@@ -18,25 +20,32 @@ var ocEnvCmd = &cobra.Command{
1820
Short: "Add the 'oc' binary to PATH",
1921
Long: `Add the OpenShift client binary 'oc' to PATH`,
2022
Run: func(cmd *cobra.Command, args []string) {
21-
userShell, err := shell.GetShell(forceShell)
22-
if err != nil {
23-
exit.WithMessage(1, "Error running the oc-env command: %s", err.Error())
24-
}
25-
26-
proxyConfig, err := machine.GetProxyConfig(constants.DefaultName)
27-
if err != nil {
28-
exit.WithoutMessage(1)
29-
}
30-
output.Outln(shell.GetPathEnvString(userShell, constants.CrcOcBinDir))
31-
if proxyConfig.IsEnabled() {
32-
output.Outln(shell.GetEnvString(userShell, "HTTP_PROXY", proxyConfig.HTTPProxy))
33-
output.Outln(shell.GetEnvString(userShell, "HTTPS_PROXY", proxyConfig.HTTPSProxy))
34-
output.Outln(shell.GetEnvString(userShell, "NO_PROXY", proxyConfig.GetNoProxyString()))
23+
if err := runOcEnv(args); err != nil {
24+
exit.WithMessage(1, err.Error())
3525
}
36-
output.Outln(shell.GenerateUsageHint(userShell, "crc oc-env"))
3726
},
3827
}
3928

29+
func runOcEnv(args []string) error {
30+
userShell, err := shell.GetShell(forceShell)
31+
if err != nil {
32+
return fmt.Errorf("Error running the oc-env command: %s", err.Error())
33+
}
34+
35+
proxyConfig, err := machine.GetProxyConfig(constants.DefaultName)
36+
if err != nil {
37+
return err
38+
}
39+
output.Outln(shell.GetPathEnvString(userShell, constants.CrcOcBinDir))
40+
if proxyConfig.IsEnabled() {
41+
output.Outln(shell.GetEnvString(userShell, "HTTP_PROXY", proxyConfig.HTTPProxy))
42+
output.Outln(shell.GetEnvString(userShell, "HTTPS_PROXY", proxyConfig.HTTPSProxy))
43+
output.Outln(shell.GetEnvString(userShell, "NO_PROXY", proxyConfig.GetNoProxyString()))
44+
}
45+
output.Outln(shell.GenerateUsageHint(userShell, "crc oc-env"))
46+
return nil
47+
}
48+
4049
func init() {
4150
rootCmd.AddCommand(ocEnvCmd)
4251
ocEnvCmd.Flags().StringVar(&forceShell, "shell", "", "Set the environment for the specified shell: [fish, cmd, powershell, tcsh, bash, zsh]. Default is auto-detect.")

cmd/crc/cmd/podman_env.go

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cmd
22

33
import (
4+
"fmt"
5+
46
"github.com/code-ready/crc/pkg/crc/constants"
57
"github.com/code-ready/crc/pkg/crc/exit"
68
"github.com/code-ready/crc/pkg/crc/machine"
@@ -14,36 +16,44 @@ var podmanEnvCmd = &cobra.Command{
1416
Short: "Setup podman environment",
1517
Long: `Setup environment for 'podman' binary to access podman on CRC VM`,
1618
Run: func(cmd *cobra.Command, args []string) {
17-
18-
// See issue #961; Currently does not work on Windows in combination with the CRC vm.
19-
exit.WithMessage(1, "Currently not supported.")
20-
21-
userShell, err := shell.GetShell(forceShell)
22-
if err != nil {
23-
exit.WithMessage(1, "Error running the podman-env command: %s", err.Error())
19+
if err := runPodmanEnv(args); err != nil {
20+
exit.WithMessage(1, err.Error())
2421
}
25-
26-
ipConfig := machine.IPConfig{
27-
Name: constants.DefaultName,
28-
Debug: isDebugLog(),
29-
}
30-
31-
exitIfMachineMissing(ipConfig.Name)
32-
33-
result, err := machine.IP(ipConfig)
34-
if err != nil {
35-
exit.WithoutMessage(1)
36-
}
37-
38-
output.Outln(shell.GetPathEnvString(userShell, constants.CrcBinDir))
39-
output.Outln(shell.GetEnvString(userShell, "PODMAN_USER", constants.DefaultSSHUser))
40-
output.Outln(shell.GetEnvString(userShell, "PODMAN_HOST", result.IP))
41-
output.Outln(shell.GetEnvString(userShell, "PODMAN_IDENTITY_FILE", constants.GetPrivateKeyPath()))
42-
output.Outln(shell.GetEnvString(userShell, "PODMAN_IGNORE_HOSTS", "1"))
43-
output.Outln(shell.GenerateUsageHint(userShell, "crc podman-env"))
4422
},
4523
}
4624

25+
func runPodmanEnv(args []string) error {
26+
// See issue #961; Currently does not work on Windows in combination with the CRC vm.
27+
exit.WithMessage(1, "Currently not supported.")
28+
29+
userShell, err := shell.GetShell(forceShell)
30+
if err != nil {
31+
return fmt.Errorf("Error running the podman-env command: %s", err.Error())
32+
}
33+
34+
ipConfig := machine.IPConfig{
35+
Name: constants.DefaultName,
36+
Debug: isDebugLog(),
37+
}
38+
39+
if err := checkIfMachineMissing(ipConfig.Name); err != nil {
40+
return err
41+
}
42+
43+
result, err := machine.IP(ipConfig)
44+
if err != nil {
45+
return err
46+
}
47+
48+
output.Outln(shell.GetPathEnvString(userShell, constants.CrcBinDir))
49+
output.Outln(shell.GetEnvString(userShell, "PODMAN_USER", constants.DefaultSSHUser))
50+
output.Outln(shell.GetEnvString(userShell, "PODMAN_HOST", result.IP))
51+
output.Outln(shell.GetEnvString(userShell, "PODMAN_IDENTITY_FILE", constants.GetPrivateKeyPath()))
52+
output.Outln(shell.GetEnvString(userShell, "PODMAN_IGNORE_HOSTS", "1"))
53+
output.Outln(shell.GenerateUsageHint(userShell, "crc podman-env"))
54+
return nil
55+
}
56+
4757
func init() {
4858
rootCmd.AddCommand(podmanEnvCmd)
4959
podmanEnvCmd.Flags().StringVar(&forceShell, "shell", "", "Set the environment for the specified shell: [fish, cmd, powershell, tcsh, bash, zsh]. Default is auto-detect.")

cmd/crc/cmd/root.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package cmd
22

33
import (
4-
"github.com/code-ready/crc/pkg/crc/exit"
5-
"github.com/code-ready/crc/pkg/crc/machine"
6-
"github.com/code-ready/crc/pkg/crc/network"
7-
"github.com/code-ready/crc/pkg/crc/output"
8-
"github.com/spf13/cobra"
4+
"fmt"
95

106
cmdConfig "github.com/code-ready/crc/cmd/crc/cmd/config"
11-
127
"github.com/code-ready/crc/pkg/crc/config"
138
"github.com/code-ready/crc/pkg/crc/constants"
9+
"github.com/code-ready/crc/pkg/crc/exit"
1410
"github.com/code-ready/crc/pkg/crc/logging"
11+
"github.com/code-ready/crc/pkg/crc/machine"
12+
"github.com/code-ready/crc/pkg/crc/network"
13+
"github.com/code-ready/crc/pkg/crc/output"
1514
"github.com/code-ready/crc/pkg/crc/preflight"
15+
"github.com/spf13/cobra"
1616
)
1717

1818
var rootCmd = &cobra.Command{
@@ -81,14 +81,15 @@ func setConfigDefaults() {
8181
config.SetDefaults()
8282
}
8383

84-
func exitIfMachineMissing(name string) {
84+
func checkIfMachineMissing(name string) error {
8585
exists, err := machine.Exists(name)
8686
if err != nil {
87-
exit.WithMessage(1, err.Error())
87+
return err
8888
}
8989
if !exists {
90-
exit.WithMessage(1, "Machine '%s' does not exist. Use 'crc start' to create it.", name)
90+
return fmt.Errorf("Machine '%s' does not exist. Use 'crc start' to create it", constants.DefaultName)
9191
}
92+
return nil
9293
}
9394

9495
func setProxyDefaults() {

cmd/crc/cmd/setup.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package cmd
22

33
import (
4-
"github.com/spf13/cobra"
5-
64
"github.com/code-ready/crc/cmd/crc/cmd/config"
75
crcConfig "github.com/code-ready/crc/pkg/crc/config"
86
"github.com/code-ready/crc/pkg/crc/constants"
7+
"github.com/code-ready/crc/pkg/crc/exit"
98
"github.com/code-ready/crc/pkg/crc/output"
109
"github.com/code-ready/crc/pkg/crc/preflight"
10+
"github.com/spf13/cobra"
1111
)
1212

1313
func init() {
@@ -21,11 +21,13 @@ var setupCmd = &cobra.Command{
2121
Short: "Set up prerequisites for the OpenShift cluster",
2222
Long: "Set up local virtualization and networking infrastructure for the OpenShift cluster",
2323
Run: func(cmd *cobra.Command, args []string) {
24-
runSetup(args)
24+
if err := runSetup(args); err != nil {
25+
exit.WithMessage(1, err.Error())
26+
}
2527
},
2628
}
2729

28-
func runSetup(arguments []string) {
30+
func runSetup(arguments []string) error {
2931
if crcConfig.GetBool(config.ExperimentalFeatures.Name) {
3032
preflight.EnableExperimentalFeatures = true
3133
}
@@ -35,4 +37,5 @@ func runSetup(arguments []string) {
3537
bundle = " -b $bundlename"
3638
}
3739
output.Outf("Setup is complete, you can now run 'crc start%s' to start the OpenShift cluster\n", bundle)
40+
return nil
3841
}

0 commit comments

Comments
 (0)