You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ntool/nsys/cmdn/response.go

41 lines
738 B
Go

package cmdn
import (
"errors"
"fmt"
"regexp"
"strings"
)
type IResponse interface {
// MessageID 消息ID
MessageID() string
}
type PlainResp struct {
ID string
Command string
Result string
Err error
}
func (r *PlainResp) MessageID() string {
return r.ID
}
func (r *PlainResp) GetResult() (string, error) {
reg, _ := regexp.Compile(`\t`)
result := strings.TrimSpace(reg.ReplaceAllString(r.Result, " "))
res := strings.Fields(result)
l := len(res)
if l > 0 {
if res[0] == "=" {
return strings.TrimSpace(strings.Join(res[1:], " ")), nil
} else if res[0] == "?" {
return "", errors.New(strings.Join(res[1:], " "))
}
}
return "", errors.New(fmt.Sprintf("错误(未知应答): %s", r.Err))
}