chore: detect wsl as separate os (resolve #817, #718)

This commit is contained in:
Ferdinand Mütsch
2025-08-02 11:03:15 +02:00
parent 70eafc3144
commit 7f281184de
4 changed files with 1183 additions and 1132 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,28 @@
package migrations
import (
"github.com/muety/wakapi/config"
"gorm.io/gorm"
)
func init() {
const name = "20250802-fix_wsl_os"
f := migrationFunc{
name: name,
background: true,
f: func(db *gorm.DB, cfg *config.Config) error {
if hasRun(name, db) {
return nil
}
if err := db.Exec("update heartbeats set operating_system = 'WSL' where user_agent like '%-WSL2-%'").Error; err != nil {
return err
}
setHasRun(name, db)
return nil
},
}
registerPostMigration(f)
}

View File

@@ -109,8 +109,10 @@ var userAgents = []struct {
nil,
},
{
"wakatime/v1.106.1 (linux-5.15.167.4-microsoft-standard-WSL2-unknown) go1.23.3 cursor/1.93.1 vscode-wakatime/24.9.2", // https://github.com/muety/wakapi/issues/718
"Linux",
// https://github.com/muety/wakapi/issues/817
// https://github.com/muety/wakapi/issues/718 (previously)
"wakatime/v1.106.1 (linux-5.15.167.4-microsoft-standard-WSL2-unknown) go1.23.3 cursor/1.93.1 vscode-wakatime/24.9.2",
"WSL",
"cursor",
nil,
},

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"github.com/duke-git/lancet/v2/condition"
"github.com/duke-git/lancet/v2/strutil"
"github.com/mileusna/useragent"
"io"
@@ -90,6 +91,7 @@ func ParseUserAgent(ua string) (string, string, error) { // os, editor, err
// try parse wakatime client user agents
var (
os, editor string
osAllCaps bool
)
if groups := userAgent.FindAllStringSubmatch(ua, -1); len(groups) > 0 && len(groups[0]) == 4 {
@@ -101,6 +103,11 @@ func ParseUserAgent(ua string) (string, string, error) { // os, editor, err
if os == "darwin" {
os = "macos"
}
// special treatment for wsl (see https://github.com/muety/wakapi/issues/817)
if strings.Contains(ua, "-WSL2-") {
os = "wsl"
osAllCaps = true
}
// parse editor
editor = groups[0][2] // for user agents sent by desktop-wakatime plugin and some others, see https://github.com/muety/wakapi/issues/686, https://github.com/muety/wakapi/issues/712
@@ -112,7 +119,8 @@ func ParseUserAgent(ua string) (string, string, error) { // os, editor, err
editor = "kate"
}
return strutil.Capitalize(os), editor, nil
os = condition.Ternary[bool, string](osAllCaps, strings.ToUpper(os), strutil.Capitalize(os))
return os, editor, nil
}
// try parse browser user agent as a fallback