Skip to content

Commit

Permalink
Merge pull request #62 from hslatman/automatic-user-agent-version-2
Browse files Browse the repository at this point in the history
Add automatic module version detection
  • Loading branch information
hslatman authored Dec 4, 2024
2 parents 982ac18 + c136e52 commit a681cdc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
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
}

0 comments on commit a681cdc

Please sign in to comment.