From 67c0a8ded4c8462c975385281ff864f0334d5bab Mon Sep 17 00:00:00 2001 From: Florian Schrofner Date: Fri, 1 Dec 2023 22:02:46 +0100 Subject: [PATCH] fix git user data --- base/base.clj | 9 ++++++++ configs/configs.clj | 52 ++++++++++++++++++++++++++----------------- packages/packages.clj | 10 ++------- 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/base/base.clj b/base/base.clj index e8ee31c..853cb16 100644 --- a/base/base.clj +++ b/base/base.clj @@ -1,4 +1,13 @@ +#!/usr/bin/env bb + +(require + '[clojure.string :as str]) + ;;executes shell command but throws exception on error (defn- safe-sh [& commands] (as-> (apply shell/sh commands) $ (if (= (:exit $) 0) $ (throw (Exception. (:err $)))))) + +;;executes command as user calling this script with sudo +(defn- safe-sh-as-user [& commands] + (safe-sh "su" "-c" (str/join " " (map #(if (str/includes? % " ") (str "\"" % "\"") %) commands)) user)) diff --git a/configs/configs.clj b/configs/configs.clj index a797e5b..28e6b6e 100755 --- a/configs/configs.clj +++ b/configs/configs.clj @@ -2,18 +2,17 @@ (require '[babashka.fs :as fs] - '[clojure.tools.cli :refer [parse-opts]] - '[clojure.string :as string]) + '[clojure.string :as str]) + +(def user (System/getenv "SUDO_USER")) ;;todo: import from base.clj (defn- safe-sh [& commands] (as-> (apply shell/sh commands) $ (if (= (:exit $) 0) $ (throw (Exception. (:err $)))))) -(def cli-options - [["-u" "--user USER" "The user to set up"]]) - -(def options (:options (parse-opts *command-line-args* cli-options))) +(defn- safe-sh-as-user [& commands] + (safe-sh "su" "-c" (str/join " " (map #(if (str/includes? % " ") (str "\"" % "\"") %) commands)) user)) (defn- setup-spacemacs [user home] ;; installing spacemacs @@ -23,21 +22,34 @@ (safe-sh "chown" (str user ":" user) "-R" (str home "/.emacs.d")) (println "spacemacs set up")) -(if-let [user (:user options)] - (let [home (str "/home/" user)] - ;;todo: fetch spacemacs - ;;todo: disable locking when closing lid on ac - (println "applying configuration..") +(defn- setup-android-scripts [user home] + (let [android-scripts-dir (str home "/Projects/android-scripts")] + (fs/create-dirs android-scripts-dir) + (safe-sh "git" "clone" "ssh://git@git.schro.fi:4242/schrofi/android-scripts.git" android-scripts-dir) + (safe-sh "chown" (str user ":" user) "-R" android-scripts-dir))) - ;;(setup-spacemacs user home) +(let [home (str "/home/" user)] + ;;todo: fetch spacemacs + ;;todo: disable locking when closing lid on ac + (println "applying configuration..") - ;;change shell to fish - (safe-sh "chsh" "-s" "/usr/bin/fish" user) - (println "changed shell to fish") + ;;(setup-spacemacs user home) - (safe-sh "git" "config" "--global" "user.name" "Florian Schrofner") - (safe-sh "git" "config" "--global" "user.email" "florian@schro.fi") - (println "changed git user data") + ;;change shell to fish + (safe-sh "chsh" "-s" "/usr/bin/fish" user) + (println "changed shell to fish") - (println "configuration applied")) - (println "error: must provide a user to set up")) + (safe-sh-as-user "git" "config" "--global" "user.name" "Florian Schrofner") + (safe-sh-as-user "git" "config" "--global" "user.email" "florian@schro.fi") + (println "changed git user data") + + ;;creating projects dir + (let [projects-dir (str home "/Projects")] + (fs/create-dirs projects-dir) + (safe-sh "chown" (str user ":" user) "-R" projects-dir) + (println "created project directory")) + + (setup-android-scripts user home) + (println "set up android scripts") + + (println "configuration applied")) diff --git a/packages/packages.clj b/packages/packages.clj index 3b31b9f..bcea4b3 100755 --- a/packages/packages.clj +++ b/packages/packages.clj @@ -1,20 +1,14 @@ #!/usr/bin/env bb (require - '[babashka.fs :as fs] - '[clojure.tools.cli :refer [parse-opts]] - '[clojure.string :as string]) + '[babashka.fs :as fs]) ;;todo: import from base.clj (defn- safe-sh [& commands] (as-> (apply shell/sh commands) $ (if (= (:exit $) 0) $ (throw (Exception. (:err $)))))) -(def cli-options - [["-u" "--user USER" "The user to set up"]]) - -(def options (:options (parse-opts *command-line-args* cli-options))) -(def user (:user options)) +(def user (System/getenv "SUDO_USER")) (def home (str "/home/" user)) (def packages {