Skip to content

Commit 71a1f6a

Browse files
authored
Improve the error description when no name is provided in func delete (#3054)
* Improve the error description when no name is provided in func delete using 2 layer approach Signed-off-by: RayyanSeliya <rayyanseliya786@gmail.com> * refactor to use the existing name required error in layer 1 Signed-off-by: RayyanSeliya <rayyanseliya786@gmail.com> * refactor the logic based on the suggestion Signed-off-by: RayyanSeliya <rayyanseliya786@gmail.com> --------- Signed-off-by: RayyanSeliya <rayyanseliya786@gmail.com>
1 parent bfd65ae commit 71a1f6a

1 file changed

Lines changed: 55 additions & 1 deletion

File tree

cmd/delete.go

Lines changed: 55 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,12 @@ 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.ErrNameRequired) {
43+
return NewErrDeleteNameRequired(err)
44+
}
45+
return err
4046
},
4147
}
4248

@@ -61,6 +67,19 @@ func runDelete(cmd *cobra.Command, args []string, newClient ClientFactory) (err
6167
if err != nil {
6268
return
6369
}
70+
71+
// If no name provided, check if function exists BEFORE prompting or connecting to cluster
72+
if cfg.Name == "" {
73+
f, err := fn.NewFunction(cfg.Path)
74+
if err != nil {
75+
return err
76+
}
77+
if !f.Initialized() {
78+
// Return technical error (Layer 1) - will be caught and enhanced by CLI
79+
return fn.ErrNameRequired
80+
}
81+
}
82+
6483
if cfg, err = cfg.Prompt(); err != nil {
6584
return
6685
}
@@ -156,3 +175,38 @@ func (c deleteConfig) Prompt() (deleteConfig, error) {
156175

157176
return dc, err
158177
}
178+
179+
// ErrDeleteNameRequired wraps core library errors with CLI-specific context
180+
// for delete operations that require a function name or path.
181+
type ErrDeleteNameRequired struct {
182+
// Underlying error from the core library (e.g., fn.ErrNameRequired)
183+
Err error
184+
}
185+
186+
// NewErrDeleteNameRequired creates a new ErrDeleteNameRequired wrapping the given error
187+
func NewErrDeleteNameRequired(err error) ErrDeleteNameRequired {
188+
return ErrDeleteNameRequired{Err: err}
189+
}
190+
191+
// Error implements the error interface with CLI-specific help text
192+
func (e ErrDeleteNameRequired) Error() string {
193+
return fmt.Sprintf(`%v
194+
195+
Function name is required for deletion (or --path not specified).
196+
197+
You can delete functions in two ways:
198+
199+
1. By name:
200+
func delete myfunction Delete function by name
201+
func delete myfunction --namespace apps Delete from specific namespace
202+
203+
2. By path:
204+
func delete --path /path/to/function Delete function at specific path
205+
206+
Examples:
207+
func delete myfunction Delete 'myfunction' from cluster
208+
func delete myfunction --namespace prod Delete from 'prod' namespace
209+
func delete --path ./myfunction Delete function at path
210+
211+
For more options, run 'func delete --help'`, e.Err)
212+
}

0 commit comments

Comments
 (0)