11package main
22
33import (
4- "io/ioutil"
4+ "archive/zip"
5+ "io"
6+ "io/fs"
7+ "os"
8+ "path"
59
610 "github.com/scriptscat/cloudcat/pkg/scriptcat"
711 "github.com/scriptscat/cloudcat/pkg/utils"
@@ -10,6 +14,7 @@ import (
1014
1115type execCmd struct {
1216 cookiefile string
17+ runOnce bool
1318}
1419
1520func newExecCmd () * execCmd {
@@ -21,32 +26,72 @@ func (e *execCmd) Commands() []*cobra.Command {
2126 Use : "exec [file] [flags]" ,
2227 Short : "执行一个脚本猫脚本" ,
2328 RunE : e .exec ,
29+ Args : cobra .ExactArgs (1 ),
2430 }
2531 ret .Flags ().StringVarP (& e .cookiefile , "cookiefile" , "c" , "" , "设置cookie文件" )
32+ ret .Flags ().BoolVarP (& e .runOnce , "run-once" , "" , false , "运行一次(如果是定时脚本的话,不会进入定时逻辑)" )
2633
2734 return []* cobra.Command {ret }
2835}
2936
3037func (e * execCmd ) exec (cmd * cobra.Command , args []string ) error {
3138
32- sc , err := scriptcat .NewScriptCat ()
33- if err != nil {
34- return err
35- }
36-
37- script , err := ioutil .ReadFile (args [0 ])
38- if err != nil {
39- return err
39+ var err error
40+ var script , cookie , value fs.File
41+ if path .Ext (args [0 ]) == ".zip" {
42+ // 软件包
43+ pkg , err := zip .OpenReader (args [0 ])
44+ if err != nil {
45+ return err
46+ }
47+ script , err = pkg .Open ("userScript.js" )
48+ if err != nil {
49+ return err
50+ }
51+ defer script .Close ()
52+ cookie , _ = pkg .Open ("cookie.json" )
53+ value , _ = pkg .Open ("value.json" )
54+ } else {
55+ script , err = os .Open (args [0 ])
56+ if err != nil {
57+ return err
58+ }
59+ defer script .Close ()
60+ cookie , err = os .Open (e .cookiefile )
61+ if err != nil {
62+ return err
63+ }
64+ defer cookie .Close ()
4065 }
4166
4267 opts := make ([]scriptcat.Option , 0 )
43- if e . cookiefile != "" {
44- jar , err := utils .ReadCookie (e . cookiefile )
68+ if cookie != nil {
69+ jar , err := utils .ReadCookie (readString ( cookie ) )
4570 if err != nil {
4671 return err
4772 }
4873 opts = append (opts , scriptcat .WithCookie (jar ))
4974 }
5075
51- return sc .Run (string (script ))
76+ if value != nil {
77+ opts = append (opts , scriptcat .WithValue (value ))
78+ }
79+
80+ sc , err := scriptcat .NewScriptCat ()
81+ if err != nil {
82+ return err
83+ }
84+
85+ if e .runOnce {
86+ return sc .RunOnce (readString (script ), opts ... )
87+ }
88+ return sc .Run (readString (script ), opts ... )
89+ }
90+
91+ func readString (r io.Reader ) string {
92+ if r == nil {
93+ return ""
94+ }
95+ byte , _ := io .ReadAll (r )
96+ return string (byte )
5297}
0 commit comments