Skip to content

Commit 6c1cc16

Browse files
committed
wip
1 parent 9a80e18 commit 6c1cc16

File tree

6 files changed

+97
-141
lines changed

6 files changed

+97
-141
lines changed

api/store/pg/entity/namespace.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ func NamespaceFromModel(model *models.Namespace) *Namespace {
2929
namespace := &Namespace{
3030
ID: model.TenantID,
3131
CreatedAt: model.CreatedAt,
32-
UpdatedAt: model.UpdatedAt,
3332
Type: string(model.Type),
3433
Name: model.Name,
3534
Settings: NamespaceSettings{
@@ -45,7 +44,6 @@ func NamespaceFromModel(model *models.Namespace) *Namespace {
4544
UserID: member.ID,
4645
NamespaceID: model.TenantID,
4746
CreatedAt: member.AddedAt,
48-
UpdatedAt: member.UpdatedAt,
4947
Status: string(member.Status),
5048
Role: string(member.Role),
5149
}

api/store/pg/entity/tag.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ type Tag struct {
1919
Namespace *Namespace `bun:"rel:belongs-to,join:namespace_id=id"`
2020
}
2121

22+
type DeviceTag struct {
23+
bun.BaseModel `bun:"table:device_tags"`
24+
DeviceID string `bun:"device_id,pk"`
25+
TagID string `bun:"tag_id,pk"`
26+
CreatedAt time.Time `bun:"created_at"`
27+
}
28+
29+
type PublicKeyTag struct {
30+
bun.BaseModel `bun:"table:public_key_tags"`
31+
PublicKeyID string `bun:"public_key_id,pk"`
32+
TagID string `bun:"tag_id,pk"`
33+
CreatedAt time.Time `bun:"created_at"`
34+
}
35+
2236
func TagFromModel(model *models.Tag) *Tag {
2337
return &Tag{
2438
ID: model.ID,
@@ -38,3 +52,11 @@ func TagToModel(entity *Tag) *models.Tag {
3852
UpdatedAt: entity.UpdatedAt,
3953
}
4054
}
55+
56+
func NewDeviceTag(tagID, deviceID string) *DeviceTag {
57+
return &DeviceTag{TagID: tagID, DeviceID: tagID}
58+
}
59+
60+
func NewPublicKeyTag(tagID, publickeyID string) *PublicKeyTag {
61+
return &PublicKeyTag{TagID: tagID, PublicKeyID: tagID}
62+
}

api/store/pg/tags.go

Lines changed: 26 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -121,128 +121,64 @@ func (pg *Pg) TagUpdate(ctx context.Context, id string, changes *models.TagChang
121121
}
122122

123123
func (pg *Pg) TagPushToTarget(ctx context.Context, id string, target store.TagTarget, targetID string) error {
124-
// First verify the tag exists
125124
tag := new(entity.Tag)
126-
err := pg.driver.NewSelect().
127-
Model(tag).
128-
Where("id = ?", id).
129-
Scan(ctx)
130-
if err != nil {
125+
if err := pg.driver.NewSelect().Model(tag).Where("id = ?", id).Scan(ctx); err != nil {
131126
return fromSqlError(err)
132127
}
133128

134-
var res bun.Result
135129
switch target {
136130
case store.TagTargetDevice:
137-
res, err = pg.driver.NewInsert().
138-
Model(&struct {
139-
bun.BaseModel `bun:"table:device_tags"`
140-
DeviceID string `bun:"device_id,pk"`
141-
TagID string `bun:"tag_id,pk"`
142-
CreatedAt any `bun:"created_at"`
143-
}{
144-
DeviceID: targetID,
145-
TagID: id,
146-
CreatedAt: bun.Ident("NOW()"),
147-
}).
148-
On("CONFLICT (device_id, tag_id) DO NOTHING").
149-
Exec(ctx)
150-
case store.TagTargetPublicKey:
151-
res, err = pg.driver.NewInsert().
152-
Model(&struct {
153-
bun.BaseModel `bun:"table:public_key_tags"`
154-
PublicKeyID string `bun:"public_key_id,pk"`
155-
TagID string `bun:"tag_id,pk"`
156-
CreatedAt any `bun:"created_at"`
157-
}{
158-
PublicKeyID: targetID,
159-
TagID: id,
160-
CreatedAt: bun.Ident("NOW()"),
161-
}).
162-
On("CONFLICT (public_key_id, tag_id) DO NOTHING").
163-
Exec(ctx)
164-
default:
165-
return store.ErrInvalidTagTarget
166-
}
131+
deviceTag := entity.NewDeviceTag(tag.ID, targetID)
132+
deviceTag.CreatedAt = clock.Now()
167133

168-
if err != nil {
169-
return fromSqlError(err)
170-
}
134+
if _, err := pg.driver.NewInsert().Model(deviceTag).On("CONFLICT (device_id, tag_id) DO NOTHING").Exec(ctx); err != nil {
135+
return fromSqlError(err)
136+
}
137+
case store.TagTargetPublicKey:
138+
publickeyTag := entity.NewPublicKeyTag(tag.ID, targetID)
139+
publickeyTag.CreatedAt = clock.Now()
171140

172-
// Check if the target exists by verifying we could insert/update
173-
if rows, _ := res.RowsAffected(); rows == 0 {
174-
// Could be because relationship already exists or target doesn't exist
175-
// We need to check if target exists
176-
switch target {
177-
case store.TagTargetDevice:
178-
count, err := pg.driver.NewSelect().
179-
Model((*entity.Device)(nil)).
180-
Where("id = ?", targetID).
181-
Count(ctx)
182-
if err != nil {
183-
return fromSqlError(err)
184-
}
185-
if count == 0 {
186-
return store.ErrNoDocuments
187-
}
188-
case store.TagTargetPublicKey:
189-
count, err := pg.driver.NewSelect().
190-
Model((*entity.PublicKey)(nil)).
191-
Where("id = ?", targetID).
192-
Count(ctx)
193-
if err != nil {
194-
return fromSqlError(err)
195-
}
196-
if count == 0 {
197-
return store.ErrNoDocuments
198-
}
141+
if _, err := pg.driver.NewInsert().Model(publickeyTag).On("CONFLICT (public_key_id, tag_id) DO NOTHING").Exec(ctx); err != nil {
142+
return fromSqlError(err)
199143
}
200144
}
201145

202146
return nil
203147
}
204148

205149
func (pg *Pg) TagPullFromTarget(ctx context.Context, id string, target store.TagTarget, targetIDs ...string) error {
206-
// First verify the tag exists
207150
tag := new(entity.Tag)
208-
err := pg.driver.NewSelect().
209-
Model(tag).
210-
Where("id = ?", id).
211-
Scan(ctx)
212-
if err != nil {
151+
if err := pg.driver.NewSelect().Model(tag).Where("id = ?", id).Scan(ctx); err != nil {
213152
return fromSqlError(err)
214153
}
215154

216-
var res bun.Result
217155
switch target {
218156
case store.TagTargetDevice:
219-
query := pg.driver.NewDelete().
220-
Model((*struct {
221-
bun.BaseModel `bun:"table:device_tags"`
222-
})(nil)).
223-
Where("tag_id = ?", id)
224-
157+
query := pg.driver.NewDelete().Model((*entity.DeviceTag)(nil)).Where("tag_id = ?", id)
225158
if len(targetIDs) > 0 {
226159
query = query.Where("device_id IN (?)", bun.In(targetIDs))
227160
}
228161

229-
res, err = query.Exec(ctx)
162+
if _, err := query.Exec(ctx); err != nil {
163+
return fromSqlError(err)
164+
}
230165
case store.TagTargetPublicKey:
231-
query := pg.driver.NewDelete().
232-
Model((*struct {
233-
bun.BaseModel `bun:"table:public_key_tags"`
234-
})(nil)).
235-
Where("tag_id = ?", id)
236-
166+
query := pg.driver.NewDelete().Model((*entity.PublicKeyTag)(nil)).Where("tag_id = ?", id)
237167
if len(targetIDs) > 0 {
238168
query = query.Where("public_key_id IN (?)", bun.In(targetIDs))
239169
}
240170

241-
res, err = query.Exec(ctx)
242-
default:
243-
return store.ErrInvalidTagTarget
171+
if _, err := query.Exec(ctx); err != nil {
172+
return fromSqlError(err)
173+
}
244174
}
245175

176+
return nil
177+
}
178+
179+
func (pg *Pg) TagDelete(ctx context.Context, id string) error {
180+
// Cascade will delete the relationships
181+
res, err := pg.driver.NewDelete().Model((*entity.Tag)(nil)).Where("id = ?", id).Exec(ctx)
246182
if err != nil {
247183
return fromSqlError(err)
248184
}
@@ -253,43 +189,3 @@ func (pg *Pg) TagPullFromTarget(ctx context.Context, id string, target store.Tag
253189

254190
return nil
255191
}
256-
257-
func (pg *Pg) TagDelete(ctx context.Context, id string) error {
258-
return pg.driver.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
259-
// Delete the tag itself
260-
res, err := tx.NewDelete().
261-
Model((*entity.Tag)(nil)).
262-
Where("id = ?", id).
263-
Exec(ctx)
264-
if err != nil {
265-
return fromSqlError(err)
266-
}
267-
268-
if rows, _ := res.RowsAffected(); rows == 0 {
269-
return store.ErrNoDocuments
270-
}
271-
272-
// Delete all relationships (CASCADE should handle this, but being explicit)
273-
_, err = tx.NewDelete().
274-
Model((*struct {
275-
bun.BaseModel `bun:"table:device_tags"`
276-
})(nil)).
277-
Where("tag_id = ?", id).
278-
Exec(ctx)
279-
if err != nil {
280-
return fromSqlError(err)
281-
}
282-
283-
_, err = tx.NewDelete().
284-
Model((*struct {
285-
bun.BaseModel `bun:"table:public_key_tags"`
286-
})(nil)).
287-
Where("tag_id = ?", id).
288-
Exec(ctx)
289-
if err != nil {
290-
return fromSqlError(err)
291-
}
292-
293-
return nil
294-
})
295-
}

cli/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# base stage
2-
FROM golang:1.23-alpine3.19 AS base
2+
FROM golang:1.24-alpine3.22 AS base
33

44
RUN apk add --no-cache git ca-certificates
55

cli/go.mod

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module github.com/shellhub-io/shellhub/cli
22

3-
go 1.23.0
3+
go 1.24.0
44

5-
toolchain go1.23.9
5+
toolchain go1.24.6
66

77
require (
88
github.com/shellhub-io/shellhub v0.13.4
@@ -33,6 +33,11 @@ require (
3333
github.com/hashicorp/errwrap v1.1.0 // indirect
3434
github.com/hashicorp/go-multierror v1.1.1 // indirect
3535
github.com/inconshreveable/mousetrap v1.1.0 // indirect
36+
github.com/jackc/pgpassfile v1.0.0 // indirect
37+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
38+
github.com/jackc/pgx/v5 v5.7.6 // indirect
39+
github.com/jackc/puddle/v2 v2.2.2 // indirect
40+
github.com/jinzhu/inflection v1.0.0 // indirect
3641
github.com/klauspost/compress v1.18.0 // indirect
3742
github.com/klauspost/pgzip v1.2.5 // indirect
3843
github.com/labstack/echo/v4 v4.13.4 // indirect
@@ -43,21 +48,27 @@ require (
4348
github.com/mholt/archiver/v4 v4.0.0-alpha.8 // indirect
4449
github.com/montanaflynn/stats v0.7.1 // indirect
4550
github.com/nwaples/rardecode/v2 v2.0.0-beta.2 // indirect
51+
github.com/oiime/logrusbun v0.1.2-0.20241011112815-4df3a0fb0e11 // indirect
4652
github.com/oschwald/geoip2-golang v1.8.0 // indirect
4753
github.com/oschwald/maxminddb-golang v1.10.0 // indirect
4854
github.com/pierrec/lz4/v4 v4.1.17 // indirect
4955
github.com/pkg/errors v0.9.1 // indirect
5056
github.com/pmezard/go-difflib v1.0.0 // indirect
57+
github.com/puzpuzpuz/xsync/v3 v3.5.1 // indirect
5158
github.com/sethvargo/go-envconfig v0.9.0 // indirect
5259
github.com/spf13/pflag v1.0.9 // indirect
5360
github.com/square/mongo-lock v0.0.0-20230808145049-cfcf499f6bf0 // indirect
5461
github.com/stretchr/objx v0.5.2 // indirect
5562
github.com/therootcompany/xz v1.0.1 // indirect
63+
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
5664
github.com/ulikunitz/xz v0.5.14 // indirect
65+
github.com/uptrace/bun v1.2.15 // indirect
66+
github.com/uptrace/bun/dbfixture v1.2.15 // indirect
67+
github.com/uptrace/bun/dialect/pgdialect v1.2.15 // indirect
5768
github.com/valyala/bytebufferpool v1.0.0 // indirect
5869
github.com/valyala/fasttemplate v1.2.2 // indirect
5970
github.com/vmihailenco/go-tinylfu v0.2.2 // indirect
60-
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
71+
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
6172
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
6273
github.com/xakep666/mongo-migrate v0.3.2 // indirect
6374
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
@@ -69,7 +80,7 @@ require (
6980
golang.org/x/crypto v0.41.0 // indirect
7081
golang.org/x/net v0.42.0 // indirect
7182
golang.org/x/sync v0.16.0 // indirect
72-
golang.org/x/sys v0.35.0 // indirect
83+
golang.org/x/sys v0.36.0 // indirect
7384
golang.org/x/text v0.28.0 // indirect
7485
gopkg.in/yaml.v3 v3.0.1 // indirect
7586
)

0 commit comments

Comments
 (0)