Skip to content

Commit e9d4da8

Browse files
committed
feat: add file locking on go-cat add command
1 parent e6d4759 commit e9d4da8

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/go-git/go-billy/v5 v5.3.1
99
github.com/go-git/go-git/v5 v5.4.2
1010
github.com/hashicorp/terraform-plugin-sdk/v2 v2.13.0
11+
github.com/juju/fslock v0.0.0-20160525022230-4d5c94c67b4b
1112
github.com/urfave/cli/v2 v2.4.0
1213
github.com/withmandala/go-log v0.1.0
1314
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c
152152
github.com/jhump/protoreflect v1.6.0/go.mod h1:eaTn3RZAmMBcV0fifFvlm6VHNz3wSkYyXYWUh7ymB74=
153153
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
154154
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
155+
github.com/juju/fslock v0.0.0-20160525022230-4d5c94c67b4b h1:FQ7+9fxhyp82ks9vAuyPzG0/vVbWwMwLJ+P6yJI5FN8=
156+
github.com/juju/fslock v0.0.0-20160525022230-4d5c94c67b4b/go.mod h1:HMcgvsgd0Fjj4XXDkbjdmlbI505rUPBs6WBMYg2pXks=
155157
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
156158
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
157159
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=

ops/add.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@ package ops
22

33
import (
44
"encoding/json"
5+
"github.com/juju/fslock"
56
"gitlab.com/sorcero/community/go-cat/infrastructure"
67
"gitlab.com/sorcero/community/go-cat/internal/helpers"
78
"gitlab.com/sorcero/community/go-cat/meta"
8-
"io/ioutil"
99
"os"
10+
"time"
1011
)
1112

1213
func AddWithDbName(infra *infrastructure.Metadata, queueDb string) error {
1314
infraMeta := &infrastructure.MetadataGroup{}
15+
lock := fslock.New(queueDb + ".lock")
16+
err := lock.LockWithTimeout(time.Hour * 1)
17+
if err != nil {
18+
panic(err)
19+
}
1420
if helpers.CheckFileExists(queueDb) {
15-
data, err := ioutil.ReadFile(queueDb)
21+
data, err := os.ReadFile(queueDb)
1622
if err != nil {
1723
return err
1824
}
@@ -21,7 +27,7 @@ func AddWithDbName(infra *infrastructure.Metadata, queueDb string) error {
2127
return err
2228
}
2329
}
24-
infraMeta, err := infraMeta.Add(infra)
30+
infraMeta, err = infraMeta.Add(infra)
2531
if err != nil {
2632
return err
2733
}
@@ -33,6 +39,10 @@ func AddWithDbName(infra *infrastructure.Metadata, queueDb string) error {
3339
if err != nil {
3440
return err
3541
}
42+
err = lock.Unlock()
43+
if err != nil {
44+
panic(err)
45+
}
3646

3747
return nil
3848
}

0 commit comments

Comments
 (0)