Command Functions

This topic introduces Pivotal Function Service (PFS) command functions and provides an example built using the pfs CLI.

Command functions are functions that can be executed by themselves in PFS. They are either shell scripts, with the executable permission set, or a compiled binary program that doesn’t require any particular runtime or dynamic library to be present.

Command functions are handled by the riff command-function-invoker on GitHub.

Requirements

How Command Functions Work

Command functions use standard input (stdin) and standard output (stdout) streams.

For each invocation, functions are expected to read stdin until the end of the stream (EOF) and provide a result on stdout.

Correct function execution is assumed if the exit code is zero. Any other value indicates an error.

Create a Command function

This example uses the sample command-wordcount function from GitHub. It consists of a single executable file named wordcount.sh with the following content:

#!/bin/bash

tr [:punct:] ' ' | tr -s ' ' '\n' | tr [:upper:] [:lower:] | sort | uniq -c | sort -n

Create a wordcount function by running the CLI command below:

pfs function create wordcount \
--git-repo https://github.com/projectriff-samples/command-wordcount \
--artifact wordcount.sh \
--tail

Deploying a function

Please see the runtime documentation for how to deploy and invoke the function.

To count the words in the US declaration of independence:

  1. Download a file with the text.

    curl -LO https://raw.githubusercontent.com/projectriff-samples/command-wordcount/master/us-declaration-of-independence.txt
    
  2. Deploy the function as wc E.g. on the Core runtime.

    pfs core deployer create wc --function-ref wordcount --tail
    
  3. In a separate terminal, port-forward to the wc-deployer service.

    kubectl port-forward service/wc-deployer 8080:80
    
  4. Invoke the function with curl using -d @<file-name> to POST the content from a file.

    curl localhost:8080 -v -w '\n' -H 'Content-Type: text/plain' -d @us-declaration-of-independence.txt