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:
@@ -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
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ func main() {
|
||||
fnSubcommands := []cli.Command{
|
||||
{Name: "create", Usage: "Create new function", Flags: []cli.Flag{fnNameFlag, fnEnvNameFlag, fnCodeFlag}, Action: fnCreate},
|
||||
{Name: "get", Usage: "Get function source code", Flags: []cli.Flag{fnNameFlag, fnUidFlag}, Action: fnGet},
|
||||
{Name: "edit", Usage: "Edit function source code in $EDITOR", Flags: []cli.Flag{fnNameFlag, fnUidFlag}, Action: fnEdit},
|
||||
{Name: "getmeta", Usage: "Get function metadata", Flags: []cli.Flag{fnNameFlag, fnUidFlag}, Action: fnGetMeta},
|
||||
{Name: "update", Usage: "Update function source code", Flags: []cli.Flag{fnNameFlag, fnEnvNameFlag, fnCodeFlag}, Action: fnUpdate},
|
||||
{Name: "delete", Usage: "Delete function", Flags: []cli.Flag{fnNameFlag, fnUidFlag}, Action: fnDelete},
|
||||
|
||||
Reference in New Issue
Block a user