package main import ( "fmt" "github.com/tint/env" "hupeh.vip/pm/cmd" "hupeh.vip/pm/db" "os" "os/exec" "strconv" ) //import ( // "flag" // "fmt" // "log" // "os" // "os/exec" // "time" //) // //// 移除不需要的参数 //func strip(slice []string, remove string) []string { // for i := 0; i < len(slice); { // if slice[i] == remove { // if i != len(slice)-1 { // slice = append(slice[:i], slice[i+1:]...) // } else { // slice = slice[:i] // } // } else { // i++ // } // } // return slice //} // //func subProcess(args []string) *exec.Cmd { // cmd := exec.Command(args[0], args[1:]...) // cmd.Stdin = os.Stdin // cmd.Stdout = os.Stdout // cmd.Stderr = os.Stderr // err := cmd.Start() // if err != nil { // fmt.Fprintf(os.Stderr, "[-] Error: %s\n", err) // } // return cmd //} // //func main() { // daemon := flag.Bool("daemon", false, "run in daemon") // forever := flag.Bool("forever", false, "run forever") // flag.Parse() // // // 启用守护模式 // if *daemon { // subProcess(strip(os.Arguments, "-daemon")) // fmt.Printf("[*] Daemon running in pid: %d PPID: %d\n", os.Getpid(), os.Getppid()) // os.Exit(0) // } // // if *forever { // for { // cmd := subProcess(strip(os.Arguments, "-forever")) // fmt.Printf("[*] Forever running in pid: %d PPID: %d\n", os.Getpid(), os.Getppid()) // if err := cmd.Wait(); err != nil { // fmt.Println(err) // os.Exit(1) // } // } // } // // fp, _ := os.OpenFile("./dosomething.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) // log.SetOutput(fp) // log.Printf("[*] Service running in pid: %d PPID: %d\n", os.Getpid(), os.Getppid()) // defer log.Println("exit ...") // for { // log.Printf("DoSomething running in pid: %d PPID: %d\n", os.Getpid(), os.Getppid()) // time.Sleep(time.Second * 5) // } //} func main() { env.Setup() switch env.String("PMT_CURRENT_FUNCTION") { case "start": case "stop": case "shell": case "list": default: runCli() } } func runCli() { cmd.New("pm"). Name("pm"). Description("description"). // start 命令 Command("start", "Start a program in daemon"). Argument("[names:...string]", "file/name/id"). Option("-w, --watch ", "Watches folder changes"). Option("-c, --cwd ", "Sets working directory "+cmd.Yellow("Sets working directory")+" Sets working directory "+cmd.Yellow("Sets working directory")+" Sets working directory "+cmd.Yellow(" Sets working directory")+" Sets working directory "+cmd.Yellow("Sets working directory")+" Sets working directory "+cmd.Yellow("Sets working directory")+" End ..."). Option("-n, --name", "Sets program name"). Option("-e, --env", "Sets current environment name"). Option("-i, --interpreter", "Sets interpreter name"). Action(func(c *cmd.Command, m map[string]any, a ...any) error { if len(a) != 1 { return c.ShowHelp() } var apps []*db.App scripts := a[0].([]string) for _, script := range scripts { loads, err := db.ParseInput(script) if err != nil { return err } apps = append(apps, loads...) } if len(apps) == 0 { fmt.Println("No app found") return nil } for _, app := range apps { if app.Status == db.StatusRunning { fmt.Println(cmd.Yellow(app.Name), cmd.Green("running")) continue } if app.ID == 0 { if err := db.SaveApp(app); err != nil { fmt.Println(cmd.Yellow(app.Name), cmd.Red("failed for save")) return err } } if err := runStart(app.ID); err != nil { fmt.Println(cmd.Yellow(app.Name), cmd.Red("failed")) fmt.Println(" ", err.Error()) } } fmt.Println("over") return nil }). // stop 命令 Command("stop", "Stops a program"). Argument("", "file/name/id"). Action(func(c *cmd.Command, m map[string]any, a ...any) error { fmt.Println("停止程序") fmt.Println(a...) return nil }). // 执行程序 Run() } func runStart(id uint) error { proc := subProcess( []string{os.Args[0], "-id", strconv.Itoa(int(id))}, []string{"PMT_CURRENT_FUNCTION=start"}, ) return proc.Wait() } func subProcess(args []string, env []string) *exec.Cmd { cmd := exec.Command(args[0], args[1:]...) cmd.Env = append(os.Environ(), env...) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr err := cmd.Start() if err != nil { fmt.Fprintf(os.Stderr, "[-] Error: %s\n", err) } return cmd } // 移除不需要的参数 func strip(slice []string, remove string) []string { for i := 0; i < len(slice); { if slice[i] == remove { if i != len(slice)-1 { slice = append(slice[:i], slice[i+1:]...) } else { slice = slice[:i] } } else { i++ } } return slice }