owrt/system.go

47 lines
1.2 KiB
Go
Raw Normal View History

package owrt
import (
"fmt"
)
// SysAction is the result of an UCI action output and return code
type SysAction struct {
*CommandResult
Command string
}
// System "Object", an inteface with openwrt system
type System struct {
exec Executor
}
// NewSystem return an OpenWRT System instance to interact with UCI
func NewSystem() *System {
exec := &localExecutor{}
return &System{exec}
}
// NewSystemWithExecutor returns a System Instance an gives you the ability to provide
// a different command executor than the default one.
func NewSystemWithExecutor(exec Executor) *System {
return &System{exec}
}
// sysRun, private method to run the UCI command
func (s *System) sysRun(cmd string, param ...string) *SysAction {
res := s.exec.Run(cmd, param...)
return &SysAction{res, fmt.Sprintf("%s %s", cmd, param)}
}
// Service implementation of "service" command
func (s *System) Service(name string, action string) *SysAction {
2018-10-26 15:08:36 +02:00
cmd := fmt.Sprintf("/etc/init.d/%s", name)
return s.sysRun(cmd, action)
}
// Opkg provides an interface to system command opkg
// useful for package installation or system update
func (s *System) Opkg(params ...string) *SysAction {
return s.sysRun("opkg", params...)
}