-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcreate_mkdir_function.rsc
70 lines (57 loc) · 2.39 KB
/
create_mkdir_function.rsc
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
# Written by Phillip Stromberg in 2019
# Distributed under the MIT license
#
#
# this creates a function called $mkdir
# from the command line you will be able to just type "$mkdir some/new/folder"
# and all the folders that need to be created will be
# if the folder already existed, it is ignored
:log info "Creating \$mkdir function..."
:global mkdir do={
# $1 refers to the input variable the user passed in to us
:local newFolder $1;
# we'll use this variable to report error messages that may arise
:local errorMsg;
# if the user put a slash on the end of the new folder name, remove it
# i.e. "some/test/" becomes "some/test"
:while (([:pick $newFolder ([:len $newFolder] - 1)]) = "/") do={
:set newFolder [:pick $newFolder 0 ([:len $newFolder] - 1)]
}
# if the user put a slash at the beginning of the new folder name, remove it
# i.e. "/some/test" becomes "some/test"
:while ([:pick $newFolder 0] = "/") do={
:set newFolder [:pick $newFolder 1 [:len $newFolder]]
}
:if ([/file find name=$newFolder] != "") do={
:set errorMsg "'$newFolder' already exists.";
:log debug "mkdir: $errorMsg";
:return $newFolder;
}
# the name of the temp file to create
:local tempfile "mkdir_temp_file.txt";
# port 0 is unlikely to be open to HTTP requests...
:local fakeURL "http://127.0.0.1:0/should-not-exist.txt";
:local fullTempPath ($newFolder . "/" . $tempfile);
# this is where the folder creation happens
# `as-value` prevents status messages from printing
# wrapping in a `:do {...} on-error` prevents error messages from printing.
:do {
/tool fetch dst-path="$fullTempPath" url="$fakeURL" duration=0.001s as-value;
} on-error={}
# this waits for the temporary dir to show up so there isn't a race
# condition where you call mkdir and then try to do something before the
# folder is actually created
:local count 0;
:while ([/file find name="$newFolder"] = "") do={
:if ($count >= 50) do={
# after 50 * 0.1s delays, it's been 5 seconds, which should be long enough
:set errorMsg "New folder could not be created at $newFolder";
:log error "mkdir: $errorMsg"
:error $errorMsg
}
:delay 0.1s;
:set count ($count + 1);
}
:return $newFolder;
}
:log info "Created function \$mkdir"