Skip to content

Instantly share code, notes, and snippets.

@tilacog
Last active December 2, 2020 04:01
Show Gist options
  • Save tilacog/493ea93a00fdf0379b4d6050d863e46a to your computer and use it in GitHub Desktop.
Save tilacog/493ea93a00fdf0379b4d6050d863e46a to your computer and use it in GitHub Desktop.
bump.clj
#!/usr/bin/env bb
;; -*- mode: clojure -*-
(defn first-rest [col]
[(first col) (rest col)])
(defn split-path [file]
(let [file (io/file file)]
[(.getParent file) (.getName file)]))
(defn numeric? [s]
(let [s (drop-while #(Character/isDigit %) s)]
(empty? s)))
(defn bad-filename [file]
(binding [*out* *err*]
(println (format "Bad filename: %s" file))
(System/exit 2)))
(defn bump [file]
(let [[parent basename] (split-path file)
segments (str/split basename #"-")
[old-number tail] (first-rest segments)]
(when (or (empty? tail) (not (numeric? old-number)))
(bad-filename file))
(let [new-number (+ 1 (Integer/parseInt old-number))
new-name (str/join "-" (cons new-number tail))]
(io/file parent new-name))))
(let [[file] *command-line-args*]
(when (empty? file)
(println "Usage: <file>")
(System/exit 1))
(println (format "%s" (bump file))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment