Command Functions

Page last updated:

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

  • PFS has been installed and you have prepared a namespace.
  • The pfs CLI has been installed.

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.

Using the pfs CLI to 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 \
--verbose

or in Windows PowerShell

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

The CLI output should show that the command invoker was selected.

For example:

-----> Process types:
web:      /workspace/io.projectriff.riff/riff-invoker-command/command-function-invoker
function: /workspace/io.projectriff.riff/riff-invoker-command/command-function-invoker

Now, let’s count the occurrences of words in the US Declaration of Independence by posting the contents of the document to the function using pfs service invoke:

curl -s https://raw.githubusercontent.com/projectriff-samples/command-wordcount/master/us-declaration-of-independence.txt \
    | pfs service invoke wordcount --text -- -d @- 
...
19 in
20 has
20 their
22 our
27 for
44 and
58 to
65 the
67 of

Creating a function from local source

To build a function from a local directory, use --local-path instead of --git-repo.

E.g. if you are in a directory with an executable file called wordcount.sh just like the one above:

pfs function create wordcount \
--local-path . \
--artifact wordcount.sh \
--verbose

or in Windows PowerShell

pfs function create wordcount `
--local-path . `
--artifact wordcount.sh `
--verbose

To update the function and deploy a new revision:

pfs function update wordcount --local-path .