diff --git a/system.go b/system.go new file mode 100644 index 0000000..e0f00af --- /dev/null +++ b/system.go @@ -0,0 +1,41 @@ +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) +}