diff --git a/project.clj b/project.clj index 6ecb782..4ac92b0 100644 --- a/project.clj +++ b/project.clj @@ -6,6 +6,7 @@ :dependencies [[org.clojure/clojure "1.10.1"] [com.github.thjomnx/java-systemd "1.1.0"] [com.taoensso/timbre "4.10.0"] + [cljstache/cljstache "2.0.6"] [org.clojure/core.async "1.2.603"]] :main ^:skip-aot stream.core :target-path "target/%s" diff --git a/resources/commander/templates/unit.mustache b/resources/commander/templates/unit.mustache new file mode 100644 index 0000000..97056a7 --- /dev/null +++ b/resources/commander/templates/unit.mustache @@ -0,0 +1,8 @@ +[Unit] +Description={{ description }} + +[Service] +ExecStart={{ command }} + +[Install] +WantedBy={{ target }} diff --git a/src/stream/commander/api.clj b/src/stream/commander/api.clj new file mode 100644 index 0000000..c158150 --- /dev/null +++ b/src/stream/commander/api.clj @@ -0,0 +1,11 @@ +(ns stream.commander.api) + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ; Globals, Structures and State ; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; TODO: specs +(defrecord process + [unit-name monitor-channel ffmpeg-config problems]) +(def processes (ref {})) diff --git a/src/stream/commander/impl.clj b/src/stream/commander/impl.clj new file mode 100644 index 0000000..92766a7 --- /dev/null +++ b/src/stream/commander/impl.clj @@ -0,0 +1,33 @@ +(ns stream.commander.impl + (:require [cljstache.core :as template] + [clojure.java.io :as io] + [taoensso.timbre :as timbre + :refer [log trace debug info warn error fatal report + logf tracef debugf infof warnf errorf fatalf reportf + spy get-env]])) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ; Unit file Management ; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(defn- render-unit-file + "Renders the unit file from a template. + The template can be found at: + resources/commander/templates/unit.mustache" + [command description target] + (template/render-resource "commander/templates/unit.mustache" + {:description description + :command command + :target target})) + +(defn create-unit-file! + "Creates or overwrites a service unit file in the appropriate + directory and returns the file path." + [name command description target] + (let [unit-contents (render-unit-file command description target) + path (str (System/getProperty "user.home") "/.config/systemd/user/" name ".service") + file (io/as-file path)] + (.mkdirs (.getParentFile file)) + (spit path unit-contents) + (debug "Wrote a unit file to:" path) + path)) diff --git a/test/stream/commander_test.clj b/test/stream/commander_test.clj new file mode 100644 index 0000000..6089093 --- /dev/null +++ b/test/stream/commander_test.clj @@ -0,0 +1,16 @@ +(ns stream.commander-test + (:require [stream.commander.impl :as impl] + [stream.commander.api :as api] + [clojure.test :refer :all] + [clojure.java.io :as io])) + +(deftest unit-files + (testing "The rendering." + (is (#'impl/render-unit-file "test" "test" "test"))) + (let [name (str (java.util.UUID/randomUUID))] + (testing "Writing a unit file." + (let [file (io/as-file + (impl/create-unit-file! name + "test" "test" "test"))] + (is (.exists file)) + (.delete file))))) diff --git a/test/stream/core_test.clj b/test/stream/core_test.clj index 007ef58..5d2d91b 100644 --- a/test/stream/core_test.clj +++ b/test/stream/core_test.clj @@ -1,7 +1,3 @@ (ns stream.core-test (:require [clojure.test :refer :all] [stream.core :refer :all])) - -(deftest a-test - (testing "FIXME, I fail." - (is (= 0 1))))