2323# Example:
2424# ls ~ | kv set "home snapshot"
2525# kv set foo 5
26- export def "kv set" [key : string , value ?: any ] {
26+ export def "kv set" [
27+ key : string
28+ value_or_closure ?: any
29+ -- return (- r ): string # Whether and what to return to the pipeline output
30+ ] {
2731 # Pipeline input is preferred, but prioritize
2832 # parameter if present. This allows $in to be
2933 # used in the parameter if needed.
3034 let input = $in
31- let value = $value | default $input
35+
36+ let arg_type = ($value_or_closure | describe )
37+ let value = match $arg_type {
38+ closure => { $input | do $value_or_closure }
39+ _ => ($value_or_closure | default $input )
40+ }
3241
3342 # Store values as nuons for type-integrity
3443 let kv_pair = {
@@ -38,31 +47,49 @@ export def "kv set" [key: string, value?: any] {
3847
3948 # Create the table if it doesn't exist
4049 try {
41- stor create - t kv_mod_store - c {key : str , value: str } | ignore
50+ stor create - t std_kv_store - c {key : str , value: str } | ignore
4251 }
4352
4453 # Does the key exist yet?
45- let key_exists = ( $key in (stor open | $in.kv_mod_store ?.key? ))
54+ let key_exists = ( $key in (stor open | $in.std_kv_store ?.key? ))
4655
4756 # If so, we need an update rather than an insert
4857 let stor_action = match $key_exists {
49- true => {{stor update - t kv_mod_store -- where-clause $" key = '($key )'" }}
50- false => {{stor insert - t kv_mod_store }}
58+ true => {{stor update - t std_kv_store -- where-clause $" key = '($key )'" }}
59+ false => {{stor insert - t std_kv_store }}
5160 }
5261
5362 # Execute the update-or-insert action
5463 $kv_pair | do $stor_action
5564
5665 # Returns the kv table itself in case it's
5766 # useful in the pipeline
58- kv list
67+ match ($return | default ' value' ) {
68+ ' all' => (kv list )
69+ ' a' => (kv list )
70+ ' value' => $value
71+ ' v' => $value
72+ ' input' => $input
73+ ' in' => $input
74+ ' ignore' => null
75+ ' i' => null
76+ _ => {
77+ error make {
78+ msg : " Invalid --return option"
79+ label : {
80+ text : " Must be 'all'/'a', 'value'/'v', 'input'/'in', or 'ignore'/'i'"
81+ span : (metadata $return ).span
82+ }
83+ }
84+ }
85+ }
5986}
6087
6188def kv_key_completions [] {
6289 try {
6390 stor open
6491 # Hack to turn a SQLiteDatabase into a table
65- | $in.kv_mod_store | wrap temp | get temp
92+ | $in.std_kv_store | wrap temp | get temp
6693 | get key?
6794 } catch {
6895 # Return no completions
@@ -82,12 +109,12 @@ export def "kv get" [
82109 key : string @kv_key_completions # Key of the kv-pair to retrieve
83110] {
84111 try {
85- stor create - t kv_mod_store - c {key : str , value: str } | ignore
112+ stor create - t std_kv_store - c {key : str , value: str } | ignore
86113 }
87114
88115 stor open
89116 # Hack to turn a SQLiteDatabase into a table
90- | $in.kv_mod_store | wrap temp | get temp
117+ | $in.std_kv_store | wrap temp | get temp
91118 | where key == $key
92119 # Should only be one occurence of each key in the stor
93120 | get - i value | first
@@ -106,10 +133,10 @@ export def "kv get" [
106133export def "kv list" [] {
107134 # Create the table if it doesn't exist
108135 try {
109- stor create - t kv_mod_store - c {key : str , value: str } | ignore
136+ stor create - t std_kv_store - c {key : str , value: str } | ignore
110137 }
111138
112- stor open | $in.kv_mod_store | each {|kv_pair |
139+ stor open | $in.std_kv_store | each {|kv_pair |
113140 {
114141 key : $kv_pair.key
115142 value : ($kv_pair.value | from nuon )
@@ -123,15 +150,15 @@ export def "kv drop" [
123150] {
124151 # Create the table if it doesn't exist
125152 try {
126- stor create - t kv_mod_store - c {key : str , value: str } | ignore
153+ stor create - t std_kv_store - c {key : str , value: str } | ignore
127154 }
128155
129156 try {
130157 stor open
131158 # Hack to turn a SQLiteDatabase into a table
132- | $in.kv_mod_store | wrap temp | get temp
159+ | $in.std_kv_store | wrap temp | get temp
133160 | where key == $key
134- # Should only be one occurence of each key in the stor
161+ # Should only be one occurrence of each key in the stor
135162 | get - i value | first
136163 | match $in {
137164 # Key not found
@@ -140,7 +167,7 @@ export def "kv drop" [
140167 # Key found
141168 _ => {
142169 let value = $in
143- stor delete -- table-name kv_mod_store -- where-clause $" key = '($key )'"
170+ stor delete -- table-name std_kv_store -- where-clause $" key = '($key )'"
144171 $value | from nuon
145172 }
146173 }
0 commit comments