Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cmd/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,26 @@ Or use --path to delete from anywhere:

For more options, run 'func delete --help'`, e.Err)

case "invoke":
return fmt.Sprintf(`%v

No function found in provided path (current directory or via --path).
You need to be inside a function directory to invoke it (or use --path).

Try this:
func create --language go myfunction Create a new function
cd myfunction Go into the function directory
func invoke Invoke the function

Or if you have an existing function:
cd path/to/your/function Go to your function directory
func invoke Invoke the function

Or use --path to invoke from anywhere:
func invoke --path /path/to/function

For more options, run 'func invoke --help'`, e.Err)

default:
return e.Err.Error()
}
Expand Down
8 changes: 3 additions & 5 deletions cmd/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,10 @@ func runInvoke(cmd *cobra.Command, _ []string, newClient ClientFactory) (err err
if err != nil {
return
}
if !f.Initialized() {
return NewErrNotInitializedFromPath(f.Root, "invoke")
}
if err = f.Validate(); err != nil {
fmt.Printf("error validating function at '%v'. %v\n", f.Root, err)
return err
}

Expand All @@ -162,10 +164,6 @@ func runInvoke(cmd *cobra.Command, _ []string, newClient ClientFactory) (err err
cfg.Format, f.Invoke, cfg.Format)
}

if !f.Initialized() {
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")
}

// If extensions were provided, ensure the format is cloudevent, otherwise return an error.
if len(cfg.Extensions) > 0 {
effectiveFormat := cfg.Format
Expand Down
23 changes: 23 additions & 0 deletions cmd/invoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"net/http"
"os"
"strings"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -78,3 +79,25 @@ func TestInvoke(t *testing.T) {
t.Fatal("function was not invoked")
}
}

// TestInvoke_NotInitialized ensures that invoking when there is no
// function in the given directory fails with the appropriate error.
func TestInvoke_NotInitialized(t *testing.T) {
_ = FromTempDirectory(t)

cmd := NewInvokeCmd(NewClient)
cmd.SetArgs([]string{})
err := cmd.Execute()

if err == nil {
t.Fatal("invoking a nonexistent function should error")
}

var errNotInit *ErrNotInitialized
if !errors.As(err, &errNotInit) {
t.Fatalf("expected ErrNotInitialized, got %T: %v", err, err)
}
if !strings.Contains(err.Error(), "No function found in provided path") {
t.Fatalf("Unexpected error text returned: %v", err)
}
}
Loading