qinglong-go/service/dependencies/dependencies.go

126 lines
4.2 KiB
Go
Raw Normal View History

2022-11-20 14:11:47 +00:00
package dependencies
import (
"bytes"
"context"
"fmt"
"github.com/huoxue1/qinglong-go/models"
2023-02-03 04:43:37 +00:00
"github.com/huoxue1/qinglong-go/service/config"
2022-11-20 14:11:47 +00:00
"github.com/huoxue1/qinglong-go/utils"
"io"
"os"
2022-11-20 14:11:47 +00:00
"strings"
"time"
)
func AddDep(dep *models.Dependences) {
if dep.Type == models.NODE {
addNodeDep(dep)
2023-01-10 12:43:28 +00:00
} else if dep.Type == models.PYTHON {
addPythonDep(dep)
2023-01-12 03:21:57 +00:00
} else {
addLinuxDep(dep)
2022-11-20 14:11:47 +00:00
}
}
func DelDep(ids []int) {
for _, id := range ids {
dep, err := models.GetDependences(id)
if err != nil {
continue
}
if dep.Type == models.NODE {
unInstallDep("pnpm remove "+dep.Name, dep)
} else if dep.Type == models.PYTHON {
pip := config.GetKey("PipCmd", "pip")
unInstallDep(fmt.Sprintf("%s uninstall %s", pip, dep.Name), dep)
} else {
unInstallDep("apk uninstall "+dep.Name, dep)
}
}
}
func unInstallDep(command string, dep *models.Dependences) {
ctx := context.WithValue(context.Background(), "cancel", make(chan int, 1))
now := time.Now()
go utils.RunTask(ctx, command, map[string]string{}, func(ctx context.Context) {
writer := ctx.Value("log").(io.Writer)
_, _ = writer.Write([]byte(fmt.Sprintf("##开始执行.. %s\n\n", now.Format("2006-01-02 15:04:05"))))
}, func(ctx context.Context) {
writer := ctx.Value("log").(io.Writer)
_, _ = writer.Write([]byte(fmt.Sprintf("\n##执行结束.. %s耗时%.1f秒\n\n", time.Now().Format("2006-01-02 15:04:05"), time.Now().Sub(now).Seconds())))
_ = models.DeleteDependences(dep.Id)
}, os.Stdout)
}
2022-11-20 14:11:47 +00:00
func addNodeDep(dep *models.Dependences) {
log := ""
buffer := bytes.NewBufferString(log)
ctx := context.WithValue(context.Background(), "cancel", make(chan int, 1))
now := time.Now()
go utils.RunTask(ctx, fmt.Sprintf("pnpm add %s", dep.Name), map[string]string{}, func(ctx context.Context) {
dep.Status = 0
_, _ = models.AddDependences(dep)
2022-11-20 14:11:47 +00:00
writer := ctx.Value("log").(io.Writer)
writer.Write([]byte(fmt.Sprintf("##开始执行.. %s\n\n", now.Format("2006-01-02 15:04:05"))))
}, func(ctx context.Context) {
writer := ctx.Value("log").(io.Writer)
writer.Write([]byte(fmt.Sprintf("\n##执行结束.. %s耗时%.1f秒\n\n", time.Now().Format("2006-01-02 15:04:05"), time.Now().Sub(now).Seconds())))
dep.Status = 1
var logs []string
for _, i2 := range strings.Split(buffer.String(), "\n") {
logs = append(logs, i2+"\n\n")
}
dep.Log = logs
_ = models.UpdateDependences(dep)
2022-11-20 14:11:47 +00:00
}, buffer)
}
2023-01-10 12:43:28 +00:00
func addPythonDep(dep *models.Dependences) {
log := ""
buffer := bytes.NewBufferString(log)
ctx := context.WithValue(context.Background(), "cancel", make(chan int, 1))
now := time.Now()
2023-02-03 04:43:37 +00:00
pip := config.GetKey("PipCmd", "pip")
go utils.RunTask(ctx, fmt.Sprintf(pip+" install %s", dep.Name), map[string]string{}, func(ctx context.Context) {
dep.Status = 0
_, _ = models.AddDependences(dep)
2023-01-10 12:43:28 +00:00
writer := ctx.Value("log").(io.Writer)
writer.Write([]byte(fmt.Sprintf("##开始执行.. %s\n\n", now.Format("2006-01-02 15:04:05"))))
}, func(ctx context.Context) {
writer := ctx.Value("log").(io.Writer)
writer.Write([]byte(fmt.Sprintf("\n##执行结束.. %s耗时%.1f秒\n\n", time.Now().Format("2006-01-02 15:04:05"), time.Now().Sub(now).Seconds())))
dep.Status = 1
var logs []string
for _, i2 := range strings.Split(buffer.String(), "\n") {
logs = append(logs, i2+"\n\n")
}
dep.Log = logs
models.UpdateDependences(dep)
2023-01-10 12:43:28 +00:00
}, buffer)
}
2023-01-12 03:21:57 +00:00
func addLinuxDep(dep *models.Dependences) {
log := ""
buffer := bytes.NewBufferString(log)
ctx := context.WithValue(context.Background(), "cancel", make(chan int, 1))
now := time.Now()
go utils.RunTask(ctx, fmt.Sprintf("apk add %s", dep.Name), map[string]string{}, func(ctx context.Context) {
dep.Status = 0
_, _ = models.AddDependences(dep)
2023-01-12 03:21:57 +00:00
writer := ctx.Value("log").(io.Writer)
writer.Write([]byte(fmt.Sprintf("##开始执行.. %s\n\n", now.Format("2006-01-02 15:04:05"))))
}, func(ctx context.Context) {
writer := ctx.Value("log").(io.Writer)
writer.Write([]byte(fmt.Sprintf("\n##执行结束.. %s耗时%.1f秒\n\n", time.Now().Format("2006-01-02 15:04:05"), time.Now().Sub(now).Seconds())))
dep.Status = 1
var logs []string
for _, i2 := range strings.Split(buffer.String(), "\n") {
logs = append(logs, i2+"\n\n")
}
dep.Log = logs
models.UpdateDependences(dep)
2023-01-12 03:21:57 +00:00
}, buffer)
}