Skip to content

Commit

Permalink
feat: add Docker support
Browse files Browse the repository at this point in the history
  • Loading branch information
suxb201 committed Sep 10, 2024
1 parent 6405b4c commit f06223c
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 481 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Docker Publish

on:
workflow_dispatch:
push:
tags:
- 'v*'

jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up QEMU
uses: docker/setup-qemu-action@v1

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v3
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha
- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY . .
RUN go mod download
RUN go build -o redis-shake ./cmd/redis-shake

FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/redis-shake .
COPY shake_sync_env.toml .
COPY shake_scan_env.toml .
COPY entrypoint.sh .

RUN chmod +x entrypoint.sh

ENTRYPOINT ["/bin/sh", "entrypoint.sh"]
9 changes: 9 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh
if [ "$SYNC" = "true" ]; then
./redis-shake shake_sync_env.toml
elif [ "$SCAN" = "true" ]; then
./redis-shake shake_scan_env.toml
else
echo "Error: Neither SYNC nor SCAN environment variable is set to true"
exit 1
fi
28 changes: 17 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,31 @@ require (
github.com/gofrs/flock v0.8.1
github.com/mcuadros/go-defaults v1.2.0
github.com/rs/zerolog v1.28.0
github.com/spf13/viper v1.15.0
github.com/spf13/viper v1.18.1
github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64
)

require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/a8m/envsubst v1.4.2 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.12.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
502 changes: 51 additions & 451 deletions go.sum

Large diffs are not rendered by default.

42 changes: 23 additions & 19 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package config

import (
"bytes"
"fmt"
"os"
"strings"

"RedisShake/internal/log"

"github.com/a8m/envsubst"
"github.com/mcuadros/go-defaults"
"github.com/rs/zerolog"
"github.com/spf13/viper"
)

type FilterOptions struct {
AllowKeyPrefix []string `mapstructure:"allow_key_prefix" default:"[]"`
AllowKeySuffix []string `mapstructure:"allow_key_suffix" default:"[]"`
BlockKeyPrefix []string `mapstructure:"block_key_prefix" default:"[]"`
BlockKeySuffix []string `mapstructure:"block_key_suffix" default:"[]"`
AllowDB []int `mapstructure:"allow_db" default:"[]"`
BlockDB []int `mapstructure:"block_db" default:"[]"`
AllowCommand []string `mapstructure:"allow_command" default:"[]"`
BlockCommand []string `mapstructure:"block_command" default:"[]"`
AllowKeyPrefix []string `mapstructure:"allow_key_prefix" default:"[]"`
AllowKeySuffix []string `mapstructure:"allow_key_suffix" default:"[]"`
BlockKeyPrefix []string `mapstructure:"block_key_prefix" default:"[]"`
BlockKeySuffix []string `mapstructure:"block_key_suffix" default:"[]"`
AllowDB []int `mapstructure:"allow_db" default:"[]"`
BlockDB []int `mapstructure:"block_db" default:"[]"`
AllowCommand []string `mapstructure:"allow_command" default:"[]"`
BlockCommand []string `mapstructure:"block_command" default:"[]"`
AllowCommandGroup []string `mapstructure:"allow_command_group" default:"[]"`
BlockCommandGroup []string `mapstructure:"block_command_group" default:"[]"`
Function string `mapstructure:"function" default:""`
Expand Down Expand Up @@ -73,7 +75,7 @@ func (opt *AdvancedOptions) GetPSyncCommand(address string) string {
}

type ShakeOptions struct {
Filter FilterOptions
Filter FilterOptions
Advanced AdvancedOptions
Module ModuleOptions
}
Expand All @@ -97,18 +99,20 @@ func LoadConfig() *viper.Viper {
if len(os.Args) == 2 {
logger.Info().Msgf("load config from file: %s", os.Args[1])
configFile := os.Args[1]
v.SetConfigFile(configFile)
err := v.ReadInConfig()
buf, err := envsubst.ReadFile(configFile)
if err != nil {
panic(err)
logger.Error().Msgf("failed to read config file: %v", err)
os.Exit(1)
}
}

// load config from environment variables
if len(os.Args) == 1 {
logger.Warn().Msg("load config from environment variables")
v.SetConfigType("env")
v.AutomaticEnv()
v.SetConfigType("toml")
err = v.ReadConfig(bytes.NewReader(buf))
if err != nil {
logger.Error().Msgf("failed to read config file: %v", err)
os.Exit(1)
}
} else {
logger.Error().Msg("config file not found")
os.Exit(1)
}

// unmarshal config
Expand Down
5 changes: 5 additions & 0 deletions shake_scan_env.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[scan_reader]
address = "${SHAKE_SRC_ADDRESS}" # export SHAKE_SRC_ADDRESS=127.0.0.1:6379

[redis_writer]
address = "${SHAKE_DST_ADDRESS}" # export SHAKE_DST_ADDRESS=127.0.0.1:6380
5 changes: 5 additions & 0 deletions shake_sync_env.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[sync_reader]
address = "${SHAKE_SRC_ADDRESS}" # export SHAKE_SRC_ADDRESS=127.0.0.1:6379

[redis_writer]
address = "${SHAKE_DST_ADDRESS}" # export SHAKE_DST_ADDRESS=127.0.0.1:6380

0 comments on commit f06223c

Please sign in to comment.