Skip to content

Commit

Permalink
add support for custom path in uaparser cli
Browse files Browse the repository at this point in the history
  • Loading branch information
negrel committed Nov 6, 2024
1 parent b5778a8 commit 56ffede
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions cmd/uaparser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ import (
"github.com/prismelabs/analytics/pkg/log"
"github.com/prismelabs/analytics/pkg/services/uaparser"
"github.com/prismelabs/analytics/pkg/wired"
"github.com/tidwall/gjson"
)

// JSON from https://useragents.me
type userAgentsMe struct {
Ua string `json:"ua"`
}

func main() {
if len(os.Args) < 2 {
fmt.Println("User agents list file path is missing")
os.Exit(1)
}

path := ""
if len(os.Args) == 3 {
path = os.Args[2]
}

logger := log.NewLogger("uaparser", os.Stderr, false)
registry := wired.ProvidePrometheusRegistry()
uaParser := uaparser.ProvideService(logger, registry)
Expand All @@ -30,15 +31,19 @@ func main() {
panic(err)
}

var userAgentsList []userAgentsMe
err = json.Unmarshal(userAgents, &userAgentsList)
if err != nil {
panic(err)
if !gjson.ValidBytes(userAgents) {
logger.Warn().Msg("json is invalid, results may be incoherent")
}

userAgentsList := gjson.Parse(string(userAgents)).Array()

var clients []uaparser.Client
for _, item := range userAgentsList {
client := uaParser.ParseUserAgent(item.Ua)
ua := item.String()
if path != "" {
ua = item.Get(path).String()
}
client := uaParser.ParseUserAgent(ua)
clients = append(clients, client)
}

Expand Down

0 comments on commit 56ffede

Please sign in to comment.