owrt/system.go

42 lines
1019 B
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(param ...string) *SysAction {
cmd := param[0]
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 {
cmd := fmt.Sprintf("/etc/init.d/%s ", name)
return s.sysRun(cmd, action)
}