nuscripts/tools/runonce.nu
2024-01-13 13:51:29 +00:00

62 lines
1.5 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
] {
let $state_dir = $"($env.BASE)/done"
mkdir $state_dir
let $state_file = $"($state_dir)/nu_done_($name)"
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.BASE)/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.BASE '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."
# }
# }