From 940f4c2342325faeab4aa4a1e250a72db0e0cb10 Mon Sep 17 00:00:00 2001 From: jeyrce Date: Fri, 8 Apr 2022 15:26:42 +0800 Subject: [PATCH] =?UTF-8?q?build:=20=E4=B8=BB=E8=A6=81=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=E5=AE=8C=E6=AF=95(#1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + cmd/cmd.go | 53 +++++++++++++++++++++++------------------------------ cmd/conf.go | 7 +++++-- 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index a06b96a..6931fb3 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ _output/ .idea/ .DS_Store skrctl +.skrctl/ diff --git a/cmd/cmd.go b/cmd/cmd.go index ac689df..bfca7d3 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -100,27 +100,6 @@ func mv(src, dst string) error { return os.Rename(src, dst) } -// 初始化项目 .skrctl 配置目录 -func init() { - cwd, err := os.Getwd() - if err != nil { - fmt.Printf("初始化失败: %s\n", err.Error()) - return - } - dir := path.Join(cwd, ".skrctl") - _, err = os.Stat(dir) - if err != nil { - if os.IsNotExist(err) { - err = os.Mkdir(dir, 0755) - if err == nil { - return - } - } - fmt.Printf("初始化失败: %s\n", err.Error()) - return - } -} - func Add(services ...string) { for _, s := range services { if err := C.Add(s); err != nil { @@ -157,6 +136,13 @@ type view struct { } func Status(services ...string) { + // 如果传入空,则展示所有已托管服务状态 + if len(services) < 1 { + list := C.List() + for _, item := range list { + services = append(services, item.Name) + } + } var views = make([]view, 0, len(services)) for _, s := range services { if svc := C.Has(s); svc != nil { @@ -182,7 +168,7 @@ func Start(services ...string) { for _, s := range services { svc := C.Has(s) if svc == nil { - fmt.Printf("启动前需要先加入管理(skrctl add %s)\n", s) + fmt.Printf("操作前需要先加入管理(%s)\n", s) continue } svc.Start() @@ -191,26 +177,33 @@ func Start(services ...string) { func Stop(services ...string) { for _, s := range services { - if svc := C.Has(s); svc != nil { - svc.Stop() + svc := C.Has(s) + if svc == nil { + fmt.Printf("操作前需要先加入管理(%s)\n", s) + continue } + svc.Stop() } } func Enable(services ...string) { for _, s := range services { - if svc := C.Has(s); svc != nil { - svc.SetAutoStart() + svc := C.Has(s) + if svc == nil { + fmt.Printf("操作前需要先加入管理(%s)\n", s) + continue } + svc.SetAutoStart() } - } func Disable(services ...string) { for _, s := range services { - if svc := C.Has(s); svc != nil { - svc.CloseAutoStart() + svc := C.Has(s) + if svc == nil { + fmt.Printf("操作前需要先加入管理(%s)\n", s) + continue } + svc.CloseAutoStart() } - } diff --git a/cmd/conf.go b/cmd/conf.go index 1483079..a4927ea 100644 --- a/cmd/conf.go +++ b/cmd/conf.go @@ -28,8 +28,11 @@ func newConf() *conf { stat, err := os.Stat(dir) if err != nil { if os.IsNotExist(err) { - fmt.Println("未执行'skrctl init'") - os.Exit(1) + if err := os.Mkdir(dir, 0755); err != nil { + fmt.Println("初始化配置失败:", err.Error()) + os.Exit(1) + } + return &c } fmt.Printf("加载本地配置失败: %s\n", err.Error()) os.Exit(1)