diff --git a/.github/workflows/master.yaml b/.github/workflows/master.yaml new file mode 100644 index 0000000..00a7716 --- /dev/null +++ b/.github/workflows/master.yaml @@ -0,0 +1,23 @@ +name: Go +on: + push: + branches: + - master + - main +jobs: + build: + name: Build + runs-on: ubuntu-latest + steps: + - uses: actions/setup-go@v5 + with: + go-version: ^1.23 + check-latest: true + cache-dependency-path: | + **/go.sum + **/go.mod + + - uses: actions/checkout@v4 + - name: test and build + run: | + make diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..79d5b32 --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,41 @@ +name: Release +on: + push: + tags: + - "v*.*.*" +jobs: + build: + name: Build + runs-on: ubuntu-latest + steps: + - uses: actions/setup-go@v5 + with: + go-version: ^1.23 + check-latest: true + cache-dependency-path: | + **/go.sum + **/go.mod + + - uses: actions/checkout@v4 + - name: build matrix + run: | + make build GO='GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go' + tar czvf contatto-linux-amd64.tar.gz ./bin/ ./etc/contatto.toml + make clean + make build GO='GOOS=linux GOARCH=arm CGO_ENABLED=0 go' + tar czvf contatto-linux-arm.tar.gz ./bin/ ./etc/contatto.toml + make clean + make build GO='GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go' + tar czvf contatto-linux-arm64.tar.gz ./bin/ ./etc/contatto.toml + make clean + make build GO='GOOS=linux GOARCH=mips CGO_ENABLED=0 go' + tar czvf contatto-linux-mips.tar.gz ./bin/ ./etc/contatto.toml + make clean + make build GO='GOOS=linux GOARCH=mipsle CGO_ENABLED=0 go' + tar czvf contatto-linux-mipsle.tar.gz ./bin/ ./etc/contatto.toml + make clean + + - uses: ncipollo/release-action@v1 + with: + artifacts: "*.tar.gz" + token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index b626357..462b7a6 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,4 @@ go.work.sum **.env /bin /config.toml +/contatto.toml \ No newline at end of file diff --git a/Makefile b/Makefile index 22c3ecb..73b78fc 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ build: test -X main.date=$(shell date +%Y-%m-%d)" \ -o bin/contatto . run: build - ./bin/contatto proxy -c config.toml + ./bin/contatto proxy -c contatto.toml clean: rm -f ./bin/contatto diff --git a/conf/config.go b/etc/config.go similarity index 99% rename from conf/config.go rename to etc/config.go index 6eabc66..7aa786c 100644 --- a/conf/config.go +++ b/etc/config.go @@ -1,4 +1,4 @@ -package conf +package etc import ( "bytes" diff --git a/conf/config.toml b/etc/contatto.toml similarity index 100% rename from conf/config.toml rename to etc/contatto.toml diff --git a/conf/config.yaml b/etc/contatto.yaml similarity index 100% rename from conf/config.yaml rename to etc/contatto.yaml diff --git a/main.go b/main.go index 4735283..a8c41bf 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,7 @@ import ( "log" "github.com/alecthomas/kong" - "github.com/wweir/contatto/conf" + "github.com/wweir/contatto/etc" ) var cli struct { @@ -20,7 +20,7 @@ func init() { func main() { ctx := kong.Parse(&cli, kong.UsageOnError(), - kong.Description(fmt.Sprintf(`Contatto %s (%s %s)`, conf.Version, conf.Branch, conf.Date)), + kong.Description(fmt.Sprintf(`Contatto %s (%s %s)`, etc.Version, etc.Branch, etc.Date)), ) if err := ctx.Run(); err != nil { log.Fatalf("run failed: %v\n", err) diff --git a/proxy.go b/proxy.go index 561c973..328bbd2 100644 --- a/proxy.go +++ b/proxy.go @@ -13,7 +13,7 @@ import ( "github.com/containerd/containerd/v2/core/remotes/docker" "github.com/julienschmidt/httprouter" - "github.com/wweir/contatto/conf" + "github.com/wweir/contatto/etc" ) type ProxyCmd struct { @@ -23,7 +23,7 @@ type ProxyCmd struct { } func (c *ProxyCmd) Run() error { - if err := conf.InitConfig(c.Config); err != nil { + if err := etc.InitConfig(c.Config); err != nil { return err } @@ -48,9 +48,9 @@ func (c *ProxyCmd) Run() error { host = "docker.io" } - rule, ok := conf.RuleHostM[host] + rule, ok := etc.RuleHostM[host] if !ok { // no mapping rule, directly forward to the registry - if reg, ok := conf.RegHostM[host]; ok { + if reg, ok := etc.RegHostM[host]; ok { r.Out.URL.Host = reg.Host if reg.Insecure { r.Out.URL.Scheme = "http" @@ -66,7 +66,7 @@ func (c *ProxyCmd) Run() error { dstPat := &ImagePattern{} { // rewrite host, scheme, query - dstReg := conf.RegM[conf.Config.MirrorRegistry] + dstReg := etc.RegM[etc.Config.MirrorRegistry] dstPat.Registry = dstReg.Host if dstReg.Insecure { @@ -131,8 +131,8 @@ func (c *ProxyCmd) Run() error { var raw, mirror ImagePattern raw.ParseImage(w.Request.Header.Get("Contatto-Raw-Image")) mirror.ParseImage(w.Request.Header.Get("Contatto-Mirror-Image")) - if conf.OnMissing != nil { - cmdline, err := conf.OnMissing(map[string]any{ + if etc.OnMissing != nil { + cmdline, err := etc.OnMissing(map[string]any{ "Raw": raw, "Mirror": mirror, "raw": raw.String(), "mirror": mirror.String(), }) if err != nil { @@ -163,18 +163,18 @@ func (c *ProxyCmd) Run() error { return nil } - return http.ListenAndServe(conf.Config.Addr, proxy) + return http.ListenAndServe(etc.Config.Addr, proxy) } func (c *ProxyCmd) AuthCreds(host string) (string, string, error) { slog.Info("read auth creds", "registry", host) - reg := conf.RegHostM[host] + reg := etc.RegHostM[host] if reg.UserName != "" && reg.Password != "" { return reg.UserName, reg.Password, nil } - if auth, ok := conf.DockerAuth[host]; ok { + if auth, ok := etc.DockerAuth[host]; ok { return auth.UserName, auth.Password, nil }