Code: Select all
#-----------------------------------------------------------------------------------
# Function (Functions & Scripts Auto Setup) (FaSaS)
# by Sertik 13/03/2023
#-----------------------------------------------------------------------------------
# variants $1:
# $1 = txt, rsc, fnc, run
# Examples usage:
# [$FuncFaSAs txt file1 script1] - creates a script named script1 ($3) from a text file file1 ($2)
# [$FuncFaSas rsc script2.rsc] - imports a script with the specified name ($2)
# [$FuncFaSas rsc script3.rsc "http://urlpatch"] - loads from a remote resource $3 and imports the script, gives it the name $2
# [$FuncFaSas fnc myFunc script4] - creates a function named $2 from a script named $3, checking its presence in the repository
# [$FuncFaSas run script5] - executes the $2 script, checking its presence in the repository
# the function logs the results of its work and returns the corresponding errors
:global FuncFaSaS do={
:local isp false
:local ferrans "ERROR function $0 type $1:"
:if ($1="txt") do={:set isp true
# add scriptin repository from file (.txt)
:do {
# /system script set name=$3 source=[/file get [find where name=$2] contents]
:if ([:len [/system script find name=$3]]!=0) do={[/system script remove $3]}
/system script add name=$3 source=[/file get [find where name=$2] contents]
:log warning ("$0: Add $3 script from file $2 is done")
} on-error={:log error ("$ferrans add script < $3 > from file < $2 >")
:return ("$ferrans error add script $3 from file $2")}
}
:if ($1="rsc") do={:set isp true
# import or download & import script library (.rsc)
:if ([:len $3]=0) do={
:do {
:import $2;
:log warning "$0: import < $2 > script is done"
} on-error={:log error ("$ferrans error import script file < $2 >")
:return ("$ferrans error import script file $2")}
} else={
:do {
/tool fetch url=$3
:log warning "$0: download < $2 > from < $3 > is done"
:import $2
:log warning "$0 import < $2 > script is done";
} on-error={:log error ("$ferrans could not load or import script file < $2 > from < $3 >")
:return ("$ferrans could not load or import script file < $2 > from < $3 >")}
}
}
:if ($1="fnc") do={:set isp true
# installed function from script
if ([:len [/system script find name $3]]=0) do={
:log error ("$ferrans function < $2 > not define but script < $3 > not found")
:return "$ferrans script < $3 > not found"}
:do {
:exec script=":global \"$2\" [:parse [/system script get $3 source]]";
} on-error={:log error ("$ferrans declare function < $2 > or get source script < $3 >");
:return ("$ferrans declare function < $2 > or get source script < $3 >")}
:log warning ("Function $0: defined function < $2 > from script < $3>")
}
:if ($1="run") do={:set isp true
# run script in repository
if ([:len [/system script find name $2]]=0) do={
:log error ("$ferrans script < $2 > not found")
:return "$ferrans script < $2 > not found"}
:do {
/system script run $2
:log warning "script $2 is run"
} on-error={
:log error "$ferrans script < $2 > error script code"
:return "$ferrans script $2 error script code"}
}
:if ($isp) do={:return "Done"} else={:return ("ERROR function $0: bad $1 parameter")}
}
# end func FaSaS