Add OPKGUpdate() API
This commit is contained in:
parent
fc4e50bf08
commit
48db0458c0
|
@ -0,0 +1,55 @@
|
|||
package reach
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"forge.cadoles.com/Pyxis/golang-socketio"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
eventUpdate = "update"
|
||||
eventOPKGUpdateResult = "opkg update result"
|
||||
)
|
||||
|
||||
// OPKGUpdateStatus embeds informations about OPKG update status
|
||||
type OPKGUpdateStatus struct {
|
||||
Active bool `json:"active"`
|
||||
State string `json:"state"`
|
||||
Locked bool `json:"locked"`
|
||||
}
|
||||
|
||||
// OPKGUpdate asks the ReachRS module to start an OPKG update
|
||||
func (c *Client) OPKGUpdate() (*OPKGUpdateStatus, error) {
|
||||
|
||||
var err error
|
||||
var status *OPKGUpdateStatus
|
||||
var wg sync.WaitGroup
|
||||
|
||||
wg.Add(1)
|
||||
|
||||
err = c.conn.On(eventOPKGUpdateResult, func(h *gosocketio.Channel, st *OPKGUpdateStatus) {
|
||||
status = st
|
||||
c.conn.Off(eventOPKGUpdateResult)
|
||||
wg.Done()
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error while binding to '%s' event", eventOPKGUpdateResult)
|
||||
}
|
||||
|
||||
c.logf("sending '%s' event", eventUpdate)
|
||||
if err = c.conn.Emit(eventUpdate, nil); err != nil {
|
||||
return nil, errors.Wrapf(err, "error while emitting '%s' event", eventUpdate)
|
||||
}
|
||||
c.logf("'%s' event sent", eventUpdate)
|
||||
|
||||
wg.Wait()
|
||||
|
||||
c.logf(
|
||||
"opkg update result: active: %v, state: %v, locked: %v",
|
||||
status.Active, status.State, status.Locked,
|
||||
)
|
||||
|
||||
return status, err
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package reach
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestClientOPKGUpdate(t *testing.T) {
|
||||
|
||||
if !*runIntegrationTests {
|
||||
t.Skip("To run this test, use: go test -integration")
|
||||
}
|
||||
|
||||
client := NewClient(
|
||||
WithStandardLogger(),
|
||||
WithEndpoint(*reachHost, 80),
|
||||
)
|
||||
if err := client.Connect(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err := client.OPKGUpdate()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
defer client.Close()
|
||||
|
||||
}
|
Reference in New Issue