2018-10-26 14:59:08 +02:00
|
|
|
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
|
2018-10-26 15:16:55 +02:00
|
|
|
func (s *System) sysRun(cmd string, param ...string) *SysAction {
|
2018-10-26 14:59:08 +02:00
|
|
|
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)
|
2018-10-26 14:59:08 +02:00
|
|
|
return s.sysRun(cmd, action)
|
|
|
|
}
|
2018-10-26 15:16:55 +02:00
|
|
|
|
|
|
|
// 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...)
|
|
|
|
}
|