Skip to content

Commit

Permalink
Merge pull request #15 from Cian911/swb/docker-example
Browse files Browse the repository at this point in the history
SWB: Add support for Docker
  • Loading branch information
Cian911 committed Jan 23, 2022
2 parents 31c0eb0 + 04d30a6 commit 57f2235
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 7 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ on:
push:
tags:
- "*"

permissions:
contents: write

jobs:
release:
runs-on: ubuntu-latest
Expand All @@ -12,6 +16,14 @@ jobs:
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Setup QEMU
uses: docker/setup-qemu-action@v1
- name: Docker Login
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.HOMEBREW_TAP_GITHUB_TOKEN }}
- name: Go Setup
uses: actions/setup-go@v2
with:
Expand Down
16 changes: 15 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,21 @@ builds:
- 5
- 6
- 7

dockers:
-
image_templates: ["ghcr.io/cian911/{{ .ProjectName }}:{{ .Version }}"]
dockerfile: Dockerfile
use: buildx
build_flag_templates:
- --platform=linux/amd64
- --label=org.opencontainers.image.title={{ .ProjectName }}
- --label=org.opencontainers.image.description={{ .ProjectName }}
- --label=org.opencontainers.image.url=https://github.com/cian911/{{ .ProjectName }}
- --label=org.opencontainers.image.source=https://github.com/cian911/{{ .ProjectName }}
- --label=org.opencontainers.image.version={{ .Version }}
- --label=org.opencontainers.image.created={{ time "2006-01-02T15:04:05Z07:00" }}
- --label=org.opencontainers.image.revision={{ .FullCommit }}
- --label=org.opencontainers.image.licenses=GPL-3.0
archives:
-
id: switchboard
Expand Down
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM alpine

COPY switchboard /usr/local/bin/switchboard

ENTRYPOINT ["switchboard"]
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: build build-arm run
.PHONY: build build-arm build-debian run
.PHONY: test-all test-watcher test-event test-utils test-cmd lint-all vet-all

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

build-debian:
@GOOS=linux GOARCH=amd64 go build \
-ldflags "-X main.Version=${VERSION} -X main.Build=${BUILD} -X main.BuildTime=${BUILD_TIME}" \
-o ./bin/switchboard ./cmd

build-arm:
@GOOS=linux GOARCH=arm GOARM=5 go build \
-ldflags "-X main.Version=${VERSION} -X main.Build=${BUILD} -X main.BuildTime=${BUILD_TIME}" \
Expand Down
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,32 @@ brew install switchboard
switchboard -h
```

You can also upgrade the version of `switchboard` you already have installed by doing the following.

```sh
brew upgrade switchboard
```

##### Docker

```sh
docker pull ghcr.io/cian911/switchboard:${VERSION}

docker run -d -v ${SRC} -v ${DEST} ghcr.io/cian911/switchboard:${VERSION} watch -h
```

##### Go Install

```sh
go install github.com/Cian911/switchboard@latest
go install github.com/Cian911/switchboard@${VERSION}
```

##### Manually

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.
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.

```sh
wget https://github.com/Cian911/switchboard/releases/download/${VERSION}/${PACKAGE_NAME}
curl https://github.com/Cian911/switchboard/releases/download/${VERSION}/${PACKAGE_NAME} -o ${PACKAGE_NAME}
sudo tar -xvf ${PACKAGE_NAME} -C /usr/local/bin/
sudo chmod +x /usr/local/bin/switchboard
```
Expand Down
28 changes: 26 additions & 2 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package event

import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"time"

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

err := os.Rename(fmt.Sprintf("%s%s", path, file), fmt.Sprintf("%s/%s", e.Destination, e.File))
return err
sourcePath := filepath.Join(path, file)
destPath := filepath.Join(e.Destination, e.File)

inputFile, err := os.Open(sourcePath)
if err != nil {
return fmt.Errorf("Couldn't open source file: %s", err)
}
outputFile, err := os.Create(destPath)
if err != nil {
inputFile.Close()
return fmt.Errorf("Couldn't open dest file: %s", err)
}
defer outputFile.Close()
_, err = io.Copy(outputFile, inputFile)
inputFile.Close()
if err != nil {
return fmt.Errorf("Writing to output file failed: %s", err)
}
// The copy was successful, so now delete the original file
err = os.Remove(sourcePath)
if err != nil {
return fmt.Errorf("Failed removing original file: %s", err)
}
return nil
}

// IsValidEvent checks if the event operation and file extension is valid
Expand Down
20 changes: 20 additions & 0 deletions event/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ func TestEvent(t *testing.T) {
if _, err := os.Stat(fmt.Sprintf("%s/%s", event.Destination, event.File)); errors.Is(err, os.ErrNotExist) {
t.Fatalf("Failed to move from %s/%s to %s/%s: %v : %v", event.Path, event.File, event.Destination, event.File, err, *event)
}

// If the file still exists in the source directory, log an error
if _, err := os.Stat(fmt.Sprintf("%s/%s", event.Path, event.File)); !errors.Is(err, os.ErrNotExist) {
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)
}
})

t.Run("It moves file from one dir to another dir with valid destPath", func(t *testing.T) {
event := eventSetup(t)
event.Move(fmt.Sprintf("%s/", event.Path), "")

// If the file does not exist, log an error
if _, err := os.Stat(fmt.Sprintf("%s/%s", event.Destination, event.File)); errors.Is(err, os.ErrNotExist) {
t.Fatalf("Failed to move from %s/%s to %s/%s: %v : %v", event.Path, event.File, event.Destination, event.File, err, *event)
}

// If the file still exists in the source directory, log an error
if _, err := os.Stat(fmt.Sprintf("%s/%s", event.Path, event.File)); !errors.Is(err, os.ErrNotExist) {
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)
}
})

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

0 comments on commit 57f2235

Please sign in to comment.