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

Add automatic module version detection #62

Merged
merged 1 commit into from
Dec 4, 2024
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
16 changes: 12 additions & 4 deletions internal/bouncer/bouncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,26 @@ import (

"github.com/crowdsecurity/crowdsec/pkg/models"
csbouncer "github.com/crowdsecurity/go-cs-bouncer"
"github.com/hslatman/caddy-crowdsec-bouncer/internal/version"

"go.uber.org/zap"
)

const (
userAgentName = "caddy-cs-bouncer"
userAgentVersion = "v0.8.0"
userAgent = userAgentName + "/" + userAgentVersion

userAgentName = "caddy-cs-bouncer"
maxNumberOfDecisionsToLog = 10
)

var (
userAgent string
userAgentVersion string
)

func init() {
userAgentVersion = version.Current()
userAgent = userAgentName + "/" + userAgentVersion
}

// Bouncer is a wrapper for a CrowdSec bouncer. It supports both the
// streaming and live bouncer implementations. The streaming bouncer is
// backed by an immutable radix tree storing known bad IPs and IP ranges.
Expand Down
25 changes: 25 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package version

import (
"runtime/debug"
)

const (
modulePath = "github.com/hslatman/caddy-crowdsec-bouncer"
fallback = "v0.8.0"
)

func Current() string {
info, ok := debug.ReadBuildInfo()
if !ok {
return fallback
}

for _, d := range info.Deps {
if d.Path == modulePath {
return d.Version
}
}

return fallback
}
13 changes: 13 additions & 0 deletions internal/version/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package version

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCurrent(t *testing.T) {
v := Current()

assert.Equal(t, "v0.8.0", v) // fallback
}
Loading