Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests and updates #5

Merged
merged 9 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
fetch-depth: 2
- uses: actions/setup-go@v3
with:
go-version: '1.19'
go-version: '1.21'
- name: Run coverage
run: go test -coverpkg=./... ./... -race -coverprofile=coverage.out -covermode=atomic
- name: Upload coverage to Codecov
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@
# Dependency directories (remove the comment below to include it)
# vendor/
.idea/

# ignore local buid
bedel
32 changes: 31 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,34 @@ package cmd

import (
"fmt"
"log/slog"
"os"
"path"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var cfgFile string

var lvl = new(slog.LevelVar)
var logger = slog.New(
slog.NewJSONHandler(
os.Stdout,
&slog.HandlerOptions{
Level: lvl,
AddSource: true,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.SourceKey {
s := a.Value.Any().(*slog.Source)
s.File = path.Base(s.File)
}
return a
},
},
),
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "bedel",
Expand All @@ -49,6 +69,9 @@ func init() {
viper.BindPFlag("password", rootCmd.PersistentFlags().Lookup("password"))
rootCmd.PersistentFlags().StringP("username", "u", "", "username to manage acls")
viper.BindPFlag("username", rootCmd.PersistentFlags().Lookup("username"))
rootCmd.PersistentFlags().StringP("logLevel", "l", "INFO", "set default logLevel")
viper.BindPFlag("logLevel", rootCmd.PersistentFlags().Lookup("logLevel"))

}

// initConfig reads in config file and ENV variables if set.
Expand All @@ -68,6 +91,7 @@ func initConfig() {
}

viper.SetDefault("syncInterval", 10)
viper.SetDefault("username", "default")
viper.AutomaticEnv()

if err := viper.ReadInConfig(); err == nil {
Expand All @@ -85,7 +109,13 @@ func initConfig() {
}

if !viper.IsSet("username") {
fmt.Fprintln(os.Stderr, "username is required")
viper.SetDefault("username", "default")
}

err := lvl.UnmarshalText([]byte(viper.GetString("logLevel")))
if err != nil {
logger.Error(err.Error())
os.Exit(1)
}
slog.SetDefault(logger)
}
4 changes: 2 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
// runCmd represents the run command
var runCmd = &cobra.Command{
Use: "run",
Short: "Run the acl manager in mood loop, it will sync the slave with the master",
Short: "Run the acl manager in mood loop, it will sync the follower with the master",
Run: func(cmd *cobra.Command, args []string) {
mgr := aclmanager.New(viper.GetString("address"), viper.GetString("password"), viper.GetString("password"))
mgr := aclmanager.New(viper.GetString("address"), viper.GetString("username"), viper.GetString("password"))
ctx := cmd.Context()
err := mgr.Loop(ctx)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/runOnce.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
// runOnceCmd represents the runOnce command
var runOnceCmd = &cobra.Command{
Use: "runOnce",
Short: "Run the acl manager once, it will sync the slave with the master",
Short: "Run the acl manager once, it will sync the follower with the master",
Run: func(cmd *cobra.Command, args []string) {
mgr := aclmanager.New(viper.GetString("address"), viper.GetString("password"), viper.GetString("password"))
mgr := aclmanager.New(viper.GetString("address"), viper.GetString("username"), viper.GetString("password"))
err := mgr.SyncAcls()
if err != nil {
panic(err)
Expand Down
3 changes: 3 additions & 0 deletions configs/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM scratch
COPY bedel /bedel
ENTRYPOINT ["/bedel"]
15 changes: 15 additions & 0 deletions configs/docker/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.PHONY: build docker-build down up

all: build docker-build down up

build:
GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" ../../

docker-build:
docker build -t ncode/bedel:dev .

up:
docker compose up

dowm:
docker compose dowm
40 changes: 28 additions & 12 deletions configs/docker/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,41 @@ services:
container_name: redis0002
volumes:
- ./redis0002:/usr/local/etc/redis:rw
command:
- "redis-server"
- "/usr/local/etc/redis/redis.conf"
- "--slaveof"
- "redis0001"
- "6379"

redis0003:
image: redis:latest
container_name: redis-slave2
volumes:
- ./redis0003:/usr/local/etc/redis:rw
command:
- "redis-server"
- "/usr/local/etc/redis/redis.conf"
- "--slaveof"
- "redis0001"
- "6379"

bedel_redis0001:
image: ncode/bedel:dev
container_name: bedel_redis0001
depends_on:
- vault
command:
- "run"
- "-a"
- "redis0001:6379"
- "-p"
- "bedel-integration-test"
- "-l"
- "info"

bedel_redis0002:
image: ncode/bedel:dev
container_name: bedel_redis0002
depends_on:
- vault
command:
- "run"
- "-a"
- "redis0002:6379"
- "-p"
- "bedel-integration-test"
- "-l"
- "info"

vault:
image: hashicorp/vault:latest
container_name: vault
Expand Down
Loading