# HG changeset patch # User telesto # Date 1418669749 -3600 # Mon Dec 15 19:55:49 2014 +0100 # Node ID 913280b646ff889bf3cfd53fd93d8687f2716618 # Parent 6ec535ad4030984af5160fbc17ce00086e58fe21 add LICENSE; continured lua command execution diff --git a/LICENSE b/LICENSE new file mode 100644 --- /dev/null +++ b/LICENSE @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/README.md b/README.md --- a/README.md +++ b/README.md @@ -2,6 +2,9 @@ ========= [hackurist](https://bitbucket.com/telesto/hackurist) is made up of a client library and a client command line program to access a running [dfhack](https://github.com/DFHack/dfhack) rpc server. It is written in [Go](http://golang.org/). +News +---- + client library -------------- Installation: diff --git a/cmd/hackurist/main.go b/cmd/hackurist/main.go --- a/cmd/hackurist/main.go +++ b/cmd/hackurist/main.go @@ -13,10 +13,12 @@ flag.PrintDefaults() } +var address = flag.String("a", "127.0.0.1:5000", "dfhack rpc server address") +var debug = flag.Bool("d", false, "print debug messages") + func main() { - address := flag.String("a", "127.0.0.1:5000", "dfhack rpc server address") - debug := flag.Bool("d", false, "print debug messages") help := flag.Bool("h", false, "help") + lua := flag.Bool("lua", false, "run lua command") flag.Parse() if *help { @@ -27,8 +29,12 @@ usage() os.Exit(2) } - cmd := flag.Arg(0) - err := run(*address, cmd, flag.Args()[1:], *debug) + var err error + if *lua { + err = runCommand() + } else { + err = runLuaCommand() + } if err != nil { log.Println(err) os.Exit(1) @@ -36,8 +42,8 @@ os.Exit(0) } -func run(address, cmd string, args []string, debug bool) error { - conn, err := hackurist.Dial(address, debug) +func runLuaCommand() error { + conn, err := hackurist.Dial(*address, *debug) if err != nil { return err } @@ -46,10 +52,28 @@ if err != nil { return err } - result, err := conn.RunCommand(cmd, args) + result, err := conn.RunLuaCommand(flag.Arg(0), flag.Arg(1), flag.Args()[2:]) fmt.Print(string(result)) if err != nil { return err } return nil } + +func runCommand() error { + conn, err := hackurist.Dial(*address, *debug) + if err != nil { + return err + } + defer conn.Close() + err = conn.ShakeHands() + if err != nil { + return err + } + result, err := conn.RunCommand(flag.Arg(0), flag.Args()[1:]) + fmt.Print(string(result)) + if err != nil { + return err + } + return nil +} diff --git a/hackurist.go b/hackurist.go --- a/hackurist.go +++ b/hackurist.go @@ -15,9 +15,10 @@ type headerId int16 const ( - requestBindMethod headerId = 0 // n/a - requestRunCommand headerId = 1 // n/a - requestQuit headerId = -4 // RPC_REQUEST_QUIT + requestBindMethod headerId = 0 // n/a + requestRunCommand headerId = 1 // n/a + requestRunLuaCommand headerId = 4 // added in RemoteTools.cpp by addMethod() + requestQuit headerId = -4 // RPC_REQUEST_QUIT responseResult headerId = -1 // RPC_REPLY_RESULT responseFail headerId = -2 // RPC_REPLY_FAIL @@ -201,6 +202,22 @@ return response, nil } +// RunLuaCommand sends lua command to the server. +// It reads the response, processes it and returns the output +// ready for a text client. +func (c Conn) RunLuaCommand(module, fn string, args []string) ([]byte, error) { + protoCmd, err := proto.Marshal(&dfproto.CoreRunLuaRequest{Module: &module, + Function: &fn, Arguments: args}) + if err != nil { + return nil, fmt.Errorf("RunCommand: %s", err) + } + if err = c.write(protoCmd); err != nil { + return nil, fmt.Errorf("RunLuaCommand: %s", err) + } + + return nil, nil +} + // todo: timeout func (c Conn) write(b []byte) error { written, err := c.conn.Write(b)