Browse Source

新增 Chrome Checker

Signed-off-by: allan716 <[email protected]>
allan716 3 years ago
parent
commit
a66b3c9b38

+ 32 - 0
.github/workflows/build_chrome_checker.yml

@@ -0,0 +1,32 @@
+name: release
+
+on:
+  push:
+    tags:
+      - v*.*-ChromeChecker*
+  workflow_dispatch:
+
+jobs:
+  goreleaser:
+    runs-on: ubuntu-latest
+    name: build chrome_checker
+    steps:
+      - name: Checkout
+        uses: actions/checkout@v2
+        with:
+          fetch-depth: 0
+
+      - name: Set up Go
+        uses: actions/setup-go@v2
+        with:
+          go-version: 1.17
+
+      - name: Run GoReleaser
+        uses: goreleaser/goreleaser-action@v2
+        with:
+          # either 'goreleaser' (default) or 'goreleaser-pro'
+          distribution: goreleaser
+          version: latest
+          args: release -f .goreleaser-chrome_checker.yml --rm-dist
+        env:
+          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN  }}

+ 86 - 0
.goreleaser-chrome_checker.yml

@@ -0,0 +1,86 @@
+project_name: chinesesubfinder
+
+env:
+  - GO111MODULE=on
+
+before:
+  hooks:
+    - go mod tidy
+    - sudo apt-get update -y
+    - sudo apt-get install gcc-multilib
+    - sudo apt-get install -y *-w64-x86-*
+    - sudo apt-get install gcc-arm-linux-gnueabihf gcc-aarch64-linux-gnu gcc-arm-linux-gnueabi g++-arm-linux-gnueabihf g++-aarch64-linux-gnu g++-arm-linux-gnueabi
+    # https://github.com/goreleaser/goreleaser-cross 交叉编译的文档
+builds:
+  - id: linux-amd64-build
+    env:
+      - CGO_ENABLED=1
+    goos:
+      - linux
+    goarch:
+      - amd64
+    main: ./cmd/chrome_checker/main.go
+    ldflags:
+      - -s -w --extldflags "-static -fpic"
+
+  - id: linux-armv7-build
+    env:
+      - CGO_ENABLED=1
+      - CC=arm-linux-gnueabihf-gcc
+      - CXX=arm-linux-gnueabihf-g++
+    goos:
+      - linux
+    goarch:
+      - arm
+    goarm:
+      - 7
+    main: ./cmd/chrome_checker/main.go
+    ldflags:
+      - -s -w --extldflags "-static -fpic"
+
+  - id: linux-arm64-build
+    env:
+      - CGO_ENABLED=1
+      - CC=aarch64-linux-gnu-gcc
+      - CXX=aarch64-linux-gnu-g++
+    goos:
+      - linux
+    goarch:
+      - arm64
+    main: ./cmd/chrome_checker/main.go
+    ldflags:
+      - -s -w --extldflags "-static -fpic"
+
+  - id: windows-build
+    env:
+      - CGO_ENABLED=1
+      - CXX=x86_64-w64-mingw32-g++
+      - CC=x86_64-w64-mingw32-gcc
+    goos:
+      - windows
+    goarch:
+      - amd64
+    main: ./cmd/chrome_checker/main.go
+    ldflags:
+      - -s -w --extldflags "-static -fpic"
+
+archives:
+  - id: archive
+    name_template: "{{ .ProjectName }}-{{ .Version }}-{{ .Os }}-{{ .Arch }}"
+    replacements:
+      darwin: Darwin
+      linux: Linux
+      windows: Windows
+      amd64: x86_64
+checksum:
+  name_template: "checksums.txt"
+snapshot:
+  name_template: "{{ .Tag }}-next"
+changelog:
+  sort: asc
+  filters:
+    exclude:
+      - "^docs:"
+      - "^test:"
+      - "^TestData:"
+      - "^TestCode:"

+ 72 - 0
cmd/chrome_checker/main.go

@@ -0,0 +1,72 @@
+package main
+
+import (
+	"fmt"
+	"os"
+
+	"github.com/go-rod/rod"
+	"github.com/go-rod/rod/lib/launcher"
+	"github.com/urfave/cli/v2"
+)
+
+/*
+	使用的命令:
+		chrome_checker -cbp ${chromeBinFPath}
+
+		${chromeBinFPath} -> Chrome 的二进制文件路径
+
+	程序返回值:
+		exit(0) success
+		exit(1) error
+*/
+
+func main() {
+
+	if recover() != nil {
+		fmt.Println("Check Chrome has panic")
+		os.Exit(1)
+	}
+
+	var chromeBinFPath string
+
+	app := &cli.App{
+		Name:  "Chrome Checker",
+		Usage: "Check Chrome has installed, if not will not auto download and install it",
+		Flags: []cli.Flag{
+			&cli.StringFlag{
+				Name:        "chromeBinFPath",
+				Aliases:     []string{"cbp"},
+				Usage:       "Bin of the browser binary path to launch",
+				Destination: &chromeBinFPath,
+				Required:    true,
+			},
+		},
+		Action: func(c *cli.Context) error {
+			err := rod.Try(func() {
+				purl := launcher.New().Bin(chromeBinFPath).MustLaunch()
+				browser := rod.New().ControlURL(purl).MustConnect()
+				page := browser.MustPage("https://www.baidu.com").MustWaitLoad()
+				defer func() {
+					_ = page.Close()
+				}()
+			})
+			if err != nil {
+				fmt.Println("Check Chrome has error", err.Error())
+				os.Exit(1)
+			}
+
+			fmt.Println("Check Chrome is ok")
+			os.Exit(0)
+
+			return nil
+		},
+	}
+
+	err := app.Run(os.Args)
+	if err != nil {
+		fmt.Println("Check Chrome has error", err.Error())
+		os.Exit(1)
+	}
+
+	os.Exit(0)
+}