This commit is contained in:
W192547975
2024-07-15 12:38:45 +08:00
committed by GitHub

View File

@@ -27,35 +27,20 @@ func install() {
gLog.Println(LvERROR, "cd error:", err)
return
}
uninstall()
// save config file
parseParams("install", "")
targetPath := filepath.Join(defaultInstallPath, defaultBinName)
d := daemon{}
// copy files
binPath, _ := os.Executable()
src, errFiles := os.Open(binPath) // can not use args[0], on Windows call openp2p is ok(=openp2p.exe)
if errFiles != nil {
gLog.Printf(LvERROR, "os.OpenFile %s error:%s", os.Args[0], errFiles)
return
binPath, _ := os.Executable() // can not use args[0], on Windows call openp2p is ok(=openp2p.exe)
if targetPath != binPath {
// copy files
errFiles := copyFile(targetPath, binPath)
if errFiles != nil {
gLog.Println(LvERROR, errFiles)
}
}
dst, errFiles := os.OpenFile(targetPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0775)
if errFiles != nil {
gLog.Printf(LvERROR, "os.OpenFile %s error:%s", targetPath, errFiles)
return
}
_, errFiles = io.Copy(dst, src)
if errFiles != nil {
gLog.Printf(LvERROR, "io.Copy error:%s", errFiles)
return
}
src.Close()
dst.Close()
// install system service
gLog.Println(LvINFO, "targetPath:", targetPath)
err = d.Control("install", targetPath, []string{"-d"})
@@ -72,6 +57,23 @@ func install() {
gLog.Println(LvINFO, "Visit WebUI on https://console.openp2p.cn")
}
func copyFile(dst, src string) error {
srcF, err := os.Open(src)
if err != nil {
return fmt.Errorf("os.Open %s error: %w", src, err)
}
defer srcF.Close()
dstF, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0775)
if err != nil {
return fmt.Errorf("os.OpenFile %s error: %w", dst, err)
}
defer dstF.Close()
_, err = io.Copy(dstF, srcF)
if err != nil {
return fmt.Errorf("io.Copy error: %w", err)
}
}
func installByFilename() {
params := strings.Split(filepath.Base(os.Args[0]), "-")
if len(params) < 4 {