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.
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.
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
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
Please see the runtime documentation for how to deploy and invoke the function.
To count the words in the US declaration of independence:
Download a file with the text.
curl -LO https://raw.githubusercontent.com/projectriff-samples/command-wordcount/master/us-declaration-of-independence.txt
Deploy the function as
wcE.g. on the Core runtime.
pfs core deployer create wc --function-ref wordcount --tail
In a separate terminal, port-forward to the
kubectl port-forward service/wc-deployer 8080:80
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