Skip to content

Commit

Permalink
main:add cli
Browse files Browse the repository at this point in the history
  • Loading branch information
eatmeatball committed Sep 21, 2023
1 parent e622468 commit 1b8bf48
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# cock
# cock 程序唤起/任务调度

一个兼备程序唤起和任务调度程序
53 changes: 53 additions & 0 deletions cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
_ "embed"
"fmt"
"github.com/leancodebox/cock/jobmanager"
"github.com/leancodebox/cock/resource"
"log/slog"
"os"
"os/signal"
)

func init() {
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil)))
}

func main() {
if _, err := os.Stat("jobConfig.json"); os.IsNotExist(err) {
fmt.Println("当前目录下不存在jobConfig.json文件")

// 询问是否生成该文件
fmt.Print("是否生成jobConfig.json文件?(yes/no): ")
var answer string
_, err := fmt.Scanln(&answer)
if err != nil {
fmt.Println("无法读取输入,错误:", err)
return
}

if answer == "yes" {
err = os.WriteFile("jobConfig.json", resource.GetJobConfigDefault(), 0644)
if err != nil {
fmt.Println("无法写入文件,错误:", err)
return
}
fmt.Println("jobConfig.json文件已生成并写入内容。请调整配置后再次启动")
} else {
fmt.Println("请补充 jobConfig.json 后再次启动程序")
}
return
}
fileData, err := os.ReadFile("jobConfig.json")
if err != nil {
slog.Error(err.Error())
return
}

jobmanager.Reg(fileData)
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
slog.Info("bye~~👋👋")
}
6 changes: 2 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import (
"fmt"
"github.com/leancodebox/cock/jobmanager"
"github.com/leancodebox/cock/jobmanagerserver"
"github.com/leancodebox/cock/resource"
"log/slog"
"os"
"os/signal"
)

//go:embed jobConfig.example.json
var jobConfigDefault []byte

func init() {
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil)))
}
Expand All @@ -31,7 +29,7 @@ func main() {
}

if answer == "yes" {
err = os.WriteFile("jobConfig.json", jobConfigDefault, 0644)
err = os.WriteFile("jobConfig.json", resource.GetJobConfigDefault(), 0644)
if err != nil {
fmt.Println("无法写入文件,错误:", err)
return
Expand Down
File renamed without changes.
12 changes: 12 additions & 0 deletions resource/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package resource

import (
_ "embed"
)

//go:embed jobConfig.example.json
var jobConfigDefault []byte

func GetJobConfigDefault() []byte {
return jobConfigDefault
}

0 comments on commit 1b8bf48

Please sign in to comment.