forked from cyber-prophet/cy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cy-internals.nu
204 lines (172 loc) · 5.51 KB
/
cy-internals.nu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use nu-utils [ bar, cprint, "str repeat", to-safe-filename, to-number-format, number-col-format,
nearest-given-weekday, print-and-pass, clip, confirm, normalize, path-modify]
export def 'cy-path' [
...folders: string # folders to add to cy path
--create_missing # if the resulted path doesn't exist - create it
--file: string # a filename to use as a last segment of the path
]: nothing -> path {
$env | get -i cy.path
| default ($nu.home-path | path join 'cy')
| append $folders
| path join
| path expand
| if $create_missing {
if not ($in | path exists) {
let $input = $in
mkdir $input
$input
} else {}
} else {}
| if $file != null {
path join $file
} else {}
}
export def open-cy-config-toml []: nothing -> record {
let $config = $nu.home-path
| path join .cy_config.toml
| if ($in | path exists) { open } else { {} }
default-settings
| merge $config
| default (cy-path) path
| default (cy-path graph particles safe) ipfs-files-folder
| default no-config-set config-name
}
export def default-settings []: nothing -> record {
open (cy-path kickstart settings-variants.yaml)
| items {|k v|
$v.variants.0 # the first variant in the list is the default one
| if $in == other { {} } else {
match-type $v.type?
| wrap $k
}
}
| reduce -f {} {|i acc| $acc | merge $i}
}
export def match-type [
$type?
]: any -> any {
let $def_value = $in
match $type {
'int' => {$def_value | into int}
'datetime' => {$def_value | into datetime}
'duration' => {$def_value | into duration}
'bool' => {$def_value | into bool}
_ => {$def_value | into string}
}
}
export def export1 --env [] {
# if $nu.is-interactive {
# let $tested_versions = ['0.92.1' '0.92.2' '0.92.3']
# version
# | get version
# | if $in not-in $tested_versions {
# cprint $'This version of Cy was tested on ($tested_versions), and you have ($in).
# We suggest you to use one of the tested versions. If you installed *nushell*
# using brew, you can update it with the command *brew upgrade nushell*'
# }
# }
let $config = open-cy-config-toml
let $user_config = $config.path
| path join config $'($config.config-name).toml'
| if ($in | path exists) { open } else {
cprint $'A config file was not found. Run *cy config-new*'
{}
}
make-default-folders-fn
$env.cy = ($config | merge $user_config)
}
export def 'param-or-input' [
param?
] {
let $input = $in
$param | default $input
}
export def 'backup-and-echo' [
filename?: path
--quiet # don't echo the file-path back
--mv # move the file to backup directory instead of copy
] {
let $path = param-or-input $filename
let $backups_path = cy-path backups $'(now-fn)($path | path basename)'
if not ( $path | path exists ) {
cprint $'*($path)* does not exist'
return $path
}
if $mv {
mv $path $backups_path
} else {
cp $path $backups_path
}
if not $quiet { $path }
}
export def 'set-select-from-variants' [
$key
] {
let $key_record = open (cy-path kickstart settings-variants.yaml) | get -i $key
if $key_record == null {
input 'type your setting: '
} else {
cprint -h green $'*($key): ($key_record.description?)*'
$key_record
| get variants
| input list
| if ($in == 'other') {
input 'type your setting: '
} else {}
| print-and-pass
}
}
# Sets, retrieves, or defaults an environment variable based on input, without setting it if specified.
export def --env 'set-or-get-env-or-def' [
key
value?
--dont_set_env
] {
let $val_ref = $value
| if $in != null {} else {
$env.cy | get -i $key
}
| if $in != null {} else {
let $key_record = (
open (cy-path kickstart settings-variants.yaml)
| get -i $key
)
$key_record
| get -i variants.0
| match-type $key_record.type?
}
if not $dont_set_env {
$env.cy = ($env.cy | upsert $key $val_ref)
}
$val_ref
}
export def make-default-folders-fn []: nothing -> nothing {
cy-path --create_missing backups
cy-path --create_missing cache cli_out
cy-path --create_missing cache jsonl
cy-path --create_missing cache queue_cids_dead
cy-path --create_missing cache queue_cids_to_download
cy-path --create_missing cache queue_tasks_failed
cy-path --create_missing cache queue_tasks_to_run
cy-path --create_missing cache search
cy-path --create_missing config
cy-path --create_missing export
cy-path --create_missing graph particles safe
cy-path --create_missing mylinks
cy-path --create_missing temp ipfs_upload
cy-path --create_missing temp transactions
touch (cy-path graph update.toml)
if not (current-links-csv-path | path exists) {
'from,to' | save (current-links-csv-path)
}
if not (cy-path mylinks _cyberlinks_archive.csv | path exists) {
'from,to,address,timestamp,txhash'
# underscore is supposed to place the file first in the folder
| save (cy-path mylinks _cyberlinks_archive.csv)
}
}
export def 'path-exists-safe' [
path_to_check
] {
try {$path_to_check | path exists} catch {false}
}