Skip to content

Commit 1a71adf

Browse files
authored
fix: improve how buckets are managed in the store (#196)
1 parent 4ab1e10 commit 1a71adf

File tree

10 files changed

+424
-79
lines changed

10 files changed

+424
-79
lines changed

cmd/internal/exec.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ func execPreRun(_ *context.Context, _ *cobra.Command, _ []string) {
7373
runner.RegisterRunner(parallel.NewRunner())
7474
}
7575

76-
//nolint:funlen
76+
// TODO: refactor this function to simplify the logic
77+
//
78+
//nolint:funlen,gocognit
7779
func execFunc(ctx *context.Context, cmd *cobra.Command, verb executable.Verb, args []string) {
7880
logger := ctx.Logger
7981
if err := verb.Validate(); err != nil {
@@ -111,9 +113,14 @@ func execFunc(ctx *context.Context, cmd *cobra.Command, verb executable.Verb, ar
111113
if err != nil {
112114
logger.FatalErr(err)
113115
}
114-
if err = store.SetProcessBucketID(ref.String(), false); err != nil {
116+
s, err := store.NewStore()
117+
if err != nil {
118+
logger.FatalErr(err)
119+
}
120+
if _, err = s.CreateAndSetBucket(ref.String()); err != nil {
115121
logger.FatalErr(err)
116122
}
123+
_ = s.Close()
117124
if envMap == nil {
118125
envMap = make(map[string]string)
119126
}
@@ -143,7 +150,7 @@ func execFunc(ctx *context.Context, cmd *cobra.Command, verb executable.Verb, ar
143150
logger.Errorf("failed clearing process store\n%v", err)
144151
}
145152
if processStore != nil {
146-
if err = processStore.DeleteBucket(); err != nil {
153+
if err = processStore.DeleteBucket(store.EnvironmentBucket()); err != nil {
147154
logger.Errorf("failed clearing process store\n%v", err)
148155
}
149156
_ = processStore.Close()

cmd/internal/flags/types.go

+6
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,9 @@ var SetSoundNotificationFlag = &Metadata{
169169
Usage: "Update completion sound notification setting",
170170
Default: false,
171171
}
172+
173+
var StoreFullFlag = &Metadata{
174+
Name: "full",
175+
Usage: "Force clear all stored data",
176+
Default: false,
177+
}

cmd/internal/store.go

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package internal
22

33
import (
4+
"fmt"
45
"strings"
56

67
"github.com/jahvon/tuikit/views"
78
"github.com/spf13/cobra"
89

10+
"github.com/jahvon/flow/cmd/internal/flags"
911
"github.com/jahvon/flow/internal/context"
1012
"github.com/jahvon/flow/internal/io"
1113
"github.com/jahvon/flow/internal/services/store"
@@ -25,7 +27,7 @@ func RegisterStoreCmd(ctx *context.Context, rootCmd *cobra.Command) {
2527

2628
func registerStoreSetCmd(ctx *context.Context, rootCmd *cobra.Command) {
2729
subCmd := &cobra.Command{
28-
Use: "set",
30+
Use: "set KEY [VALUE]",
2931
Short: "Set a key-value pair in the data store.",
3032
Long: dataStoreDescription + "This will overwrite any existing value for the key.",
3133
Args: cobra.MinimumNArgs(1),
@@ -61,15 +63,15 @@ func storeSetFunc(ctx *context.Context, _ *cobra.Command, args []string) {
6163
case len(args) == 2:
6264
value = args[1]
6365
default:
64-
ctx.Logger.Warnx("merging multiple arguments into a single value", "count", len(args))
66+
ctx.Logger.PlainTextWarn(fmt.Sprintf("merging multiple (%d) arguments into a single value", len(args)-1))
6567
value = strings.Join(args[1:], " ")
6668
}
6769

6870
s, err := store.NewStore()
6971
if err != nil {
7072
ctx.Logger.FatalErr(err)
7173
}
72-
if err = s.CreateBucket(); err != nil {
74+
if err = s.CreateBucket(store.EnvironmentBucket()); err != nil {
7375
ctx.Logger.FatalErr(err)
7476
}
7577
defer func() {
@@ -80,12 +82,12 @@ func storeSetFunc(ctx *context.Context, _ *cobra.Command, args []string) {
8082
if err = s.Set(key, value); err != nil {
8183
ctx.Logger.FatalErr(err)
8284
}
83-
ctx.Logger.Infof("Key %q set in the store", key)
85+
ctx.Logger.PlainTextInfo(fmt.Sprintf("Key %q set in the store", key))
8486
}
8587

8688
func registerStoreGetCmd(ctx *context.Context, rootCmd *cobra.Command) {
8789
subCmd := &cobra.Command{
88-
Use: "get",
90+
Use: "get KEY",
8991
Aliases: []string{"view"},
9092
Short: "Get a value from the data store.",
9193
Long: dataStoreDescription + "This will retrieve the value for the given key.",
@@ -104,7 +106,7 @@ func storeGetFunc(ctx *context.Context, _ *cobra.Command, args []string) {
104106
if err != nil {
105107
ctx.Logger.FatalErr(err)
106108
}
107-
if err = s.CreateBucket(); err != nil {
109+
if _, err = s.CreateAndSetBucket(store.EnvironmentBucket()); err != nil {
108110
ctx.Logger.FatalErr(err)
109111
}
110112
defer func() {
@@ -130,15 +132,29 @@ func registerStoreClearCmd(ctx *context.Context, rootCmd *cobra.Command) {
130132
storeClearFunc(ctx, cmd, args)
131133
},
132134
}
135+
RegisterFlag(ctx, subCmd, *flags.StoreFullFlag)
133136
rootCmd.AddCommand(subCmd)
134137
}
135138

136-
func storeClearFunc(ctx *context.Context, _ *cobra.Command, _ []string) {
139+
func storeClearFunc(ctx *context.Context, cmd *cobra.Command, _ []string) {
140+
full := flags.ValueFor[bool](ctx, cmd, *flags.StoreFullFlag, false)
141+
if full {
142+
if err := store.DestroyStore(); err != nil {
143+
ctx.Logger.FatalErr(err)
144+
}
145+
ctx.Logger.PlainTextSuccess("Data store cleared")
146+
return
147+
}
137148
s, err := store.NewStore()
138149
if err != nil {
139150
ctx.Logger.FatalErr(err)
140151
}
141-
if err := s.DeleteBucket(); err != nil {
152+
defer func() {
153+
if err := s.Close(); err != nil {
154+
ctx.Logger.Error(err, "cleanup failure")
155+
}
156+
}()
157+
if err := s.DeleteBucket(store.EnvironmentBucket()); err != nil {
142158
ctx.Logger.FatalErr(err)
143159
}
144160
ctx.Logger.PlainTextSuccess("Data store cleared")

docs/cli/flow_store_clear.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ flow store clear [flags]
1515
### Options
1616

1717
```
18+
--full Force clear all stored data
1819
-h, --help help for clear
1920
```
2021

docs/cli/flow_store_get.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The data store is a key-value store that can be used to persist data across exec
99
This will retrieve the value for the given key.
1010

1111
```
12-
flow store get [flags]
12+
flow store get KEY [flags]
1313
```
1414

1515
### Options

docs/cli/flow_store_set.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The data store is a key-value store that can be used to persist data across exec
99
This will overwrite any existing value for the key.
1010

1111
```
12-
flow store set [flags]
12+
flow store set KEY [VALUE] [flags]
1313
```
1414

1515
### Options

examples/wip.flow

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# yaml-language-server: $schema=https://flowexec.io/schemas/flowfile_schema.json
2+
visibility: internal
3+
namespace: wip
4+
description: Work-in-progress executables. These may include configurations that are not yet released.
5+
executables:
6+
- verb: run
7+
name: stateful
8+
serial:
9+
params:
10+
- envKey: VAL1
11+
text: value
12+
- envKey: FLOW
13+
text: ../.bin/flow
14+
execs:
15+
- cmd: |
16+
$FLOW store set wip-test1 hello
17+
echo "$VAL1" | $FLOW store set wip-test2
18+
$FLOW store set wip-test3 to be merged
19+
- cmd: |
20+
echo "wip-test1: $($FLOW store get wip-test1)"
21+
echo "wip-test2: $($FLOW store get wip-test2)"
22+
echo "wip-test3: $($FLOW store get wip-test3)"

internal/services/store/mocks/mock_store.go

+169
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)