diff --git a/ides.nix b/ides.nix index f782615..084910a 100644 --- a/ides.nix +++ b/ides.nix @@ -252,8 +252,6 @@ in # transform into attrs that mkWorks expects to receive { - inherit name bin; - args = finalArgs; inherit bin sdArgs @@ -268,56 +266,148 @@ let # create commands to run and clean up services mkWorks = + name: { - name, unitName, bin, - args, cfgArgs, sdArgs, }: { - runner = '' + runner = pkgs.writeShellScriptBin "run" '' echo "[ides]: Starting ${name}.." systemd-run --user -G -u ${unitName} ${sdArgs} ${bin} ${cfgArgs} ''; - cleaner = '' + cleaner = pkgs.writeShellScriptBin "clean" '' echo "[ides]: Stopping ${name}.." systemctl --user stop ${unitName} ''; }; - works = - let - inherit (pkgs.lib) foldlAttrs; - in - foldlAttrs - ( - acc: _: svc: - let - pair = mkWorks svc; - in - { - runners = acc.runners + pair.runner; - cleaners = acc.cleaners + pair.cleaner; - } - ) - { - runners = ""; - cleaners = ""; - } - config._buildIdes.finalServices; + works = pkgs.lib.mapAttrs ( + name: serviceConf: mkWorks name serviceConf + ) config._buildIdes.finalServices; + # create the ides cli inherit (pkgs) writeShellScriptBin; - runners = writeShellScriptBin "ides" works.runners; - cleaners = writeShellScriptBin "et-tu" ( - works.cleaners - + '' - systemctl --user reset-failed - '' - ); - restart = writeShellScriptBin "restart" "et-tu; ides"; + inherit (pkgs.lib) foldlAttrs; + cli = + let + runAll = foldlAttrs ( + acc: name: works: + acc + "${works.runner}/bin/run\n" + ) "" works; + runFns = foldlAttrs ( + acc: name: works: + acc + + '' + function run-${name}() { + ${works.runner}/bin/run + } + '' + ) "" works; + cleanAll = foldlAttrs ( + acc: name: works: + acc + "${works.cleaner}/bin/clean\n" + ) "" works; + cleanFns = foldlAttrs ( + acc: name: works: + acc + + '' + function clean-${name}() { + ${works.cleaner}/bin/clean + } + '' + ) "" works; + names = foldlAttrs ( + acc: name: _: + acc + "${name}\n" + ) "" works; + in + writeShellScriptBin "ides" ( + let + help = '' + [ides]: use "ides [action] [target]" to control services. + actions: + start synonyms: run r + - start a service + stop synonyms: s clean et-tu + - stop a service + + restart synonyms: qq + - stop and then restart all services + + targets + - print a list of available targets + + help + - print this helpful information + + target names are the same as the attribute used to define a service. + an empty target will execute the action on all available services. + + current targets: + ''; + in + '' + function print-help() { + printf '${help}' + list-targets + } + + function list-targets() { + printf '${names}\n' + } + + ${runFns} + + function run-all() { + ${runAll} + } + + ${cleanFns} + + function clean-all() { + ${cleanAll} + } + + function action() { + action=$1 + if [ $# -gt 1 ]; then + shift + for service in "$@"; do + $action-$service + done + else + $action-all + fi + } + + case $1 in + start|run|r) + shift + action run $@ + ;; + clean|stop|et-tu|s) + shift + action clean $@ + ;; + restart|qq) + clean-all + run-all + ;; + targets) + list-targets + ;; + -h|h|help|*) + print-help + ;; + esac + '' + ); + + # create the ides shell final = let inherit (config._buildIdes) shellArgs; @@ -325,9 +415,7 @@ shellArgs // { nativeBuildInputs = (shellArgs.nativeBuildInputs or [ ]) ++ [ - runners - cleaners - restart + cli ]; shellHook = let