Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding AddCommandWithRevsion() #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 43 additions & 8 deletions eapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -182,6 +191,32 @@ 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 {
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)
}

// getAllCommands iterates through the list of command blocks
// and returns the commands as an array of interfaces.
func (handle *EapiReqHandle) getAllCommands() []interface{} {
Expand Down Expand Up @@ -251,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
}
Expand Down Expand Up @@ -317,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
}
Expand Down