Skip to content

Commit ae604a3

Browse files
committed
feat: implement new configuration provider and enhance response body handling
1 parent 6e6f01b commit ae604a3

File tree

11 files changed

+73
-22
lines changed

11 files changed

+73
-22
lines changed

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
BIN_NAME=github-backup
22
BUILD_DIR=./build
3+
MODULE := $(shell go list -m)
34
BUILD=$(shell git rev-parse --short HEAD)@$(shell date +%s)
45
CURRENT_OS := $(shell uname -s | tr '[:upper:]' '[:lower:]')
56
CURRENT_ARCH := $(shell uname -m | tr '[:upper:]' '[:lower:]')
@@ -56,3 +57,14 @@ compressAll: buildAll
5657
.PHONY: buildImage
5758
buildImage:
5859
docker buildx build --platform=linux/amd64,linux/arm64 -t ghcr.io/tbxark/github-backup:latest . --push --provenance=false
60+
61+
.PHONY: lint
62+
lint:
63+
go fmt ./...
64+
go vet ./...
65+
go get ./...
66+
go test ./...
67+
go mod tidy
68+
golangci-lint fmt --no-config --enable gofmt,goimports
69+
golangci-lint run --no-config --fix
70+
nilaway -include-pkgs="$(MODULE)" ./...

config/config.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package config
22

33
import (
4+
"context"
45
"encoding/json"
6+
"errors"
7+
8+
"github.com/go-sphere/confstore"
9+
"github.com/go-sphere/confstore/codec"
10+
"github.com/go-sphere/confstore/provider"
11+
"github.com/go-sphere/confstore/provider/file"
12+
"github.com/go-sphere/confstore/provider/http"
513
)
614

715
type BackupProviderConfigType string
@@ -111,3 +119,25 @@ func ToRaw[T any](conf T) json.RawMessage {
111119
}
112120
return raw
113121
}
122+
123+
func newConfProvider(path string) (provider.Provider, error) {
124+
if http.IsRemoteURL(path) {
125+
return http.New(path, http.WithTimeout(10)), nil
126+
}
127+
if file.IsLocalPath(path) {
128+
return file.New(path, file.WithExpandEnv()), nil
129+
}
130+
return nil, errors.New("unsupported config path")
131+
}
132+
133+
func NewConfig(path string) (*SyncConfig, error) {
134+
pro, err := newConfProvider(path)
135+
if err != nil {
136+
return nil, err
137+
}
138+
config, err := confstore.Load[SyncConfig](context.Background(), pro, codec.JsonCodec())
139+
if err != nil {
140+
return nil, err
141+
}
142+
return config, nil
143+
}

config/config_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package config
33
import (
44
"encoding/json"
55
"fmt"
6-
"github.com/TBXark/github-backup/provider/gitea"
76
"testing"
7+
8+
"github.com/TBXark/github-backup/provider/gitea"
89
)
910

1011
func TestSyncConfig(t *testing.T) {

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module github.com/TBXark/github-backup
22

3-
go 1.23
3+
go 1.23.0
44

55
toolchain go1.23.5
66

77
require (
8+
github.com/go-sphere/confstore v0.0.2
89
github.com/robfig/cron/v3 v3.0.1
9-
github.com/TBXark/confstore v0.0.0-20250123065822-f224518502e3
1010
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
github.com/TBXark/confstore v0.0.0-20250123065822-f224518502e3 h1:ZjqHMo5/qClZzMRj5F39tJ2lFcar1sXNv0T9HzsiAME=
2-
github.com/TBXark/confstore v0.0.0-20250123065822-f224518502e3/go.mod h1:TOxM19Snt9wT02PJzyz66sgq7sWhr4AFzPEZIKvVnTE=
1+
github.com/go-sphere/confstore v0.0.2 h1:9nuPS86wlv5Rpi+fnnFNq9tDVxgfAOK6uOPiASsgubo=
2+
github.com/go-sphere/confstore v0.0.2/go.mod h1:rvp2oSOW4x3E8JU0efD9JtHpBM2M3VIqM4rohoSMr34=
33
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
44
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=

main.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,38 @@ package main
33
import (
44
"flag"
55
"fmt"
6-
"github.com/TBXark/confstore"
6+
"log"
7+
78
"github.com/TBXark/github-backup/config"
89
"github.com/robfig/cron/v3"
9-
"log"
1010
)
1111

1212
var (
1313
BuildVersion = "dev"
1414
)
1515

1616
func main() {
17-
c := flag.String("config", "config.json", "config file")
18-
v := flag.Bool("version", false, "show version")
19-
h := flag.Bool("help", false, "show help")
17+
conf := flag.String("config", "config.json", "config file")
18+
version := flag.Bool("version", false, "show version")
19+
help := flag.Bool("help", false, "show help")
2020
flag.Parse()
21-
if *v {
21+
if *version {
2222
fmt.Println(BuildVersion)
2323
return
2424
}
25-
if *h {
25+
if *help {
2626
flag.Usage()
2727
return
2828
}
29-
conf, err := confstore.Load[config.SyncConfig](*c)
29+
data, err := config.NewConfig(*conf)
3030
if err != nil {
3131
log.Fatalf("load config error: %s", err.Error())
3232
}
3333

34-
syncTask := NewTask(conf)
35-
if conf.Cron != "" {
34+
syncTask := NewTask(data)
35+
if data.Cron != "" {
3636
task := cron.New()
37-
_, e := task.AddJob(conf.Cron, syncTask)
37+
_, e := task.AddJob(data.Cron, syncTask)
3838
if e != nil {
3939
log.Fatalf("add cron task error: %s", e.Error())
4040
}

provider/gitea/gitea.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package gitea
22

33
import (
44
"fmt"
5+
"strings"
6+
57
"github.com/TBXark/github-backup/provider/provider"
68
"github.com/TBXark/github-backup/utils/request"
7-
"strings"
89
)
910

1011
type Config struct {

provider/github/github.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package github
22

33
import (
44
"fmt"
5-
"github.com/TBXark/github-backup/utils/request"
65
"strings"
6+
7+
"github.com/TBXark/github-backup/utils/request"
78
)
89

910
type Repo struct {

provider/local/local.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package local
22

33
import (
44
"fmt"
5-
"github.com/TBXark/github-backup/provider/provider"
65
"log"
76
"os"
87
"os/exec"
98
"path/filepath"
9+
10+
"github.com/TBXark/github-backup/provider/provider"
1011
)
1112

1213
type UpdateAction string

task.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package main
22

33
import (
44
"fmt"
5+
"log"
6+
57
"github.com/TBXark/github-backup/config"
68
"github.com/TBXark/github-backup/provider/gitea"
79
"github.com/TBXark/github-backup/provider/github"
810
"github.com/TBXark/github-backup/provider/local"
911
"github.com/TBXark/github-backup/provider/provider"
1012
"github.com/TBXark/github-backup/utils/matcher"
11-
"log"
1213
)
1314

1415
func BuildBackupProvider(conf *config.BackupProviderConfig) (provider.Provider, error) {

0 commit comments

Comments
 (0)