Skip to content

Commit

Permalink
Merge pull request #5 from LinuxSuRen/fix-goget-server-base-image
Browse files Browse the repository at this point in the history
Change base image of the goget-server to golang:1.16
  • Loading branch information
LinuxSuRen authored Oct 19, 2021
2 parents 5c1f143 + 3d18442 commit e59a45b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
FROM alpine:3.10
FROM golang:1.16

RUN apt update && \
apt install upx -y

COPY goget-server /

Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
run:
go run cmd/server/root.go

build-server:
CGO_ENABLE=0 GOOS=linux GOARCH=amd64 go build -o bin/goget-server cmd/server/root.go
build-client:
go build -o bin/goget cmd/cli/root.go

goreleaser:
goreleaser build --snapshot --rm-dist

build-image: build-server
docker build bin -f Dockerfile -t ghcr.io/linuxsuren/goget-server:test
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
Usage: `goget github.com/linuxsuren/http-downloader`
This project aims to provide a way to get binary file from a Golang project easily. Users don't need to have a Golang
environment.

## Server

Usage:
```shell
docker run --restart always -d -v /var/data/goget:/tmp -p 7878:7878 ghcr.io/linuxsuren/goget-server:latest
```

## Client

Usage:
```shell
goget github.com/linuxsuren/http-downloader
```
2 changes: 1 addition & 1 deletion cmd/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (o *option) runE(cmd *cobra.Command, args []string) (err error) {

if resp.StatusCode == http.StatusOK {
// Create the file
out, err := os.OpenFile(binaryName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0544)
out, err := os.OpenFile(binaryName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0744)
if err != nil {
return err
}
Expand Down
28 changes: 22 additions & 6 deletions cmd/server/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ func createServerCommand() (cmd *cobra.Command) {

if wd, err = repo.Worktree(); err == nil {
if err = wd.Pull(&git.PullOptions{
Progress: cmd.OutOrStdout(),
Progress: cmd.OutOrStdout(),
ReferenceName: plumbing.NewBranchReferenceName(branch),
Force: true,
Force: true,
}); err != nil && err != git.NoErrAlreadyUpToDate {
err = fmt.Errorf("failed to pull git repository '%s', error: %v", repo, err)
} else {
Expand All @@ -58,9 +58,9 @@ func createServerCommand() (cmd *cobra.Command) {
}
} else {
_, err = git.PlainClone(dir, false, &git.CloneOptions{
URL: gitRepo,
URL: gitRepo,
ReferenceName: plumbing.NewBranchReferenceName(branch),
Progress: cmd.OutOrStdout(),
Progress: cmd.OutOrStdout(),
})
}

Expand Down Expand Up @@ -90,6 +90,11 @@ func createServerCommand() (cmd *cobra.Command) {
fmt.Println("success", binaryName)
binaryFilePath := path.Join(dir, binaryName)

// compress the binary file if upx is true
if upx := getUpx(r.URL); upx == "true" {
_ = RunCommandInDir("upx", dir, os.Environ(), []string{binaryName}...)
}

if binaryFileInfo, err := os.Stat(binaryFilePath); err == nil {
w.Header().Set("Content-Length", fmt.Sprintf("%d", binaryFileInfo.Size()))
w.Header().Set("Content-Type", "application/octet-stream")
Expand Down Expand Up @@ -118,15 +123,26 @@ func createServerCommand() (cmd *cobra.Command) {
return
}

func getUpx(httpURL *url.URL) (upx string) {
if upx = getValueFromURL(httpURL, "branch"); upx == "" {
upx = "true"
}
return
}

func getBranch(httpURL *url.URL) (branch string) {
if branch = httpURL.Query().Get("branch"); branch == "" {
if branch = getValueFromURL(httpURL, "branch"); branch == "" {
branch = "master"
}
return
}

func getValueFromURL(httpURL *url.URL, key string) string {
return httpURL.Query().Get(key)
}

func pairFromQuery(httpURL *url.URL, key, pairKey string) string {
if val := httpURL.Query().Get(key); val != "" {
if val := getValueFromURL(httpURL, key); val != "" {
return pair(pairKey, val)
}
return ""
Expand Down

0 comments on commit e59a45b

Please sign in to comment.