Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
krisukox committed Mar 12, 2023
1 parent c731609 commit 358d3d1
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:

- name: Build binary
run: |
GOOS=${{ matrix.target.os }} GOARCH=${{ matrix.target.arch }} CGO_ENABLED=0 go build -o bazels3cache-${{ matrix.target.os }}-${{ matrix.target.arch }} ./...
GOOS=${{ matrix.target.os }} GOARCH=${{ matrix.target.arch }} CGO_ENABLED=0 go build -o bazels3cache-${{ matrix.target.os }}-${{ matrix.target.arch }} .
- name: Upload artifact
uses: actions/upload-artifact@v3
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
build:
CGO_ENABLED=0 go build -o bazels3cache ./...
CGO_ENABLED=0 go build -o bazels3cache .

build-debug:
CGO_ENABLED=0 go build -tags debug -o bazels3cache ./...
CGO_ENABLED=0 go build -tags debug -o bazels3cache .

start:
./bazels3cache --bucket bazel
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ Choose your platform from the [releases page](https://github.com/krisukox/bazels

```
sudo wget https://github.com/krisukox/bazels3cache/releases/latest/download/bazels3cache-linux-amd64 -O /usr/local/bin/bazels3cache
chmod u+x /usr/local/bin/bazels3cache
sudo chmod +x /usr/local/bin/bazels3cache
hash -r
```

## Installation using Go

```
go install -v github.com/krisukox/bazels3cache@latest
go install -v github.com/krisukox/bazels3cache@HEAD
```

## Testing
Expand Down
15 changes: 11 additions & 4 deletions app/daemon.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package app

import (
"bytes"
Expand All @@ -17,15 +17,22 @@ import (
"github.com/aws/aws-sdk-go-v2/service/s3/types"
)

const (
DefaultPort = 7777
ShutdownUrlTmpl = "http://localhost:%d/shutdown"
RootPortEnv = "ROOT_PORT"
SuccessMsg = "success"
)

type Daemon struct {
s3client *s3.Client
bucketName string
infoLog *log.Logger
errorLog *log.Logger
}

func daemonProcess(bucketName, s3url string, port int, infoLog, errorLog *log.Logger) error {
rootPort, err := strconv.Atoi(os.Getenv(rootPortEnv))
func DaemonProcess(bucketName, s3url string, port int, infoLog, errorLog *log.Logger) error {
rootPort, err := strconv.Atoi(os.Getenv(RootPortEnv))
if err != nil {
return fmt.Errorf("internal application error: %v", err)
}
Expand Down Expand Up @@ -160,7 +167,7 @@ func sendMsg(rootPort int, msg string) error {
}

func sendSuccess(rootPort int) error {
return sendMsg(rootPort, successMsg)
return sendMsg(rootPort, SuccessMsg)
}

func sendError(rootPort int, err error) error {
Expand Down
2 changes: 1 addition & 1 deletion app/s3client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build !debug
// +build !debug

package main
package app

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion app/s3client_testutils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build debug
// +build debug

package main
package app

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/krisukox/bazel-s3-cache
module github.com/krisukox/bazels3cache

go 1.20

Expand Down
22 changes: 8 additions & 14 deletions app/main.go → main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,12 @@ import (
"strconv"
"syscall"

"github.com/krisukox/bazels3cache/app"
"github.com/sevlyar/go-daemon"
)

const (
defaultPort = 7777
shutdownUrlTmpl = "http://localhost:%d/shutdown"
rootPortEnv = "ROOT_PORT"
successMsg = "success"
)

func sendShutdownToDaemon(port int) error {
resp, err := http.Get(fmt.Sprintf(shutdownUrlTmpl, port))
resp, err := http.Get(fmt.Sprintf(app.ShutdownUrlTmpl, port))
if err != nil {
if errors.Is(err, syscall.ECONNREFUSED) {
return fmt.Errorf("server is not running")
Expand Down Expand Up @@ -56,7 +50,7 @@ func createDaemonContext(rootPort int) *daemon.Context {
LogFileName: filepath.Join(workDir, ".bazels3cache.log"),
LogFilePerm: 0640,
Umask: 027,
Env: append(os.Environ(), rootPortEnv+"="+strconv.Itoa(rootPort)),
Env: append(os.Environ(), app.RootPortEnv+"="+strconv.Itoa(rootPort)),
Args: append(os.Args, "[bazels3cache-daemon]"),
}
}
Expand All @@ -72,21 +66,21 @@ func rootProcess(port int, logFile string, ln net.Listener) error {
if err != nil {
return err
}
if string(buf) == successMsg {
if string(buf) == app.SuccessMsg {
executablePath, err := os.Executable()
if err != nil {
executablePath = ""
}
executableName := filepath.Base(executablePath)

portSwitch := ""
if port != defaultPort {
if port != app.DefaultPort {
portSwitch = " --port " + strconv.Itoa(port)
}

fmt.Printf(
"Server `%[1]s` is running, to stop it run `%[1]s --stop%[2]s` or `curl %[3]s`\n",
executableName, portSwitch, fmt.Sprintf(shutdownUrlTmpl, port),
executableName, portSwitch, fmt.Sprintf(app.ShutdownUrlTmpl, port),
)
fmt.Printf("Logging to %s\n", logFile)
return nil
Expand All @@ -112,7 +106,7 @@ func listenOnAny(avoidPort int) (net.Listener, int, error) {

func main() {
bucketName := flag.String("bucket", "", "S3 bucket name")
daemonPort := flag.Int("port", defaultPort, "Server HTTP port number")
daemonPort := flag.Int("port", app.DefaultPort, "Server HTTP port number")
stop := flag.Bool("stop", false, "Stop application")
s3url := flag.String("s3url", "", "S3 url used for testing")
flag.Parse()
Expand Down Expand Up @@ -154,7 +148,7 @@ func main() {
infoLog := log.New(os.Stdout, "INFO\t", log.Ltime)
errorLog := log.New(os.Stderr, "ERROR\t", log.Ltime)

if err := daemonProcess(*bucketName, *s3url, *daemonPort, infoLog, errorLog); err != nil {
if err := app.DaemonProcess(*bucketName, *s3url, *daemonPort, infoLog, errorLog); err != nil {
errorLog.Fatal(err)
}
}

0 comments on commit 358d3d1

Please sign in to comment.