torrent/nushell/tools/runonce.nu
2024-01-13 13:01:54 +03:00

61 lines
1.6 KiB
Plaintext

#run a command only once
export def "run" [
name: string
task: closure
#is the code which will be executed
--reset: bool = false
#means we redo the command
] {
mkdir $"($env.HOME)/hero/done/)"
let state_file = [ $env.HOME 'hero' done ('nu_done_' + $name) ] | path join
if ($reset or (not ($state_file | path exists ) ) ) {
let start = date now
# let result = do $task
do $task
let end = date now
let total = $end - $start | format duration sec
let body = $"given task finished in ($total)"
"" | save $state_file -f
# return $result
# } else {
# "Command " + $name + " has already been run."
}
}
#reset all the done states
export def "reset" [
prefix: string
] {
ls $"($env.HOME)/hero/done/nu_done*" | each { |it| rm $it.name}
}
# runonce once 'lls' { ls / } --reset true
# def "runonce" [
# name: string
# task: closure
# #is the code which will be executed
# --reset: bool = false
# #means we redo the command
# ] {
# let state_file = [ $env.HOME 'hero' ('nu_done_' + $name) ] | path join
# if ($reset or (not ($state_file | path exists ) ) ) {
# let start = date now
# # let result = do $task
# do $task
# let end = date now
# let total = $end - $start | format duration sec
# let body = $"given task finished in ($total)"
# "" | save $state_file -f
# # return $result
# # } else {
# # "Command " + $name + " has already been run."
# }
# }