1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package zdaemon
- import (
- "fmt"
- "git.swzry.com/zry/pathutils"
- "html/template"
- "os"
- "path"
- "strconv"
- )
- const tpl_initd_openwrt = `#!/bin/sh /etc/rc.common
- START={{ .startnum }}
- STOP={{ .stopnum }}
- cmd={{ .executable }}
- start() {
- $cmd start
- }
- stop() {
- $cmd stop
- }
- `
- func (zd *ZDaemon) install_openwrt() {
- if zd.daemonConfig.InstallMode != "free" {
- fmt.Println("Failed install: already installed.")
- return
- }
- initsp := path.Join("/etc/init.d/", zd.daemonName)
- pe, _ := pathutils.PathExists(initsp)
- if pe {
- err := os.Remove(initsp)
- if err != nil {
- fmt.Println("Failed install, old file exist and error in deleting old file: ", err.Error())
- return
- }
- }
- otpl := template.New(zd.daemonName)
- tpl, err := otpl.Parse(tpl_initd_openwrt)
- if err != nil {
- fmt.Println("Failed install: ", err.Error())
- return
- }
- fids, err := os.OpenFile(initsp, os.O_WRONLY|os.O_CREATE, 0777)
- if err != nil {
- fmt.Println("Failed install: ", err.Error())
- return
- }
- tdat := map[string]string{
- "startnum": strconv.Itoa(zd.startOrder),
- "stopnum": strconv.Itoa(zd.stopOrder),
- "executable": zd.executableFileName,
- }
- err = tpl.Execute(fids, tdat)
- if err != nil {
- fmt.Println("Failed install: ", err.Error())
- _ = fids.Close()
- return
- }
- _ = fids.Close()
- zd.daemonConfig.InstallMode = "openwrt"
- zd.writeStatus()
- fmt.Println("Installed.")
- }
- func (zd *ZDaemon) uninstall_openwrt() {
- initsp := path.Join("/etc/init.d/", zd.daemonName)
- pe, _ := pathutils.PathExists(initsp)
- if pe {
- err := os.Remove(initsp)
- if err != nil {
- fmt.Println("Failed uninstall: ", err.Error())
- return
- }
- }
- zd.daemonConfig.InstallMode = "free"
- zd.writeStatus()
- fmt.Println("Uninstalled.")
- }
|