Skip to content

Commit 2359b86

Browse files
committed
init
0 parents  commit 2359b86

File tree

4 files changed

+197
-0
lines changed

4 files changed

+197
-0
lines changed

.github/workflows/release.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: release
2+
3+
on:
4+
push:
5+
tags:
6+
- "v*"
7+
8+
permissions:
9+
contents: write
10+
11+
jobs:
12+
build-linux:
13+
runs-on: ubuntu-24.04
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v4
17+
- uses: actions/setup-go@v5
18+
with:
19+
go-version: "^1.23.0"
20+
- id: get-version
21+
run: |
22+
echo "version=$(echo ${{github.ref_name}} | cut -c2-)" >> $GITHUB_OUTPUT
23+
- name: Build tool
24+
id: build
25+
run: |
26+
GOOS=linux GOARCH=amd64 go build -o github-release-sync
27+
tar -zcvf "github-release-sync-${{ steps.get-version.outputs.version }}-linux-amd64.tar.gz" github-release-sync
28+
GOOS=linux GOARCH=386 go build -o github-release-sync
29+
tar -zcvf "github-release-sync-${{ steps.get-version.outputs.version }}-linux-i386.tar.gz" github-release-sync
30+
GOOS=linux GOARCH=arm64 go build -o github-release-sync
31+
tar -zcvf "github-release-sync-${{ steps.get-version.outputs.version }}-linux-arm64.tar.gz" github-release-sync
32+
GOOS=linux GOARCH=loong64 go build -o github-release-sync
33+
tar -zcvf "github-release-sync-${{ steps.get-version.outputs.version }}-linux-loong64.tar.gz" github-release-sync
34+
- name: Release
35+
uses: softprops/action-gh-release@v2
36+
with:
37+
prerelease: true
38+
tag_name: ${{github.ref_name}}
39+
files: |
40+
github-release-sync-*.tar.gz

cli.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"flag"
6+
"fmt"
7+
"io"
8+
"net/http"
9+
"os"
10+
"path"
11+
"strings"
12+
13+
"github.com/hashicorp/go-version"
14+
15+
"github.com/google/go-github/github"
16+
)
17+
18+
var (
19+
basePath string
20+
)
21+
22+
func fileExists(filename string) bool {
23+
info, err := os.Stat(filename)
24+
if os.IsNotExist(err) {
25+
return false
26+
}
27+
return !info.IsDir()
28+
}
29+
30+
func init() {
31+
flag.StringVar(&basePath, "base", "/var/www/pojt-get", "-base /var/www/pojt-get")
32+
}
33+
34+
func main() {
35+
flag.Parse()
36+
37+
baseVersion := "v0.0.0-test.0"
38+
lastVersion, err := version.NewVersion(baseVersion)
39+
if err != nil {
40+
panic(err)
41+
}
42+
43+
client := github.NewClient(nil)
44+
opt := &github.ListOptions{}
45+
46+
releases, _, err := client.Repositories.ListReleases(context.Background(), "Pigeon-Developer", "pigeon-oj-tool", opt)
47+
48+
if err != nil {
49+
panic(err)
50+
}
51+
52+
downloadDir := path.Join(basePath, "download")
53+
54+
for _, release := range releases {
55+
releasePath := path.Join(downloadDir, *release.TagName)
56+
os.MkdirAll(releasePath, os.ModePerm)
57+
58+
fmt.Println("sync release ", *release.TagName)
59+
60+
// isStable := !*release.Prerelease && !*release.Draft
61+
isStable := true
62+
if isStable {
63+
v, err := version.NewVersion(*release.TagName)
64+
if err != nil {
65+
continue
66+
}
67+
68+
if v.GreaterThan(lastVersion) {
69+
lastVersion = v
70+
}
71+
}
72+
73+
for _, asset := range release.Assets {
74+
filePath := path.Join(releasePath, *asset.Name)
75+
if fileExists(filePath) {
76+
fmt.Println("sync file ", filePath, " already exists")
77+
continue
78+
}
79+
if !strings.HasPrefix(*asset.Name, "pojt-") {
80+
continue
81+
}
82+
83+
fmt.Println("sync file ", *asset.Name)
84+
85+
rc, url, err := client.Repositories.DownloadReleaseAsset(context.Background(), "Pigeon-Developer", "pigeon-oj-tool", *asset.ID)
86+
if err != nil {
87+
fmt.Println("sync file ", filePath, " error: ", err)
88+
continue
89+
}
90+
91+
f, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, os.ModePerm)
92+
if err != nil {
93+
fmt.Println("sync file ", filePath, " error: ", err)
94+
continue
95+
}
96+
defer f.Close()
97+
98+
if rc != nil {
99+
defer rc.Close()
100+
_, err := io.Copy(f, rc)
101+
if err != nil {
102+
fmt.Println("sync file ", filePath, " error: ", err)
103+
continue
104+
}
105+
} else {
106+
resp, err := http.Get(url)
107+
if err != nil {
108+
fmt.Println("sync file ", filePath, " error: ", err)
109+
continue
110+
}
111+
defer resp.Body.Close()
112+
113+
_, err = io.Copy(f, resp.Body)
114+
115+
if err != nil {
116+
fmt.Println("sync file ", filePath, " error: ", err)
117+
continue
118+
}
119+
}
120+
}
121+
}
122+
123+
if lastVersion.Original() != baseVersion {
124+
versionPath := path.Join(basePath, "latest-version")
125+
versionFile, err := os.OpenFile(versionPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
126+
if err != nil {
127+
panic(err)
128+
}
129+
130+
defer versionFile.Close()
131+
132+
versionFile.WriteString(lastVersion.String())
133+
}
134+
}

go.mod

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module github.com/Pigeon-Developer/github-release-sync
2+
3+
go 1.23.5
4+
5+
require (
6+
github.com/google/go-github v17.0.0+incompatible
7+
github.com/hashicorp/go-version v1.7.0
8+
)
9+
10+
require (
11+
github.com/google/go-cmp v0.6.0 // indirect
12+
github.com/google/go-querystring v1.1.0 // indirect
13+
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
2+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
3+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
4+
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
5+
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
6+
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
7+
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
8+
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
9+
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
10+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 commit comments

Comments
 (0)