@@ -2,13 +2,16 @@ package main
22
33import (
44 "archive/zip"
5+ "context"
56 "io"
67 "io/fs"
78 "os"
9+ "os/signal"
810 "path"
911
1012 "github.com/scriptscat/cloudcat/pkg/scriptcat"
1113 "github.com/scriptscat/cloudcat/pkg/utils"
14+ "github.com/sirupsen/logrus"
1215 "github.com/spf13/cobra"
1316)
1417
@@ -29,16 +32,15 @@ func (e *execCmd) Commands() []*cobra.Command {
2932 Args : cobra .ExactArgs (1 ),
3033 }
3134 ret .Flags ().StringVarP (& e .cookiefile , "cookiefile" , "c" , "" , "设置cookie文件" )
32- ret .Flags ().BoolVarP (& e .runOnce , "run-once" , "" , false , "运行一次(如果是定时脚本的话,不会进入定时逻辑) " )
35+ ret .Flags ().BoolVarP (& e .runOnce , "run-once" , "" , false , "让定时脚本只运行一次 " )
3336
3437 return []* cobra.Command {ret }
3538}
3639
3740func (e * execCmd ) exec (cmd * cobra.Command , args []string ) error {
38-
3941 var err error
4042 var script , cookie , value fs.File
41- if path .Ext (args [0 ]) == ".zip " {
43+ if path .Ext (args [0 ]) != ".js " {
4244 // 软件包
4345 pkg , err := zip .OpenReader (args [0 ])
4446 if err != nil {
@@ -64,7 +66,7 @@ func (e *execCmd) exec(cmd *cobra.Command, args []string) error {
6466 defer cookie .Close ()
6567 }
6668
67- opts := make ( []scriptcat.Option , 0 )
69+ opts := []scriptcat.Option { scriptcat . WithLogger ( logrus . StandardLogger (). Logf )}
6870 if cookie != nil {
6971 jar , err := utils .ReadCookie (readString (cookie ))
7072 if err != nil {
@@ -83,9 +85,13 @@ func (e *execCmd) exec(cmd *cobra.Command, args []string) error {
8385 }
8486
8587 if e .runOnce {
86- return sc .RunOnce (readString (script ), opts ... )
88+ _ , err = sc .RunOnce (context .Background (), readString (script ), opts ... )
89+ } else {
90+ ctx , cancel := signal .NotifyContext (context .Background (), os .Interrupt )
91+ defer cancel ()
92+ _ , err = sc .Run (ctx , readString (script ), opts ... )
8793 }
88- return sc . Run ( readString ( script ), opts ... )
94+ return err
8995}
9096
9197func readString (r io.Reader ) string {
0 commit comments