Skip to content

Commit 5932504

Browse files
authored
feat: add a unique id to dbc requests (#44)
closes #24 I created github.com/columnar-tech/machine-id to get something a bit better than creating and storing a GUID.
1 parent 72a7115 commit 5932504

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

drivers.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net/url"
1212
"os"
1313
"path"
14+
"path/filepath"
1415
"runtime"
1516
"runtime/debug"
1617
"slices"
@@ -21,6 +22,8 @@ import (
2122
"github.com/Masterminds/semver/v3"
2223
"github.com/ProtonMail/gopenpgp/v3/crypto"
2324
"github.com/goccy/go-yaml"
25+
"github.com/google/uuid"
26+
machineid "github.com/zeroshade/machine-id"
2427
)
2528

2629
const defaultURL = "https://dbc-cdn.columnar.tech"
@@ -29,6 +32,8 @@ var (
2932
baseURL = defaultURL
3033
Version = "unknown"
3134
userAgent string
35+
mid string
36+
uid uuid.UUID
3237
)
3338

3439
func init() {
@@ -50,6 +55,41 @@ func init() {
5055
userAgent += " CI"
5156
}
5257
}
58+
59+
mid, _ = machineid.ProtectedID()
60+
61+
// get user config dir
62+
userdir, err := os.UserConfigDir()
63+
if err != nil {
64+
// if we can't get the dir for some reason, just generate a new UUID
65+
uid = uuid.New()
66+
return
67+
}
68+
69+
// try to read the existing UUID file
70+
dirname := "columnar"
71+
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
72+
dirname = "Columnar"
73+
}
74+
75+
fp := filepath.Join(userdir, dirname, "dbc", "uid.uuid")
76+
data, err := os.ReadFile(fp)
77+
if err == nil {
78+
if err = uid.UnmarshalBinary(data); err == nil {
79+
return
80+
}
81+
}
82+
83+
// if the file didn't exist or we couldn't parse it, generate a new uuid
84+
// and then write a new file
85+
uid = uuid.New()
86+
// if we fail to create the dir or write the file, just ignore the error
87+
// and use the fresh UUID
88+
if err = os.MkdirAll(filepath.Dir(fp), 0o700); err == nil {
89+
if data, err = uid.MarshalBinary(); err == nil {
90+
os.WriteFile(fp, data, 0o600)
91+
}
92+
}
5393
}
5494

5595
func makereq(u string) (resp *http.Response, err error) {
@@ -58,6 +98,11 @@ func makereq(u string) (resp *http.Response, err error) {
5898
return nil, fmt.Errorf("failed to parse URL %s: %w", uri, err)
5999
}
60100

101+
q := uri.Query()
102+
q.Add("mid", mid)
103+
q.Add("uid", uid.String())
104+
uri.RawQuery = q.Encode()
105+
61106
req := http.Request{
62107
Method: http.MethodGet,
63108
URL: uri,

go.mod

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module github.com/columnar-tech/dbc
22

3-
go 1.24.2
3+
go 1.24.4
4+
5+
toolchain go1.24.6
46

57
require (
68
github.com/Masterminds/semver/v3 v3.4.0
@@ -10,9 +12,11 @@ require (
1012
github.com/charmbracelet/bubbletea v1.3.7
1113
github.com/charmbracelet/lipgloss v1.1.0
1214
github.com/goccy/go-yaml v1.17.1
15+
github.com/google/uuid v1.6.0
1316
github.com/pelletier/go-toml/v2 v2.2.4
1417
github.com/stretchr/testify v1.10.0
15-
golang.org/x/sys v0.34.0
18+
github.com/zeroshade/machine-id v0.0.0-20250917170903-4283e98485ba
19+
golang.org/x/sys v0.36.0
1620
gopkg.in/yaml.v3 v3.0.1
1721
)
1822

@@ -27,6 +31,7 @@ require (
2731
github.com/charmbracelet/x/cellbuf v0.0.13 // indirect
2832
github.com/charmbracelet/x/term v0.2.1 // indirect
2933
github.com/cloudflare/circl v1.6.1 // indirect
34+
github.com/columnar-tech/machine-id v0.0.0-20250917165521-f900c2b8afc9 // indirect
3035
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
3136
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
3237
github.com/kr/text v0.2.0 // indirect

go.sum

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQ
3434
github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg=
3535
github.com/cloudflare/circl v1.6.1 h1:zqIqSPIndyBh1bjLVVDHMPpVKqp8Su/V+6MeDzzQBQ0=
3636
github.com/cloudflare/circl v1.6.1/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZh3pJrofs=
37+
github.com/columnar-tech/machine-id v0.0.0-20250917165521-f900c2b8afc9 h1:RwPia8bI7oTl++lBMNT9EGCU2ICb4kN5uVJ9phl9Sak=
38+
github.com/columnar-tech/machine-id v0.0.0-20250917165521-f900c2b8afc9/go.mod h1:REaxLA5IqWLxjhHfVtYguP/4z4eKMZtf3KeYmPf68N0=
3739
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
3840
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3941
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
@@ -42,6 +44,8 @@ github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6
4244
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
4345
github.com/goccy/go-yaml v1.17.1 h1:LI34wktB2xEE3ONG/2Ar54+/HJVBriAGJ55PHls4YuY=
4446
github.com/goccy/go-yaml v1.17.1/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
47+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
48+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
4549
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
4650
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
4751
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
@@ -76,14 +80,16 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
7680
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7781
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
7882
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
83+
github.com/zeroshade/machine-id v0.0.0-20250917170903-4283e98485ba h1:yStvOL+OyJJ41jmZVqHqAspXXr4VVKC3gIfnGvO/+Pw=
84+
github.com/zeroshade/machine-id v0.0.0-20250917170903-4283e98485ba/go.mod h1:RHX47A/DYmoTfyT25mb6C+Eve7X5miOporc+RVUoRoY=
7985
golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8=
8086
golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
8187
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
8288
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
8389
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
8490
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
85-
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
86-
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
91+
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
92+
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
8793
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
8894
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
8995
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

0 commit comments

Comments
 (0)