From 0fc16342554d293438a62a96adec4f48dac09d54 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Sat, 20 Aug 2022 02:32:50 +0200 Subject: feat(users/Profpatsch): init haskell exec-helpers Mirrors the rust exec-helpers Change-Id: I8fba49525949a8f38ca8c215e2ec7ba9d97d3bf9 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6116 Tested-by: BuildkiteCI Reviewed-by: Profpatsch --- users/Profpatsch/execline/ExecHelpers.hs | 48 ++++++++++++++++++++++++++++ users/Profpatsch/execline/default.nix | 18 +++++++++++ users/Profpatsch/execline/exec-helpers.cabal | 14 ++++++++ 3 files changed, 80 insertions(+) create mode 100644 users/Profpatsch/execline/ExecHelpers.hs create mode 100644 users/Profpatsch/execline/exec-helpers.cabal (limited to 'users/Profpatsch/execline') diff --git a/users/Profpatsch/execline/ExecHelpers.hs b/users/Profpatsch/execline/ExecHelpers.hs new file mode 100644 index 000000000000..438047b2b957 --- /dev/null +++ b/users/Profpatsch/execline/ExecHelpers.hs @@ -0,0 +1,48 @@ +{-# LANGUAGE DerivingStrategies #-} +{-# LANGUAGE GeneralizedNewtypeDeriving #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE QuasiQuotes #-} +{-# LANGUAGE TypeApplications #-} + +module ExecHelpers where + +import Data.String (IsString) +import MyPrelude +import qualified System.Exit as Sys + +newtype CurrentProgramName = CurrentProgramName { unCurrentProgramName :: Text } + deriving newtype (Show, Eq, Ord, IsString) + +-- | Exit 1 to signify a generic expected error +-- (e.g. something that sometimes just goes wrong, like a nix build). +dieExpectedError :: CurrentProgramName -> Text -> IO a +dieExpectedError = dieWith 1 + +-- | Exit 100 to signify a user error (“the user is holding it wrong”). +-- This is a permanent error, if the program is executed the same way +-- it should crash with 100 again. +dieUserError :: CurrentProgramName -> Text -> IO a +dieUserError = dieWith 100 + +-- | Exit 101 to signify an unexpected crash (failing assertion or panic). +diePanic :: CurrentProgramName -> Text -> IO a +diePanic = dieWith 101 + +-- | Exit 111 to signify a temporary error (such as resource exhaustion) +dieTemporary :: CurrentProgramName -> Text -> IO a +dieTemporary = dieWith 111 + +-- | Exit 126 to signify an environment problem +-- (the user has set up stuff incorrectly so the program cannot work) +dieEnvironmentProblem :: CurrentProgramName -> Text -> IO a +dieEnvironmentProblem = dieWith 126 + +-- | Exit 127 to signify a missing executable. +dieMissingExecutable :: CurrentProgramName -> Text -> IO a +dieMissingExecutable = dieWith 127 + +dieWith :: Natural -> CurrentProgramName -> Text -> IO a +dieWith status currentProgramName msg = do + putStderrLn [fmt|{currentProgramName & unCurrentProgramName}: {msg}|] + Sys.exitWith + (Sys.ExitFailure (status & fromIntegral @Natural @Int)) diff --git a/users/Profpatsch/execline/default.nix b/users/Profpatsch/execline/default.nix index 752774e6ad0c..9b8856a9a84a 100644 --- a/users/Profpatsch/execline/default.nix +++ b/users/Profpatsch/execline/default.nix @@ -7,6 +7,23 @@ let } (builtins.readFile ./exec_helpers.rs); + exec-helpers-hs = pkgs.haskellPackages.mkDerivation { + pname = "exec-helpers"; + version = "0.1.0"; + + src = depot.users.Profpatsch.exactSource ./. [ + ./exec-helpers.cabal + ./ExecHelpers.hs + ]; + + libraryHaskellDepends = [ + depot.users.Profpatsch.my-prelude + ]; + + isLibrary = true; + license = lib.licenses.mit; + }; + print-one-env = depot.nix.writers.rustSimple { name = "print-one-env"; @@ -32,6 +49,7 @@ in depot.nix.readTree.drvTargets { inherit exec-helpers + exec-helpers-hs print-one-env ; } diff --git a/users/Profpatsch/execline/exec-helpers.cabal b/users/Profpatsch/execline/exec-helpers.cabal new file mode 100644 index 000000000000..5441dff1d0f0 --- /dev/null +++ b/users/Profpatsch/execline/exec-helpers.cabal @@ -0,0 +1,14 @@ +cabal-version: 2.4 +name: exec-helpers +version: 0.1.0.0 +author: Profpatsch +maintainer: mail@profpatsch.de + +library + exposed-modules: ExecHelpers + + build-depends: + base ^>=4.15.1.0, + my-prelude + + default-language: Haskell2010 -- cgit 1.4.1