mirror of https://github.com/knative/client.git
remove use of os.Exit (#851)
This commit is contained in:
parent
739e63f835
commit
4b3b971ae5
|
|
@ -33,8 +33,13 @@ var err error
|
|||
func main() {
|
||||
defer cleanup()
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
err = core.NewDefaultKnCommand().Execute()
|
||||
kn, err := core.NewDefaultKnCommand()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err := kn.Execute(); err != nil {
|
||||
if err.Error() != "subcommand is required" {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -113,10 +113,10 @@ func (h *DefaultPluginHandler) Execute(executablePath string, cmdArgs, environme
|
|||
cmd.Stdin = os.Stdin
|
||||
cmd.Env = environment
|
||||
err := cmd.Run()
|
||||
if err == nil {
|
||||
os.Exit(0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
return syscall.Exec(executablePath, cmdArgs, environment)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ import (
|
|||
)
|
||||
|
||||
// NewDefaultKnCommand creates the default `kn` command with a default plugin handler
|
||||
func NewDefaultKnCommand() *cobra.Command {
|
||||
func NewDefaultKnCommand() (*cobra.Command, error) {
|
||||
rootCmd := NewKnCommand()
|
||||
|
||||
// Needed since otherwise --plugins-dir and --lookup-plugins
|
||||
|
|
@ -53,7 +53,7 @@ func NewDefaultKnCommand() *cobra.Command {
|
|||
pluginsDir, lookupPluginsInPath, err := extractKnPluginFlags(os.Args)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%v\n", err)
|
||||
os.Exit(1)
|
||||
return &cobra.Command{}, fmt.Errorf("%v", err)
|
||||
}
|
||||
|
||||
pluginHandler := plugin.NewDefaultPluginHandler(plugin.ValidPluginFilenamePrefixes,
|
||||
|
|
@ -70,9 +70,9 @@ func NewDefaultKnCommandWithArgs(rootCmd *cobra.Command,
|
|||
args []string,
|
||||
in io.Reader,
|
||||
out,
|
||||
errOut io.Writer) *cobra.Command {
|
||||
errOut io.Writer) (*cobra.Command, error) {
|
||||
if pluginHandler == nil {
|
||||
return rootCmd
|
||||
return rootCmd, nil
|
||||
}
|
||||
if len(args) > 1 {
|
||||
cmdPathPieces := args[1:]
|
||||
|
|
@ -84,18 +84,16 @@ func NewDefaultKnCommandWithArgs(rootCmd *cobra.Command,
|
|||
if err != nil || plugin.InAllowedExtensibleCommandGroups(foundCmd.Name()) {
|
||||
err := plugin.HandlePluginCommand(pluginHandler, cmdPathPieces)
|
||||
if err != nil {
|
||||
fmt.Fprintf(rootCmd.OutOrStderr(), "Error: unknown command '%s' \nRun 'kn --help' for usage.\n", args[1])
|
||||
os.Exit(1)
|
||||
return &cobra.Command{}, fmt.Errorf("unknown command '%s' \nRun 'kn --help' for usage", args[1])
|
||||
}
|
||||
} else if foundCmd.HasSubCommands() {
|
||||
if _, _, err := rootCmd.Find(innerArgs); err != nil {
|
||||
fmt.Fprintf(rootCmd.OutOrStderr(), showSubcommands(foundCmd, cmdPathPieces, innerArgs[0]))
|
||||
os.Exit(1)
|
||||
return &cobra.Command{}, fmt.Errorf(showSubcommands(foundCmd, cmdPathPieces, innerArgs[0]))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rootCmd
|
||||
return rootCmd, nil
|
||||
}
|
||||
|
||||
// NewKnCommand creates the rootCmd which is the base command when called without any subcommands
|
||||
|
|
@ -232,8 +230,7 @@ func initConfig() {
|
|||
func defaultConfigDir() (string, error) {
|
||||
home, err := homedir.Dir()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%v\n", err)
|
||||
os.Exit(1)
|
||||
return "", fmt.Errorf("%v", err)
|
||||
}
|
||||
// Check the deprecated path first and fallback to it, add warning to error message
|
||||
if configHome := filepath.Join(home, ".kn"); dirExists(configHome) {
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ func TestNewDefaultKnCommand(t *testing.T) {
|
|||
var rootCmd *cobra.Command
|
||||
|
||||
setup := func(t *testing.T) {
|
||||
rootCmd = NewDefaultKnCommand()
|
||||
rootCmd, _ = NewDefaultKnCommand()
|
||||
}
|
||||
|
||||
t.Run("returns a valid root command", func(t *testing.T) {
|
||||
|
|
@ -48,7 +48,7 @@ func TestNewDefaultKnCommandWithArgs(t *testing.T) {
|
|||
)
|
||||
|
||||
setup := func(t *testing.T) {
|
||||
rootCmd = NewDefaultKnCommandWithArgs(NewKnCommand(), pluginHandler, args, os.Stdin, os.Stdout, os.Stderr)
|
||||
rootCmd, _ = NewDefaultKnCommandWithArgs(NewKnCommand(), pluginHandler, args, os.Stdin, os.Stdout, os.Stderr)
|
||||
}
|
||||
|
||||
t.Run("when pluginHandler is nil", func(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -82,7 +82,7 @@ func TestWrongCommand(t *testing.T) {
|
|||
r.AssertError(out)
|
||||
|
||||
out = test.Kn{}.Run("rev")
|
||||
assert.Check(t, util.ContainsAll(out.Stderr, "Error", "unknown command", "rev"))
|
||||
assert.Check(t, util.ContainsAll(out.Stderr, "unknown command", "rev"))
|
||||
r.AssertError(out)
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue