From 12cfdaea9946ddd327da356c1e5587c5c31520fb Mon Sep 17 00:00:00 2001 From: fpkoehler Date: Mon, 7 Mar 2022 13:54:31 -0800 Subject: [PATCH 1/2] Adding AddCommandWithRevsion() Provides the ability to specify the revision of the command to execute. --- eapi.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/eapi.go b/eapi.go index 066546e..59a1bdb 100644 --- a/eapi.go +++ b/eapi.go @@ -182,6 +182,41 @@ func (handle *EapiReqHandle) AddCommand(v EapiCommand) error { return AddCommandStr(handle, command, v) } +// AddCommandRevisionStr adds a command string with specified EapiCommand type and +// revision to the command block list for this EapiReqHandle. +func AddCommandWithRevisionStr(handle *EapiReqHandle, command string, revision int, v EapiCommand) error { + if err := handle.checkHandle(); err != nil { + return err + } + if command == "" { + handle.err = fmt.Errorf("Invalid null Command string") + return handle.err + } + + if len(handle.eapiCommands) == maxCmdBuflen { + handle.err = fmt.Errorf("Limit of %d commands reached for AddCommand", + maxCmdBuflen) + return handle.err + } + + type RevisionCommand struct { + Cmd string `json:"cmd"` + Revision int `json:"revision"` + } + + cmd := RevisionCommand{Cmd: command, Revision: revision} + cmdBlock := commandBlock{command: cmd, EapiCommand: v} + handle.eapiCommands = append(handle.eapiCommands, cmdBlock) + return nil +} + +// AddCommandRevision adds a pre-defined EapiCommand type of specified revsion +// to the command block list for this EapiReqHandle. +func (handle *EapiReqHandle) AddCommandWithRevsion(v EapiCommand, revision int) error { + command := v.GetCmd() + return AddCommandWithRevisionStr(handle, command, revision, v) +} + // getAllCommands iterates through the list of command blocks // and returns the commands as an array of interfaces. func (handle *EapiReqHandle) getAllCommands() []interface{} { From 5e9543143d81335c4755325645b72c63f983b091 Mon Sep 17 00:00:00 2001 From: fpkoehler Date: Wed, 9 Mar 2022 16:10:52 -0800 Subject: [PATCH 2/2] Update eapi.go Refactor to consolidate common code between AddCommandStr() and AddCommandWithRevisionStr() And changes fro "go fmt" --- eapi.go | 66 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/eapi.go b/eapi.go index 59a1bdb..c3aac54 100644 --- a/eapi.go +++ b/eapi.go @@ -141,9 +141,8 @@ func (handle *EapiReqHandle) getNode() (*Node, error) { return handle.node, nil } -// AddCommandStr adds a command string with specified EapiCommand type to the -// command block list for this EapiReqHandle. -func AddCommandStr(handle *EapiReqHandle, command string, v EapiCommand) error { +// commandCheck verifies command and handle are ok to be added +func commandCheck(handle *EapiReqHandle, command string) error { if err := handle.checkHandle(); err != nil { return err } @@ -157,6 +156,16 @@ func AddCommandStr(handle *EapiReqHandle, command string, v EapiCommand) error { maxCmdBuflen) return handle.err } + return nil +} + +// AddCommandStr adds a command string with specified EapiCommand type to the +// command block list for this EapiReqHandle. +func AddCommandStr(handle *EapiReqHandle, command string, v EapiCommand) error { + err := commandCheck(handle, command) + if err != nil { + return err + } cmd := commandBlock{command: command, EapiCommand: v} handle.eapiCommands = append(handle.eapiCommands, cmd) return nil @@ -185,36 +194,27 @@ func (handle *EapiReqHandle) AddCommand(v EapiCommand) error { // AddCommandRevisionStr adds a command string with specified EapiCommand type and // revision to the command block list for this EapiReqHandle. func AddCommandWithRevisionStr(handle *EapiReqHandle, command string, revision int, v EapiCommand) error { - if err := handle.checkHandle(); err != nil { - return err - } - if command == "" { - handle.err = fmt.Errorf("Invalid null Command string") - return handle.err - } - - if len(handle.eapiCommands) == maxCmdBuflen { - handle.err = fmt.Errorf("Limit of %d commands reached for AddCommand", - maxCmdBuflen) - return handle.err - } - - type RevisionCommand struct { - Cmd string `json:"cmd"` - Revision int `json:"revision"` - } - - cmd := RevisionCommand{Cmd: command, Revision: revision} - cmdBlock := commandBlock{command: cmd, EapiCommand: v} - handle.eapiCommands = append(handle.eapiCommands, cmdBlock) - return nil + err := commandCheck(handle, command) + if err != nil { + return err + } + + type RevisionCommand struct { + Cmd string `json:"cmd"` + Revision int `json:"revision"` + } + + cmd := RevisionCommand{Cmd: command, Revision: revision} + cmdBlock := commandBlock{command: cmd, EapiCommand: v} + handle.eapiCommands = append(handle.eapiCommands, cmdBlock) + return nil } // AddCommandRevision adds a pre-defined EapiCommand type of specified revsion // to the command block list for this EapiReqHandle. func (handle *EapiReqHandle) AddCommandWithRevsion(v EapiCommand, revision int) error { - command := v.GetCmd() - return AddCommandWithRevisionStr(handle, command, revision, v) + command := v.GetCmd() + return AddCommandWithRevisionStr(handle, command, revision, v) } // getAllCommands iterates through the list of command blocks @@ -286,7 +286,7 @@ func (handle *EapiReqHandle) Call() error { commands := handle.getAllCommands() jsonrsp, err := handle.node.conn.Execute(commands, handle.encoding) - + if err != nil { return err } @@ -352,12 +352,12 @@ func (handle *EapiReqHandle) parseResponse(resp *JSONRPCResponse) error { continue } - d, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{ TagName: "json", Result: cmd.EapiCommand }) - if err != nil { + d, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{TagName: "json", Result: cmd.EapiCommand}) + if err != nil { return err - } + } - err = d.Decode(result) + err = d.Decode(result) if err != nil { return err }