Add a "fission function edit" command

The new command invokes editor on a copy of the source code, and
uploads the new source code.  It's an easy way to manually edit
functions from a terminal.
This commit is contained in:
Soam Vasani
2016-11-15 15:51:47 -08:00
parent 2a9f229c17
commit 1a19f718d4
2 changed files with 54 additions and 0 deletions
+53
View File
@@ -20,6 +20,7 @@ import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"text/tabwriter"
"github.com/urfave/cli"
@@ -155,3 +156,55 @@ func fnList(c *cli.Context) error {
return err
}
func fnEdit(c *cli.Context) error {
client := getClient(c.GlobalString("server"))
fnName := c.String("name")
if len(fnName) == 0 {
fatal("Need name of function, use --name")
}
fnUid := c.String("uid")
// get function meta
function, err := client.FunctionGet(&fission.Metadata{Name: fnName, Uid: fnUid})
checkErr(err, fmt.Sprintf("read function '%v'", fnName))
// write to tmp file
tmpFile, err := ioutil.TempFile("", fnName)
checkErr(err, "create temp file")
defer os.Remove(tmpFile.Name())
_, err = tmpFile.Write([]byte(function.Code))
checkErr(err, "write temp file")
tmpFile.Close()
// invoke $EDITOR on tmp file and wait for it
editor := os.Getenv("EDITOR")
if len(editor) == 0 {
editor = "vi"
}
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("%v %v", editor, tmpFile.Name()))
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
checkErr(err, "start editor")
err = cmd.Wait()
checkErr(err, "wait for editor")
// read new code out of the file
contents, err := ioutil.ReadFile(tmpFile.Name())
checkErr(err, "read temp file")
function.Code = string(contents)
// upload the updated function
newfn, err := client.FunctionUpdate(function)
checkErr(err, "upload edited function")
fmt.Printf("function %v updated, new uuid: %v\n", newfn.Name, newfn.Uid)
return nil
}