From cbb959a31e2c861ec65e36fdd7e4461cde5dd08f Mon Sep 17 00:00:00 2001
From: loveuer <loveuer@live.com>
Date: Sat, 13 Jul 2024 21:56:47 +0800
Subject: [PATCH] chore: add github workflow to build nfctl

---
 .github/workflows/nfctl.yml  | 62 ++++++++++++++++++++++++++++++++++++
 nft/nfctl/main.go            |  7 ++--
 nft/nfctl/version/version.go |  6 ++--
 3 files changed, 71 insertions(+), 4 deletions(-)
 create mode 100644 .github/workflows/nfctl.yml

diff --git a/.github/workflows/nfctl.yml b/.github/workflows/nfctl.yml
new file mode 100644
index 0000000..7f47a13
--- /dev/null
+++ b/.github/workflows/nfctl.yml
@@ -0,0 +1,62 @@
+name: Auto Build
+on:
+  push:
+    branches:
+      - 'master'
+
+env:
+  RELEASE_VERSION: v24.07.13-r1
+
+jobs:
+  build-job:
+    runs-on: ubuntu-latest
+    permissions:
+      id-token: write
+      contents: write
+      pull-requests: write
+      repository-projects: write
+    steps:
+      - name: checkout repository
+        uses: actions/checkout@v4
+
+      - name: install golang
+        uses: actions/setup-go@v4
+        with:
+          go-version: '1.20'
+
+      - name: build linux amd64
+        run: CGO_ENABLE=0 GOOS=linux GOARCH=amd64 go build -ldflags='-s -w' -o "dist/nfctl-linux_amd64-${{ env.RELEASE_VERSION }}" nft/nfctl/main.go
+
+      - name: build linux arm64
+        run: CGO_ENABLE=0 GOOS=linux GOARCH=arm64 go build -ldflags='-s -w' -o "dist/nfctl-linux_arm64-${{ env.RELEASE_VERSION }}" nft/nfctl/main.go
+
+      - name: build windows amd64
+        run: CGO_ENABLE=0 GOOS=windows GOARCH=amd64 go build -ldflags='-s -w' -o "dist/nfctl-win_amd64-${{ env.RELEASE_VERSION }}.exe" nft/nfctl/main.go
+
+      - name: build windows arm64
+        run: CGO_ENABLE=0 GOOS=windows GOARCH=arm64 go build -ldflags='-s -w' -o "dist/nfctl-win_arm64-${{ env.RELEASE_VERSION }}.exe" nft/nfctl/main.go
+
+      - name: build darwin amd64
+        run: CGO_ENABLE=0 GOOS=darwin GOARCH=amd64 go build -ldflags='-s -w' -o "dist/nfctl-darwin_arm64-${{ env.RELEASE_VERSION }}" nft/nfctl/main.go
+
+      - name: build darwin arm64
+        run: CGO_ENABLE=0 GOOS=darwin GOARCH=arm64 go build -ldflags='-s -w' -o "dist/nfctl-darwin_arm64-${{ env.RELEASE_VERSION }}" nft/nfctl/main.go
+
+      - name: show all builds
+        run: ls -lash dist
+
+      - name: create releases
+        id: create_releases
+        uses: "marvinpinto/action-automatic-releases@latest"
+        with:
+          automatic_release_tag: "Release-nfctl-${{ env.RELEASE_VERSION }}"
+          repo_token: "${{ secrets.GITHUB_TOKEN }}"
+          title: "Release_${{ env.RELEASE_VERSION }}"
+          prerelease: false
+          files: |
+            dist/nfctl-linux_amd64-${{ env.RELEASE_VERSION }}
+            dist/nfctl-linux_arm64-${{ env.RELEASE_VERSION }}
+            dist/nfctl-win_amd64-${{ env.RELEASE_VERSION }}.exe
+            dist/nfctl-win_arm64-${{ env.RELEASE_VERSION }}.exe
+            dist/nfctl-darwin_arm64-${{ env.RELEASE_VERSION }}
+            dist/nfctl-darwin_arm64-${{ env.RELEASE_VERSION }}
diff --git a/nft/nfctl/main.go b/nft/nfctl/main.go
index b45f775..200de16 100644
--- a/nft/nfctl/main.go
+++ b/nft/nfctl/main.go
@@ -18,6 +18,9 @@ func main() {
 
 	_ = cmd.Root.ExecuteContext(ctx)
 
-	// 延迟以便 check 检查
-	time.Sleep(1500 * time.Millisecond)
+	select {
+	case <-time.After(3 * time.Second):
+	case <-ctx.Done():
+	case <-version.OkCh:
+	}
 }
diff --git a/nft/nfctl/version/version.go b/nft/nfctl/version/version.go
index 835bcf2..09d7234 100644
--- a/nft/nfctl/version/version.go
+++ b/nft/nfctl/version/version.go
@@ -11,7 +11,7 @@ import (
 	"sync"
 )
 
-const Version = "v24.07.12-r2"
+const Version = "v24.07.13-r1"
 
 var (
 	lk      = &sync.Mutex{}
@@ -25,7 +25,8 @@ var (
 			fmt.Println()
 		}
 	}
-	Fn = empty
+	Fn   = empty
+	OkCh = make(chan struct{}, 1)
 )
 
 func Check() {
@@ -57,6 +58,7 @@ func Check() {
 					lk.Lock()
 					Fn = upgrade(v)
 					lk.Unlock()
+					OkCh <- struct{}{}
 					return
 				}
 			}