John McCabe 706761e92a Migrate CLI to Cobra and add experimental bash completion
This adds the following commands:
- faas-cli
- faas-cli help
- faas-cli build
- faas-cli deploy
- faas-cli remove (alias: rm)
- faas-cli version
- faas-cli push

Note that the following is also added but hidden from help pending a
more robust bash completion solution, initially using the Cobra
generated bash completion but needs spf13/cobra#520 to merge before
it'll work on the OSX default Bash 3.x.
- faas-cli bashcompletion

This commit intercepts the command line args passed to `faas-cli` and
attempts to translate them from the deprecated go flag based syntax
(`faas-cli -action xxx`) to the new Cobra verb/noun based syntax
(`faas-cli xxx`), it also translates a frozen set of legacy flags (with
the go-style single-dash) into a GNU style double-dash.

Note that some special cases are included:
- changing the delete action to remove
- passing the function name as a noun to remove rather than as an arg to
`-name`
- it also handles the legacy format where args are passed after =
(`-name=fnname`).

If the translation results in a new set of args then a message is
displayed to the user (stderr) telling warning that they are using the
deprecated cli syntax and also prints the new syntax command that is
being executed and which they should use going forward.

Any errors thrown during translation result in the command failing with
it printing the error cause to stderr.

This renames the `fetchTemplates.go` file to use snake case. The
convention appears to be for snakecase - as observed in both the Go and
Kubernetes source. For example heres a random selection of source files.

-
https://github.com/kubernetes/kubernetes/blob/master/pkg/kubeapiserver/default_storage_factory_builder.go
-
https://github.com/kubernetes/kubernetes/blob/master/pkg/kubectl/bash_comp_utils.go
-
https://github.com/golang/go/blob/master/src/compress/bzip2/move_to_front.go

Note that the language spec does not set a hard rule for source file
names, only for package names, but making this change for consistency.

Note that this file was initially generated by Cobra, but has been
tweaked to include some fixes.

It it an experimental initial version.

This commit adds some instructions on enabling the `faas-cli` bash
auto-completion support.

Instructions for Linux users are very light as it differs per-distro and
the assumption is that Linux users should be capable of following their
Distros instructions on enabling bash completion support.

Signed-off-by: John McCabe <john@johnmccabe.net>
2017-08-31 15:57:15 +01:00
2017-08-23 22:32:04 +01:00
2017-08-27 22:32:29 +01:00
2017-08-27 22:32:29 +01:00
2017-08-27 22:32:29 +01:00
2017-08-27 22:32:29 +01:00
2017-06-23 15:00:48 +01:00
2017-08-10 10:18:39 +01:00
2017-08-27 22:32:29 +01:00
2017-08-27 09:15:43 +01:00
2017-05-04 14:08:14 +01:00

faas-cli

Build Status

This is a CLI for use with OpenFaaS - a serverless functions framework for Docker & Kubernetes.

Before using this tool please setup OpenFaaS by following instructions over on the main repo.

The CLI can be used to build and deploy functions to OpenFaaS. You can build OpenFaaS functions from a set of supported language templates (such as Node.js, Python, CSharp and Ruby). That means you just write a handler file such as (handler.py/handler.js) and the CLI does the rest to create a Docker image.

Demo: ASCII cinema

Intall the CLI

The easiest way to install the faas-cli is through a curl script or brew:

$ curl -sSL https://cli.openfaas.com | sudo sh

or

$ brew install faas-cli

The contributing guide has instructions for building from source

Run the CLI

The main commands supported by the CLI are:

  • faas-cli build - builds Docker images from the supported language types
  • faas-cli push - pushes Docker images into a registry
  • faas-cli deploy - deploys the functions into a local or remote OpenFaaS gateway
  • faas-cli remove - removes the functions from a local or remote OpenFaaS gateway

Help for all of the commands supported by the CLI can be found by running:

  • faas-cli help or faas-cli [command] --help

You can chose between using a programming language template where you only need to provide a handler file, or a Docker that you can build yourself.

Templates

Specify lang: node/python/csharp/ruby

  • Supports common languages

  • Quick and easy - just write one file

  • Specify depenencies on Gemfile / requirements.txt or package.json etc

  • Customise the provided templates

Perhaps you need to have gcc or another dependency in your Python template? That's not a problem.

You can customise the Dockerfile or code for any of the templates. Just create a new directory and copy in the templates folder from this repository. The templates in your current working directory are always used for builds.

Docker image

Specify lang: Dockerfile if you want the faas-cli to execute a build or skip_build: true for pre-supplied images.

  • Ultimate versatility and control
  • Package anything
  • If you are using a stack file add the skip_build: true attribute
  • Use one of the samples as a basis

YAML files for ease of use

You can define individual functions or a set of of them within a YAML file. This makes the CLI easier to use and means you can use this file to deploy to your OpenFaaS instance.

Here is an example file using the samples.yml file included in the repository.

provider:
  name: faas
  gateway: http://localhost:8080

functions:
  url-ping:
    lang: python
    handler: ./sample/url-ping
    image: alexellis2/faas-urlping

This url-ping function is defined in the sample/url-ping folder makes use of Python. All we had to do was to write a handler.py file and then to list off any Python modules in requirements.txt.

  • Build the files in the .yml file:
$ faas-cli build -f ./samples.yml

-f specifies the file or URL to download your YAML file from. The long version of the -f flag is: --yaml.

You can also download over HTTP/s:

$ faas-cli build -f https://raw.githubusercontent.com/alexellis/faas-cli/master/samples.yml

Docker along with a Python template will be used to build an image named alexellis2/faas-urlping.

  • Deploy your function

Now you can use the following command to deploy your function(s):

$ faas-cli deploy -f ./samples.yml
  • Possible entries for functions are documented below:
functions:
  deployed_function_name:
    lang: node or python (optional)
    handler: ./path/to/handler (optional)
    image: docker-image-name
    environment:
      env1: value1
      env2: "value2"

Use environmental variables for setting tokens and configuration.

Accessing the function with curl

You can initiate a HTTP POST via curl:

  • with the -d flag i.e. -d "my data here"
  • or with --data-binary @filename.txt to send a whole file including newlines
  • if you want to pass input from STDIN then use --data-binary @-
$ curl -d '{"hello": "world"}' http://localhost:8080/function/nodejs-echo
{ nodeVersion: 'v6.9.1', input: '{"hello": "world"}' }

$ curl --data-binary @README.md http://localhost:8080/function/nodejs-echo

$ uname -a | curl http://localhost:8080/function/nodejs-echo--data-binary @-

For further instructions on the manual CLI flags (without using a YAML file) read manual_cli.md

Bash Auto-completion [experimental]

An experimental initial Bash auto-completion script for faas-cli is available at contrib/bash/faas-cli.

Please raise issues with feedback and suggestions on improvements to the auto-completion support.

This may be enabled it as follows.

Enabling Bash auto-completion on OSX

Brew install the bash_completions package.

$ brew install bash-completion

Add the following line to your ~/.bash_profile if not already present.

[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion

Copy the provided faas-cli bash completion script from this repo.

cp contrib/bash/faas-cli /usr/local/etc/bash_completion.d/

Enabling Bash auto-completion on Linux

Refer to your distributions instructions on installing and enabling bash-completion, then copy the faas-cli completion script from contrib/bash/ into the appropriate completion directory.

FaaS-CLI Developers / Contributors

See contributing guide.

License

This project is part of the OpenFaaS project licensed under the MIT License.

Languages
Go 94.7%
Shell 3.4%
JavaScript 1.2%
Dockerfile 0.4%
Makefile 0.3%