|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + "github.com/jahvon/tuikit/views" |
| 7 | + "github.com/spf13/cobra" |
| 8 | + |
| 9 | + "github.com/jahvon/flow/internal/context" |
| 10 | + "github.com/jahvon/flow/internal/io" |
| 11 | + "github.com/jahvon/flow/internal/services/store" |
| 12 | +) |
| 13 | + |
| 14 | +func RegisterStoreCmd(ctx *context.Context, rootCmd *cobra.Command) { |
| 15 | + subCmd := &cobra.Command{ |
| 16 | + Use: "store", |
| 17 | + Short: "Manage the data store.", |
| 18 | + Args: cobra.NoArgs, |
| 19 | + } |
| 20 | + registerStoreSetCmd(ctx, subCmd) |
| 21 | + registerStoreGetCmd(ctx, subCmd) |
| 22 | + registerStoreClearCmd(ctx, subCmd) |
| 23 | + rootCmd.AddCommand(subCmd) |
| 24 | +} |
| 25 | + |
| 26 | +func registerStoreSetCmd(ctx *context.Context, rootCmd *cobra.Command) { |
| 27 | + subCmd := &cobra.Command{ |
| 28 | + Use: "set", |
| 29 | + Short: "Set a key-value pair in the data store.", |
| 30 | + Long: dataStoreDescription + "This will overwrite any existing value for the key.", |
| 31 | + Args: cobra.MinimumNArgs(1), |
| 32 | + Run: func(cmd *cobra.Command, args []string) { |
| 33 | + storeSetFunc(ctx, cmd, args) |
| 34 | + }, |
| 35 | + } |
| 36 | + rootCmd.AddCommand(subCmd) |
| 37 | +} |
| 38 | + |
| 39 | +func storeSetFunc(ctx *context.Context, _ *cobra.Command, args []string) { |
| 40 | + key := args[0] |
| 41 | + |
| 42 | + var value string |
| 43 | + switch { |
| 44 | + case len(args) == 1: |
| 45 | + form, err := views.NewForm( |
| 46 | + io.Theme(ctx.Config.Theme.String()), |
| 47 | + ctx.StdIn(), |
| 48 | + ctx.StdOut(), |
| 49 | + &views.FormField{ |
| 50 | + Key: "value", |
| 51 | + Type: views.PromptTypeMultiline, |
| 52 | + Title: "Enter the value to store", |
| 53 | + }) |
| 54 | + if err != nil { |
| 55 | + ctx.Logger.FatalErr(err) |
| 56 | + } |
| 57 | + if err = form.Run(ctx.Ctx); err != nil { |
| 58 | + ctx.Logger.FatalErr(err) |
| 59 | + } |
| 60 | + value = form.FindByKey("value").Value() |
| 61 | + case len(args) == 2: |
| 62 | + value = args[1] |
| 63 | + default: |
| 64 | + ctx.Logger.Warnx("merging multiple arguments into a single value", "count", len(args)) |
| 65 | + value = strings.Join(args[1:], " ") |
| 66 | + } |
| 67 | + |
| 68 | + s, err := store.NewStore() |
| 69 | + if err != nil { |
| 70 | + ctx.Logger.FatalErr(err) |
| 71 | + } |
| 72 | + if err = s.CreateBucket(); err != nil { |
| 73 | + ctx.Logger.FatalErr(err) |
| 74 | + } |
| 75 | + defer func() { |
| 76 | + if err = s.Close(); err != nil { |
| 77 | + ctx.Logger.Error(err, "cleanup failure") |
| 78 | + } |
| 79 | + }() |
| 80 | + if err = s.Set(key, value); err != nil { |
| 81 | + ctx.Logger.FatalErr(err) |
| 82 | + } |
| 83 | + ctx.Logger.Infof("Key %q set in the store", key) |
| 84 | +} |
| 85 | + |
| 86 | +func registerStoreGetCmd(ctx *context.Context, rootCmd *cobra.Command) { |
| 87 | + subCmd := &cobra.Command{ |
| 88 | + Use: "get", |
| 89 | + Aliases: []string{"view"}, |
| 90 | + Short: "Get a value from the data store.", |
| 91 | + Long: dataStoreDescription + "This will retrieve the value for the given key.", |
| 92 | + Args: cobra.ExactArgs(1), |
| 93 | + Run: func(cmd *cobra.Command, args []string) { |
| 94 | + storeGetFunc(ctx, cmd, args) |
| 95 | + }, |
| 96 | + } |
| 97 | + rootCmd.AddCommand(subCmd) |
| 98 | +} |
| 99 | + |
| 100 | +func storeGetFunc(ctx *context.Context, _ *cobra.Command, args []string) { |
| 101 | + key := args[0] |
| 102 | + |
| 103 | + s, err := store.NewStore() |
| 104 | + if err != nil { |
| 105 | + ctx.Logger.FatalErr(err) |
| 106 | + } |
| 107 | + if err = s.CreateBucket(); err != nil { |
| 108 | + ctx.Logger.FatalErr(err) |
| 109 | + } |
| 110 | + defer func() { |
| 111 | + if err := s.Close(); err != nil { |
| 112 | + ctx.Logger.Error(err, "cleanup failure") |
| 113 | + } |
| 114 | + }() |
| 115 | + value, err := s.Get(key) |
| 116 | + if err != nil { |
| 117 | + ctx.Logger.FatalErr(err) |
| 118 | + } |
| 119 | + ctx.Logger.PlainTextSuccess(value) |
| 120 | +} |
| 121 | + |
| 122 | +func registerStoreClearCmd(ctx *context.Context, rootCmd *cobra.Command) { |
| 123 | + subCmd := &cobra.Command{ |
| 124 | + Use: "clear", |
| 125 | + Aliases: []string{"reset"}, |
| 126 | + Short: "Clear the data store.", |
| 127 | + Long: dataStoreDescription + "This will remove all keys and values from the data store.", |
| 128 | + Args: cobra.NoArgs, |
| 129 | + Run: func(cmd *cobra.Command, args []string) { |
| 130 | + storeClearFunc(ctx, cmd, args) |
| 131 | + }, |
| 132 | + } |
| 133 | + rootCmd.AddCommand(subCmd) |
| 134 | +} |
| 135 | + |
| 136 | +func storeClearFunc(ctx *context.Context, _ *cobra.Command, _ []string) { |
| 137 | + s, err := store.NewStore() |
| 138 | + if err != nil { |
| 139 | + ctx.Logger.FatalErr(err) |
| 140 | + } |
| 141 | + if err := s.DeleteBucket(); err != nil { |
| 142 | + ctx.Logger.FatalErr(err) |
| 143 | + } |
| 144 | + ctx.Logger.PlainTextSuccess("Data store cleared") |
| 145 | +} |
| 146 | + |
| 147 | +var dataStoreDescription = "The data store is a key-value store that can be used to persist data across executions. " + |
| 148 | + "Values that are set outside of an executable will persist across all executions until they are cleared. " + |
| 149 | + "When set within an executable, the data will only persist across serial or parallel sub-executables but all " + |
| 150 | + "values will be cleared when the parent executable completes.\n\n" |
0 commit comments