Skip to content

Commit 3eedefe

Browse files
committed
Improve the error description when no name is provided in func delete using 2 layer approach
Signed-off-by: RayyanSeliya <[email protected]>
1 parent 05a786b commit 3eedefe

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

cmd/delete.go

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

33
import (
4+
"errors"
45
"fmt"
56

67
"github.com/AlecAivazis/survey/v2"
@@ -36,7 +37,28 @@ No local files are deleted.
3637
PreRunE: bindEnv("path", "confirm", "all", "namespace", "verbose"),
3738
SilenceUsage: true, // no usage dump on error
3839
RunE: func(cmd *cobra.Command, args []string) error {
39-
return runDelete(cmd, args, newClient)
40+
// Layer 2: Catch technical errors and provide CLI-specific user-friendly messages
41+
err := runDelete(cmd, args, newClient)
42+
if err != nil && errors.Is(err, fn.ErrNameRequiredForDelete) {
43+
return fmt.Errorf(`%v
44+
45+
You can delete functions in two ways:
46+
47+
1. By name :
48+
func delete myfunction Delete function by name
49+
func delete myfunction --namespace apps Delete from specific namespace
50+
51+
2. By path :
52+
func delete --path /path/to/function Delete function at specific path
53+
54+
Examples:
55+
func delete myfunction Delete 'myfunction' from cluster
56+
func delete myfunction --namespace prod Delete from 'prod' namespace
57+
func delete --path ./myfunction Delete function at path
58+
59+
For more options, run 'func delete --help'`, err)
60+
}
61+
return err
4062
},
4163
}
4264

@@ -61,6 +83,19 @@ func runDelete(cmd *cobra.Command, args []string, newClient ClientFactory) (err
6183
if err != nil {
6284
return
6385
}
86+
87+
// If no name provided, check if function exists BEFORE prompting or connecting to cluster
88+
if cfg.Name == "" {
89+
f, err := fn.NewFunction(cfg.Path)
90+
if err != nil {
91+
return err
92+
}
93+
if !f.Initialized() {
94+
// Return technical error (Layer 1) - will be caught and enhanced by CLI
95+
return fn.ErrNameRequiredForDelete
96+
}
97+
}
98+
6499
if cfg, err = cfg.Prompt(); err != nil {
65100
return
66101
}

pkg/functions/errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ var (
2828
// eg "registry required". Then catch the error in the CLI and add the
2929
// cli-specific usage hints there
3030
ErrRegistryRequired = errors.New("registry required to build function, please set with `--registry` or the FUNC_REGISTRY environment variable")
31+
32+
// These are returned by core functions and caught by CLI for user-friendly messages
33+
ErrNameRequiredForDelete = errors.New("function name required for deletion")
3134
)
3235

3336
// ErrNotInitialized indicates that a function is uninitialized

0 commit comments

Comments
 (0)