Skip to content

Commit 066d374

Browse files
committed
fix(cmd): use NewErrNotInitializedFromPath for invoke consistency
1 parent 7fb6180 commit 066d374

3 files changed

Lines changed: 46 additions & 4 deletions

File tree

cmd/errors.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,26 @@ Or use --path to delete from anywhere:
412412
413413
For more options, run 'func delete --help'`, e.Err)
414414

415+
case "invoke":
416+
return fmt.Sprintf(`%v
417+
418+
No function found in provided path (current directory or via --path).
419+
You need to be inside a function directory to invoke it (or use --path).
420+
421+
Try this:
422+
func create --language go myfunction Create a new function
423+
cd myfunction Go into the function directory
424+
func invoke Invoke the function
425+
426+
Or if you have an existing function:
427+
cd path/to/your/function Go to your function directory
428+
func invoke Invoke the function
429+
430+
Or use --path to invoke from anywhere:
431+
func invoke --path /path/to/function
432+
433+
For more options, run 'func invoke --help'`, e.Err)
434+
415435
default:
416436
return e.Err.Error()
417437
}

cmd/invoke.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ func runInvoke(cmd *cobra.Command, _ []string, newClient ClientFactory) (err err
150150
if err != nil {
151151
return
152152
}
153+
if !f.Initialized() {
154+
return NewErrNotInitializedFromPath(f.Root, "invoke")
155+
}
153156
if err = f.Validate(); err != nil {
154157
fmt.Printf("error validating function at '%v'. %v\n", f.Root, err)
155158
return err
@@ -162,10 +165,6 @@ func runInvoke(cmd *cobra.Command, _ []string, newClient ClientFactory) (err err
162165
cfg.Format, f.Invoke, cfg.Format)
163166
}
164167

165-
if !f.Initialized() {
166-
return fmt.Errorf("no function found in current directory.\nYou need to be inside a function directory to invoke it.\n\nTry this:\n func create --language go myfunction Create a new function\n cd myfunction Go into the function directory\n func invoke Now you can invoke it\n\nOr if you have an existing function:\n cd path/to/your/function Go to your function directory\n func invoke Invoke the function")
167-
}
168-
169168
// If extensions were provided, ensure the format is cloudevent, otherwise return an error.
170169
if len(cfg.Extensions) > 0 {
171170
effectiveFormat := cfg.Format

cmd/invoke_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net"
88
"net/http"
99
"os"
10+
"strings"
1011
"sync/atomic"
1112
"testing"
1213
"time"
@@ -78,3 +79,25 @@ func TestInvoke(t *testing.T) {
7879
t.Fatal("function was not invoked")
7980
}
8081
}
82+
83+
// TestInvoke_NotInitialized ensures that invoking when there is no
84+
// function in the given directory fails with the appropriate error.
85+
func TestInvoke_NotInitialized(t *testing.T) {
86+
_ = FromTempDirectory(t)
87+
88+
cmd := NewInvokeCmd(NewClient)
89+
cmd.SetArgs([]string{})
90+
err := cmd.Execute()
91+
92+
if err == nil {
93+
t.Fatal("invoking a nonexistent function should error")
94+
}
95+
96+
var errNotInit *ErrNotInitialized
97+
if !errors.As(err, &errNotInit) {
98+
t.Fatalf("expected ErrNotInitialized, got %T: %v", err, err)
99+
}
100+
if !strings.Contains(err.Error(), "No function found in provided path") {
101+
t.Fatalf("Unexpected error text returned: %v", err)
102+
}
103+
}

0 commit comments

Comments
 (0)