diff --git a/cmd/errors.go b/cmd/errors.go index 60d3361b4a..7d6e5f1eb9 100644 --- a/cmd/errors.go +++ b/cmd/errors.go @@ -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() } diff --git a/cmd/invoke.go b/cmd/invoke.go index 9ff96d37fa..7d7799e1ce 100644 --- a/cmd/invoke.go +++ b/cmd/invoke.go @@ -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 } @@ -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 diff --git a/cmd/invoke_test.go b/cmd/invoke_test.go index 7feae43f03..89cb8c8f54 100644 --- a/cmd/invoke_test.go +++ b/cmd/invoke_test.go @@ -7,6 +7,7 @@ import ( "net" "net/http" "os" + "strings" "sync/atomic" "testing" "time" @@ -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) + } +}