From d846b4e78ecdc5274424b87cd767c264be412733 Mon Sep 17 00:00:00 2001 From: Yad Smood Date: Mon, 20 Jan 2020 18:06:50 +0900 Subject: [PATCH] add builtin cmd writefile --- client/nodejs/package-lock.json | 2 +- client/serve.go | 27 +++++++++++++++++++++++++++ cmd/digto/digto.go | 2 +- server/version.go | 2 +- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/client/nodejs/package-lock.json b/client/nodejs/package-lock.json index 9914a9a..6d2df49 100644 --- a/client/nodejs/package-lock.json +++ b/client/nodejs/package-lock.json @@ -1,6 +1,6 @@ { "name": "digto", - "version": "1.0.0", + "version": "1.0.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/client/serve.go b/client/serve.go index 9ebc476..5efbdc9 100644 --- a/client/serve.go +++ b/client/serve.go @@ -2,6 +2,7 @@ package client import ( "bytes" + "errors" "fmt" "io" "io/ioutil" @@ -112,6 +113,18 @@ func (c *Client) ServeExec() { return } + if isBuiltin, fn := c.execBuiltin(args); isBuiltin { + err := fn() + if err != nil { + resErr(send, err.Error()) + } + err = send(http.StatusOK, nil, nil) + if err != nil { + resErr(send, err.Error()) + } + return + } + cmd := exec.Command(args[0], args[1:]...) stdout, err := cmd.StdoutPipe() @@ -140,6 +153,20 @@ func (c *Client) ServeExec() { } } +func (c *Client) execBuiltin(args []string) (bool, func() error) { + switch args[0] { + case "writefile": + return true, func() error { + if len(args) < 3 { + return errors.New("writefile requires 2 args") + } + return kit.OutputFile(args[1], args[2], nil) + } + default: + return false, nil + } +} + func resErr(send Send, msg string) { err := send(http.StatusInternalServerError, nil, bytes.NewBufferString(msg)) if err != nil { diff --git a/cmd/digto/digto.go b/cmd/digto/digto.go index 776a95b..e0f400a 100644 --- a/cmd/digto/digto.go +++ b/cmd/digto/digto.go @@ -68,7 +68,7 @@ func proxy(cmd kit.TaskCmd) func() { func exec(cmd kit.TaskCmd) func() { subdomain := cmd.Arg("subdomain", "the subdomain to use, default is random string").Required().String() - args := cmd.Arg("cmd", "the tcp address to proxy to").Required().Strings() + args := cmd.Arg("cmd", "the tcp address to proxy to. Builtin cmds: writefile(path, str)").Required().Strings() return func() { c := client.New(*subdomain) diff --git a/server/version.go b/server/version.go index dd05b47..4b8c377 100644 --- a/server/version.go +++ b/server/version.go @@ -1,4 +1,4 @@ package server // Version ... -const Version = "v1.5.3" +const Version = "v1.5.4"