Skip to content

Commit 2aa21eb

Browse files
committed
完善crontab脚本&docker镜像
1 parent ae35c32 commit 2aa21eb

14 files changed

Lines changed: 222 additions & 45 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ config.yaml
77
*.zip
88
*.exe
99

10+
scriptcat
11+
cloudcat

Dockerfile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
FROM golang:1.16 as build
2+
3+
ARG GOOS=linux
4+
ARG GOARCH=amd64
5+
ARG GOPROXY="https://goproxy.cn,direct"
6+
7+
WORKDIR /cloudcat
8+
9+
COPY . .
10+
11+
# TODO: 交叉编译
12+
13+
RUN CGO_LDFLAGS="-static" go build -o scriptcat ./cmd/scriptcat && \
14+
go build -o cloudcat ./cmd/cloudcat
15+
16+
ARG ARCH
17+
18+
FROM ${ARCH}busybox:1.33.1-musl
19+
20+
WORKDIR /cloudcat
21+
22+
COPY --from=build /cloudcat/scriptcat .
23+
24+
COPY --from=build /cloudcat/cloudcat .
25+
26+
RUN ls -l && chmod +x scriptcat cloudcat
27+
28+
ENTRYPOINT ["./scriptcat"]

Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
VERSION=0.1.0
2+
DOCKER_REPO=codfrm
3+
REMOTE_REPO=$(DOCKER_REPO)/cloudcat:$(VERSION)
4+
5+
build: scriptcat cloudcat
6+
7+
scriptcat:
8+
go build .\cmd\scriptcat
9+
10+
cloudcat:
11+
go build .\cmd\cloudcat
12+
13+
docker:
14+
docker build -t cloudcat .
15+
16+
docker-test:
17+
docker run -it -v $(PWD)/bilibili.zip:/cloudcat/bilibili.zip $(REMOTE_REPO) exec bilibili.zip
18+
19+
docker-push: docker
20+
docker tag cloudcat $(REMOTE_REPO)
21+
docker push $(REMOTE_REPO)
File renamed without changes.

cmd/scriptcat/exec.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ package main
22

33
import (
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

3740
func (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

9197
func readString(r io.Reader) string {

docker-compose.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/scriptscat/cloudcat
33
go 1.15
44

55
require (
6+
github.com/robfig/cron/v3 v3.0.1
67
github.com/sirupsen/logrus v1.8.1
78
github.com/spf13/cobra v1.2.1
89
github.com/stretchr/testify v1.7.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
196196
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
197197
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
198198
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
199+
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
200+
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
199201
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
200202
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
201203
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=

pkg/executor/executor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ func NewExecutor() (*Executor, error) {
1515
}
1616

1717
func (c *Executor) Run(ctx *Context, source string) (*v8go.Value, error) {
18-
return ctx.RunScript(source, "main.js")
18+
return ctx.RunScript(source, "app.js")
1919
}

pkg/executor/gmxhr_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestGmXmlHttpRequest(t *testing.T) {
1616
ctx, _ := NewContext(iso, WithLogger(logrus.StandardLogger().Logf), GmNotification(), GmXmlHttpRequest(nil), Console())
1717

1818
ret, err := iso.Run(ctx, `
19-
function main() {
19+
function app() {
2020
return new Promise(resolve => {
2121
GM_xmlhttpRequest({
2222
method: 'GET',
@@ -46,7 +46,7 @@ function main() {
4646
})
4747
})
4848
}
49-
main();
49+
app();
5050
`)
5151
assert.Nil(t, err)
5252
p, err := ret.AsPromise()

0 commit comments

Comments
 (0)