Skip to content

Commit

Permalink
add builtin cmd writefile
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Jan 20, 2020
1 parent 505e1b2 commit d846b4e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion client/nodejs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions client/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/digto/digto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion server/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server

// Version ...
const Version = "v1.5.3"
const Version = "v1.5.4"

0 comments on commit d846b4e

Please sign in to comment.