Skip to content

Commit 57f2235

Browse files
authored
Merge pull request #15 from Cian911/swb/docker-example
SWB: Add support for Docker
2 parents 31c0eb0 + 04d30a6 commit 57f2235

File tree

7 files changed

+101
-7
lines changed

7 files changed

+101
-7
lines changed

.github/workflows/release.yml

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ on:
44
push:
55
tags:
66
- "*"
7+
8+
permissions:
9+
contents: write
10+
711
jobs:
812
release:
913
runs-on: ubuntu-latest
@@ -12,6 +16,14 @@ jobs:
1216
uses: actions/checkout@v2
1317
with:
1418
fetch-depth: 0
19+
- name: Setup QEMU
20+
uses: docker/setup-qemu-action@v1
21+
- name: Docker Login
22+
uses: docker/login-action@v1
23+
with:
24+
registry: ghcr.io
25+
username: ${{ github.repository_owner }}
26+
password: ${{ secrets.HOMEBREW_TAP_GITHUB_TOKEN }}
1527
- name: Go Setup
1628
uses: actions/setup-go@v2
1729
with:

.goreleaser.yml

+15-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,21 @@ builds:
2626
- 5
2727
- 6
2828
- 7
29-
29+
dockers:
30+
-
31+
image_templates: ["ghcr.io/cian911/{{ .ProjectName }}:{{ .Version }}"]
32+
dockerfile: Dockerfile
33+
use: buildx
34+
build_flag_templates:
35+
- --platform=linux/amd64
36+
- --label=org.opencontainers.image.title={{ .ProjectName }}
37+
- --label=org.opencontainers.image.description={{ .ProjectName }}
38+
- --label=org.opencontainers.image.url=https://github.com/cian911/{{ .ProjectName }}
39+
- --label=org.opencontainers.image.source=https://github.com/cian911/{{ .ProjectName }}
40+
- --label=org.opencontainers.image.version={{ .Version }}
41+
- --label=org.opencontainers.image.created={{ time "2006-01-02T15:04:05Z07:00" }}
42+
- --label=org.opencontainers.image.revision={{ .FullCommit }}
43+
- --label=org.opencontainers.image.licenses=GPL-3.0
3044
archives:
3145
-
3246
id: switchboard

Dockerfile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM alpine
2+
3+
COPY switchboard /usr/local/bin/switchboard
4+
5+
ENTRYPOINT ["switchboard"]

Makefile

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: build build-arm run
1+
.PHONY: build build-arm build-debian run
22
.PHONY: test-all test-watcher test-event test-utils test-cmd lint-all vet-all
33

44
VERSION := test-build
@@ -10,6 +10,11 @@ build:
1010
-ldflags "-X main.Version=${VERSION} -X main.Build=${BUILD} -X main.BuildTime=${BUILD_TIME}" \
1111
-o ./bin/switchboard ./cmd
1212

13+
build-debian:
14+
@GOOS=linux GOARCH=amd64 go build \
15+
-ldflags "-X main.Version=${VERSION} -X main.Build=${BUILD} -X main.BuildTime=${BUILD_TIME}" \
16+
-o ./bin/switchboard ./cmd
17+
1318
build-arm:
1419
@GOOS=linux GOARCH=arm GOARM=5 go build \
1520
-ldflags "-X main.Version=${VERSION} -X main.Build=${BUILD} -X main.BuildTime=${BUILD_TIME}" \

README.md

