Skip to content
Merged
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ Create an API key from the [Kernel dashboard](https://dashboard.onkernel.com).

- `--version <version>`, `-v` - Specify app version (default: latest)
- `--payload <json>`, `-p` - JSON payload for the action
- `--payload-file <path>`, `-f` - Read JSON payload from a file (use `-` for stdin)
- `--sync`, `-s` - Invoke synchronously (timeout after 60s)

- `kernel app list` - List deployed apps
Expand Down Expand Up @@ -423,6 +424,15 @@ kernel invoke my-scraper scrape-page
# With JSON payload
kernel invoke my-scraper scrape-page --payload '{"url": "https://example.com"}'

# Read payload from a file
kernel invoke my-scraper scrape-page --payload-file payload.json

# Read payload from stdin
cat payload.json | kernel invoke my-scraper scrape-page --payload-file -

# Pipe from another command
echo '{"url": "https://example.com"}' | kernel invoke my-scraper scrape-page -f -

# Synchronous invoke (wait for completion)
kernel invoke my-scraper quick-task --sync
```
Expand Down
69 changes: 60 additions & 9 deletions cmd/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"os/signal"
"strings"
Expand Down Expand Up @@ -36,7 +37,9 @@ var invocationHistoryCmd = &cobra.Command{
func init() {
invokeCmd.Flags().StringP("version", "v", "latest", "Specify a version of the app to invoke (optional, defaults to 'latest')")
invokeCmd.Flags().StringP("payload", "p", "", "JSON payload for the invocation (optional)")
invokeCmd.Flags().StringP("payload-file", "f", "", "Path to a JSON file containing the payload (use '-' for stdin)")
invokeCmd.Flags().BoolP("sync", "s", false, "Invoke synchronously (default false). A synchronous invocation will open a long-lived HTTP POST to the Kernel API to wait for the invocation to complete. This will time out after 60 seconds, so only use this option if you expect your invocation to complete in less than 60 seconds. The default is to invoke asynchronously, in which case the CLI will open an SSE connection to the Kernel API after submitting the invocation and wait for the invocation to complete.")
invokeCmd.MarkFlagsMutuallyExclusive("payload", "payload-file")

invocationHistoryCmd.Flags().Int("limit", 100, "Max invocations to return (default 100)")
invocationHistoryCmd.Flags().StringP("app", "a", "", "Filter by app name")
Expand Down Expand Up @@ -64,15 +67,11 @@ func runInvoke(cmd *cobra.Command, args []string) error {
Async: kernel.Opt(!isSync),
}

payloadStr, _ := cmd.Flags().GetString("payload")
if cmd.Flags().Changed("payload") {
// validate JSON unless empty string explicitly set
if payloadStr != "" {
var v interface{}
if err := json.Unmarshal([]byte(payloadStr), &v); err != nil {
return fmt.Errorf("invalid JSON payload: %w", err)
}
}
payloadStr, hasPayload, err := getPayload(cmd)
if err != nil {
return err
}
if hasPayload {
params.Payload = kernel.Opt(payloadStr)
}
// we don't really care to cancel the context, we just want to handle signals
Expand Down Expand Up @@ -213,6 +212,58 @@ func printResult(success bool, output string) {
}
}

// getPayload reads the payload from either --payload flag or --payload-file flag.
// Returns the payload string, whether a payload was explicitly provided, and any error.
// The second return value (hasPayload) is true when the user explicitly set a payload,
// even if that payload is an empty string.
func getPayload(cmd *cobra.Command) (payload string, hasPayload bool, err error) {
payloadStr, _ := cmd.Flags().GetString("payload")
payloadFile, _ := cmd.Flags().GetString("payload-file")

// If --payload was explicitly set, use it (even if empty string)
if cmd.Flags().Changed("payload") {
// Validate JSON unless empty string explicitly set
if payloadStr != "" {
var v interface{}
if err := json.Unmarshal([]byte(payloadStr), &v); err != nil {
return "", false, fmt.Errorf("invalid JSON payload: %w", err)
}
}
return payloadStr, true, nil
}

// If --payload-file was set, read from file
if cmd.Flags().Changed("payload-file") {
var data []byte

if payloadFile == "-" {
// Read from stdin
data, err = io.ReadAll(os.Stdin)
if err != nil {
return "", false, fmt.Errorf("failed to read payload from stdin: %w", err)
}
} else {
// Read from file
data, err = os.ReadFile(payloadFile)
if err != nil {
return "", false, fmt.Errorf("failed to read payload file: %w", err)
}
}

payloadStr = strings.TrimSpace(string(data))
// Validate JSON unless empty
if payloadStr != "" {
var v interface{}
if err := json.Unmarshal([]byte(payloadStr), &v); err != nil {
return "", false, fmt.Errorf("invalid JSON in payload file: %w", err)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Misleading error message for stdin JSON validation failure

Low Severity

When reading from stdin using -f -, the JSON validation error message says "invalid JSON in payload file" which is incorrect for stdin input. The read error messages correctly differentiate between stdin and file cases (lines 243 and 249), but the JSON validation error message at line 258 is shared and uses the file-specific wording. Users piping data via stdin will see a confusing error referencing a "payload file" that doesn't exist.

Fix in Cursor Fix in Web

}
}
return payloadStr, true, nil
}

return "", false, nil
}

func runInvocationHistory(cmd *cobra.Command, args []string) error {
client := getKernelClient(cmd)

Expand Down