job 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #!/usr/bin/env bash
  2. . pkg.sh
  3. . cfg.sh
  4. . log.sh
  5. case $1 in
  6. auto)
  7. p=$(readlink -f $0)
  8. TREE_STUMP=$(grep ^TREE_STUMP ./paths.conf | cut -f2 -d'=' | sed 's/^"//;s/"$//')
  9. if ! [ -d ${TREE_STUMP} ]; then
  10. $p init
  11. $p update db
  12. fi
  13. $p update conf
  14. if [ -z "$(ps -ef | grep 'job hook' | grep -v 'grep')" ]; then
  15. $p hook
  16. fi
  17. ;;
  18. update)
  19. case "${2}" in
  20. db)
  21. _log info 'Updating database...'
  22. store_db
  23. update_db
  24. ;;
  25. conf)
  26. _log info 'Updating config...'
  27. update_conf
  28. ;;
  29. esac
  30. ;;
  31. download)
  32. shift
  33. while [ $# -gt 0 ]; do
  34. D=""
  35. for i in ${@}; do
  36. shift
  37. D="${D} $(depends_pkg ${i})"
  38. ! [ -z ${D} ] && _log info "${i} depends \x1b[1m${D}\x1b[m"
  39. P="$(lookup_pkg ${i})"
  40. if [ $? -ne 0 ]; then
  41. _log error "could not resolve: ${i}"
  42. else
  43. download_pkg ${P}
  44. [ $? -eq 0 ] && prepare_pkg $(echo ${P} | cut -s -f2 -d' ')
  45. fi
  46. done
  47. set -- ${D}
  48. done
  49. ;;
  50. list)
  51. shift
  52. FILTER="${1}"
  53. while read -r line; do
  54. OUT=""
  55. set -- ${line}
  56. while [ $# -gt 0 ]; do
  57. case $(echo "${1}" | grep -oE "${FILTER}|--") in
  58. 'NEW')
  59. OUT="\x1b[32mN\x1b[00m "
  60. ;;
  61. 'MOD')
  62. OUT=" \x1b[33mC\x1b[00m "
  63. ;;
  64. 'DEL')
  65. OUT=" \x1b[31mD\x1b[00m"
  66. ;;
  67. '--')
  68. [ -z "${OUT}" ] && { shift; continue; }
  69. DIFF=$(expr 6 - $(echo ${OUT} | wc --bytes))
  70. printf '[%*b] %s\n' "${DIFF}" "${OUT}" "$(echo ${line} | cut -s -f3- -d'-')"
  71. ;;
  72. esac
  73. shift
  74. done
  75. done < <(diff_db)
  76. ;;
  77. hook)
  78. EVENT_FILE=$(grep ^EVENT_FILE ./paths.conf | cut -f2 -d'=' | sed 's/^"//;s/"$//')
  79. if ! [ -z "${2}" ]; then
  80. while read -r line; do
  81. run_pkg $line $(date --rfc-3339=seconds | tr ' ' '_')
  82. done < <(grep "${2}" ./hooks.conf | cut -f2- -d' ')
  83. if ! [ "${2}" = "exit" ]; then
  84. $(readlink -f $0) hook
  85. fi
  86. else
  87. if [ $(stat -t ${EVENT_FILE} | cut -f2 -d' ') -ne 0 ]; then
  88. $(readlink -f $0) hook "$(sed -i -e '1{w /dev/stdout' -e 'd}' ${EVENT_FILE})";
  89. else
  90. { watch -g stat ${EVENT_FILE} > /dev/null && { $(readlink -f $0) hook "$(sed -i -e '1{w /dev/stdout' -e 'd}' ${EVENT_FILE})"; };} &
  91. fi
  92. fi
  93. ;;
  94. event)
  95. EVENT_FILE=$(grep ^EVENT_FILE ./paths.conf | cut -f2 -d'=' | sed 's/^"//;s/"$//')
  96. if [ -z "${2}" ]; then
  97. touch ${EVENT_FILE}
  98. else
  99. echo "${2}" >> ${EVENT_FILE}
  100. fi
  101. ;;
  102. init)
  103. TREE_STUMP=$(grep ^TREE_STUMP ./paths.conf | cut -f2 -d'=' | sed 's/^"//;s/"$//')
  104. if [ -d ${TREE_STUMP} ]; then
  105. _log error 'Already initialized'
  106. exit 1
  107. fi
  108. while read -r line; do
  109. line=$(echo ${line} | cut -f2 -d'=' | sed 's/^"//;s/"$//')
  110. if [ "$(echo $line | cut -c1)" = "#" ]; then
  111. MODE="$(echo $line | cut -c2-)"
  112. continue
  113. fi
  114. case "${MODE}" in
  115. FOLDER)
  116. DIR=$(eval "echo ${line}")
  117. mkdir -p ${DIR} 2>&1 > /dev/null
  118. ;;
  119. FILE)
  120. DIR=$(eval "echo ${line}")
  121. # TODO: Create file path?
  122. touch ${DIR} 2>&1 > /dev/null
  123. ;;
  124. URL)
  125. break
  126. ;;
  127. esac
  128. case $(ls -ld ${DIR} | cut -c1) in
  129. d)
  130. type=DIRECTORY
  131. ;;
  132. -)
  133. type=FILE
  134. ;;
  135. esac
  136. if [ $? -ne 0 ]; then
  137. _log error "cannot create ${type} ${DIR}" 2>/dev/null
  138. else
  139. _log success "created ${type} ${DIR}"
  140. fi
  141. done < paths.conf
  142. ;;
  143. list)
  144. exit
  145. shift
  146. FMT="%10s %10 %20s\n"
  147. printf "${FMT}" "NAME" "REPO" "DEPENDS"
  148. while read -r repo; do
  149. while read -r line; do
  150. printf "${FMT}" $(basename ${repo}) ${line}
  151. done < ${repo}
  152. done < <(find ${REPO_STORE} -type f)
  153. ;;
  154. deinit)
  155. TREE_STUMP=$(grep ^TREE_STUMP ./paths.conf | cut -f2 -d'=' | sed 's/^"//;s/"$//')
  156. echo ${TREE_STUMP}
  157. if ! [ -d ${TREE_STUMP} ]; then
  158. _log error "Not initialized."
  159. exit
  160. fi
  161. START=""
  162. DELFILES=$(mktemp)
  163. while read -r line; do
  164. line=$(readlink -f ${line})
  165. if [ $(echo ${START} | wc --bytes) -lt $(echo ${line} | wc --bytes) ]; then
  166. START=${line}
  167. fi
  168. done < <(find ${TREE_STUMP})
  169. [ -f "${START}" ] && START=$(dirname ${START})
  170. while true; do
  171. while read -r l; do
  172. # _log warn "deleting ${l}"
  173. # rm -rf ${l}
  174. if [ -z "$(grep -E ^${l}\$ ${DELFILES})" ]; then
  175. echo ${l} >> ${DELFILES}
  176. fi
  177. done < <(find ${START} -type d)
  178. START=$(dirname ${START})
  179. if [ ${START} = $(readlink -f ${TREE_STUMP}) ]; then
  180. break
  181. fi
  182. done
  183. while read -r l; do
  184. # _log warn "deleting ${l}"
  185. # rm -rf ${l}
  186. if [ -z "$(grep -E ^${l}\$ ${DELFILES})" ]; then
  187. echo ${l} >> ${DELFILES}
  188. fi
  189. done < <(find ${START} -type d | sort -r)
  190. while read -r line; do
  191. # _log warn "deleting ${line}"
  192. if [ -z "$(grep ${line} ${DELFILES})" ]; then
  193. echo ${line} >> ${DELFILES}
  194. fi
  195. done < <(sed -n '/^#FILE/,/^#URL/{/^#URL/d; /^#FILE/d; s/^.*="//; s/"$//p}' ./paths.conf)
  196. _log info 'The following files are going to be deleted:'
  197. while read -r line; do
  198. _log info $line
  199. done < ${DELFILES}
  200. printf 'Are you okay with this [\x1b[32;1my\x1b[m/\x1b[31;1mN\x1b[m]: '
  201. read response
  202. case $(echo $response | tr '[[:lower:]]' '[[:upper:]]') in
  203. Y*)
  204. while read -r line; do
  205. _log success "deleting ${line}"
  206. rm -rf $line
  207. done < ${DELFILES}
  208. ;;
  209. esac
  210. rm ${DELFILES}
  211. ;;
  212. show)
  213. TREE_STUMP=$(grep ^TREE_STUMP ./paths.conf | cut -f2 -d'=' | sed 's/^"//;s/"$//')
  214. eval "LOG_STORE="$(grep ^LOG_STORE ./paths.conf | cut -f2 -d'=' | sed 's/^"//;s/"$//')
  215. RUN=""
  216. while read -r line; do
  217. if ! [ -z $(basename $line | grep -E "${2}.*") ]; then
  218. RUN="${RUN} $(basename $line)"
  219. fi
  220. done < <(find ${LOG_STORE} -maxdepth 1 -mindepth 1| sort)
  221. case $(echo "$RUN" | wc --words) in
  222. 0)
  223. _log error 'no log matches'
  224. exit 1
  225. ;;
  226. 1)
  227. _log success "matched:\x1b[1m${RUN}\x1b[m"
  228. RUN=$(echo ${RUN} | cut -f1 -d' ')
  229. ;;
  230. *)
  231. _log error 'multiple matches!'
  232. exit 1
  233. ;;
  234. esac
  235. TIME=""
  236. ANSI=""
  237. MACRO=0
  238. while read -r line; do
  239. if [ $(head -1 $line) = ".macros" ]; then
  240. MACRO=1
  241. fi
  242. while read -r l; do
  243. if [ $MACRO -eq 1 ] && [ $(echo $l | cut -c1) = "." ]; then
  244. case $(echo $l | cut -c2- | cut -f1 -d' ') in
  245. type)
  246. case $(echo ${l} | cut -s -f2 -d' ') in
  247. error)
  248. ANSI='\x1b[1;31;7m'
  249. ;;
  250. *)
  251. ANSI='\x1b[m'
  252. ;;
  253. esac
  254. ;;
  255. reset)
  256. ANSI='\x1b[m'
  257. ;;
  258. time)
  259. TIME=$(date --date="@$(echo ${l} | cut -f2 -d' ')")
  260. ;;
  261. esac
  262. else
  263. if [ $MACRO -eq 1 ]; then
  264. printf "[%s]: %b%s\x1b[m\n" "${TIME}" "${ANSI}" "$l"
  265. else
  266. printf "%s\n" "$l"
  267. fi
  268. fi
  269. done < <(cat $line)
  270. done < <(find ${LOG_STORE}/${RUN} -mindepth 1 ! -iname 'kickoff')
  271. ;;
  272. esac