+17-3
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,32 @@ brew install switchboard
3232
switchboard -h
3333
```
3434

35+
You can also upgrade the version of `switchboard` you already have installed by doing the following.
36+
37+
```sh
38+
brew upgrade switchboard
39+
```
40+
41+
##### Docker
42+
43+
```sh
44+
docker pull ghcr.io/cian911/switchboard:${VERSION}
45+
46+
docker run -d -v ${SRC} -v ${DEST} ghcr.io/cian911/switchboard:${VERSION} watch -h
47+
```
48+
3549
##### Go Install
3650

3751
```sh
38-
go install github.com/Cian911/switchboard@latest
52+
go install github.com/Cian911/switchboard@${VERSION}
3953
```
4054

4155
##### Manually
4256

43-
You can download the pre-compiled binary for your specific OS type from the [OSS releases page](https://github.com/Cian911/switchboard/releases). You will need to copy these and extract the binary, then move it to you local bin directory. See the example below.
57+
You can download the pre-compiled binary for your specific OS type from the [OSS releases page](https://github.com/Cian911/switchboard/releases). You will need to copy these and extract the binary, then move it to you local bin directory. See the example below for extracting a zipped version.
4458

4559
```sh
46-
wget https://github.com/Cian911/switchboard/releases/download/${VERSION}/${PACKAGE_NAME}
60+
curl https://github.com/Cian911/switchboard/releases/download/${VERSION}/${PACKAGE_NAME} -o ${PACKAGE_NAME}
4761
sudo tar -xvf ${PACKAGE_NAME} -C /usr/local/bin/
4862
sudo chmod +x /usr/local/bin/switchboard
4963
```

event/event.go

+26-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package event
22

33
import (
44
"fmt"
5+
"io"
56
"log"
67
"os"
8+
"path/filepath"
79
"time"
810

911
"github.com/cian911/switchboard/utils"
@@ -42,8 +44,30 @@ func New(file, path, dest, ext string) *Event {
4244
func (e *Event) Move(path, file string) error {
4345
log.Printf("Moving e.Path: %s to %s/%s\n", path, e.Destination, e.File)
4446

45-
err := os.Rename(fmt.Sprintf("%s%s", path, file), fmt.Sprintf("%s/%s", e.Destination, e.File))
46-
return err
47+
sourcePath := filepath.Join(path, file)
48+
destPath := filepath.Join(e.Destination, e.File)
49+
50+
inputFile, err := os.Open(sourcePath)
51+
if err != nil {
52+
return fmt.Errorf("Couldn't open source file: %s", err)
53+
}
54+
outputFile, err := os.Create(destPath)
55+
if err != nil {
56+
inputFile.Close()
57+
return fmt.Errorf("Couldn't open dest file: %s", err)
58+
}
59+
defer outputFile.Close()
60+
_, err = io.Copy(outputFile, inputFile)
61+
inputFile.Close()
62+
if err != nil {
63+
return fmt.Errorf("Writing to output file failed: %s", err)
64+
}
65+
// The copy was successful, so now delete the original file
66+
err = os.Remove(sourcePath)
67+
if err != nil {
68+
return fmt.Errorf("Failed removing original file: %s", err)
69+
}
70+
return nil
4771
}
4872

4973
// IsValidEvent checks if the event operation and file extension is valid

event/event_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ func TestEvent(t *testing.T) {
4949
if _, err := os.Stat(fmt.Sprintf("%s/%s", event.Destination, event.File)); errors.Is(err, os.ErrNotExist) {
5050
t.Fatalf("Failed to move from %s/%s to %s/%s: %v : %v", event.Path, event.File, event.Destination, event.File, err, *event)
5151
}
52+
53+
// If the file still exists in the source directory, log an error
54+
if _, err := os.Stat(fmt.Sprintf("%s/%s", event.Path, event.File)); !errors.Is(err, os.ErrNotExist) {
55+
t.Fatalf("Failed to delete file from %s/%s to %s/%s after Move: %v : %v", event.Path, event.File, event.Destination, event.File, err, *event)
56+
}
57+
})
58+
59+
t.Run("It moves file from one dir to another dir with valid destPath", func(t *testing.T) {
60+
event := eventSetup(t)
61+
event.Move(fmt.Sprintf("%s/", event.Path), "")
62+
63+
// If the file does not exist, log an error
64+
if _, err := os.Stat(fmt.Sprintf("%s/%s", event.Destination, event.File)); errors.Is(err, os.ErrNotExist) {
65+
t.Fatalf("Failed to move from %s/%s to %s/%s: %v : %v", event.Path, event.File, event.Destination, event.File, err, *event)
66+
}
67+
68+
// If the file still exists in the source directory, log an error
69+
if _, err := os.Stat(fmt.Sprintf("%s/%s", event.Path, event.File)); !errors.Is(err, os.ErrNotExist) {
70+
t.Fatalf("Failed to delete file from %s/%s to %s/%s after Move: %v : %v", event.Path, event.File, event.Destination, event.File, err, *event)
71+
}
5272
})
5373

5474
t.Run("It does not move file from one dir to another dir", func(t *testing.T) {

0 commit comments

Comments
 (0)