Add cmdjStruct to fill a struct passed by reference ##golang

* Add test example
This commit is contained in:
Anderson authored and GitHub committed 2022-01-13 00:17:26 +01:00
1 parent bf06efdf31
commit 8915471092
2 files changed
+37 -2

No files matched your search

+16
View File
@@ -203,11 +203,27 @@ func (r2p *Pipe) Cmdj(cmd string) (out interface{}, err error) {
return out, err
}
// CmdjStruct acts like Cmdjs but it will fill the interface/struct with the wanted values. It
// returns the command execution error.
func (r2p *Pipe) CmdjStruct(cmd string, out interface{}) (err error) {
rstr, err := r2p.Cmd(cmd)
if err == nil {
err = json.Unmarshal([]byte(rstr), out)
}
return err
}
//like cmdj but formats the command
func (r2p *Pipe) Cmdjf(f string, args ...interface{}) (interface{}, error) {
return r2p.Cmdj(fmt.Sprintf(f, args...))
}
// like Cmdj, but besides format the command it will already fill the interface sent
func (r2p *Pipe) CmdjfStruct(f string, out interface{}, args ...interface{}) error {
return r2p.CmdjStruct(fmt.Sprintf(f, args...), out)
}
// Close shuts down r2, closing the created pipe.
func (r2p *Pipe) Close() error {
if r2p.close != nil {
+21 -2
View File
@@ -2,8 +2,15 @@
package r2pipe
import "testing"
import "fmt"
import (
"fmt"
"testing"
)
type Offset struct {
Offset uint
Current bool
}
func TestCmd(t *testing.T) {
fmt.Println("[*] Testing r2 spawn pipe")
@@ -26,4 +33,16 @@ func TestCmd(t *testing.T) {
if buf != check {
t.Errorf("buf=%v; want=%v", buf, check)
}
offset := Offset{}
r2p.CmdjStruct("sj ~{0}", &offset)
if !offset.Current {
t.Errorf("CurrentOffset=%v; want=%v", offset.Current, true)
}
r2p.CmdjfStruct("sj ~{%d}", &offset, 0)
if !offset.Current {
t.Errorf("CurrentOffset=%v; want=%v", offset.Current, true)
}
}