1
1
mirror of https://github.com/nmattia/niv.git synced 2024-11-07 22:36:53 +03:00

Prettify logs

This commit is contained in:
Nicolas Mattia 2019-09-08 19:43:31 +02:00
parent 43747ff10b
commit 44c52165a8
3 changed files with 85 additions and 42 deletions

View File

@ -21,6 +21,7 @@ extra-source-files:
dependencies: dependencies:
- aeson - aeson
- aeson-pretty - aeson-pretty
- ansi-terminal
- base < 5 - base < 5
- bytestring - bytestring
- directory - directory

View File

@ -17,6 +17,7 @@ import Data.FileEmbed (embedFile)
import Data.Hashable (Hashable) import Data.Hashable (Hashable)
import Data.Maybe (fromMaybe) import Data.Maybe (fromMaybe)
import Data.String.QQ (s) import Data.String.QQ (s)
import Niv.Logger
import Niv.GitHub import Niv.GitHub
import Niv.Update import Niv.Update
import System.Exit (exitFailure, ExitCode(ExitSuccess)) import System.Exit (exitFailure, ExitCode(ExitSuccess))
@ -166,34 +167,35 @@ parseCmdInit = Opts.info (pure cmdInit <**> Opts.helper) $ mconcat desc
cmdInit :: IO () cmdInit :: IO ()
cmdInit = do cmdInit = do
job "initializing" $ do
-- Writes all the default files -- Writes all the default files
-- a path, a "create" function and an update function for each file. -- a path, a "create" function and an update function for each file.
forM_ forM_
[ ( pathNixSourcesNix [ ( pathNixSourcesNix
, (`createFile` initNixSourcesNixContent) , (`createFile` initNixSourcesNixContent)
, \path content -> do , \path content -> do
if shouldUpdateNixSourcesNix content if shouldUpdateNixSourcesNix content
then do then do
putStrLn "Updating sources.nix" putStrLn "Updating sources.nix"
B.writeFile path initNixSourcesNixContent B.writeFile path initNixSourcesNixContent
else putStrLn "Not updating sources.nix" else putStrLn "Not updating sources.nix"
) )
, ( pathNixSourcesJson , ( pathNixSourcesJson
, \path -> do , \path -> do
createFile path initNixSourcesJsonContent createFile path initNixSourcesJsonContent
-- Imports @niv@ and @nixpkgs@ (19.03) -- Imports @niv@ and @nixpkgs@ (19.03)
putStrLn "Importing 'niv' ..." putStrLn "Importing 'niv' ..."
cmdAdd Nothing (PackageName "nmattia/niv", PackageSpec HMS.empty) cmdAdd Nothing (PackageName "nmattia/niv", PackageSpec HMS.empty)
putStrLn "Importing 'nixpkgs' ..." putStrLn "Importing 'nixpkgs' ..."
cmdAdd cmdAdd
(Just (PackageName "nixpkgs")) (Just (PackageName "nixpkgs"))
( PackageName "NixOS/nixpkgs-channels" ( PackageName "NixOS/nixpkgs-channels"
, PackageSpec (HMS.singleton "branch" "nixos-19.03")) , PackageSpec (HMS.singleton "branch" "nixos-19.03"))
, \path _content -> dontCreateFile path) , \path _content -> dontCreateFile path)
] $ \(path, onCreate, onUpdate) -> do ] $ \(path, onCreate, onUpdate) -> do
exists <- Dir.doesFileExist path exists <- Dir.doesFileExist path
if exists then B.readFile path >>= onUpdate path else onCreate path if exists then B.readFile path >>= onUpdate path else onCreate path
where where
createFile :: FilePath -> B.ByteString -> IO () createFile :: FilePath -> B.ByteString -> IO ()
createFile path content = do createFile path content = do
@ -333,25 +335,25 @@ specToLockedAttrs = fmap (Locked,) . unPackageSpec
-- TODO: sexy logging + concurrent updates -- TODO: sexy logging + concurrent updates
cmdUpdate :: Maybe (PackageName, PackageSpec) -> IO () cmdUpdate :: Maybe (PackageName, PackageSpec) -> IO ()
cmdUpdate = \case cmdUpdate = \case
Just (packageName, cliSpec) -> do Just (packageName, cliSpec) ->
T.putStrLn $ "Updating single package: " <> unPackageName packageName job ("Update " <> T.unpack (unPackageName packageName)) $ do
sources <- unSources <$> getSources sources <- unSources <$> getSources
eFinalSpec <- case HMS.lookup packageName sources of eFinalSpec <- case HMS.lookup packageName sources of
Just defaultSpec -> do Just defaultSpec -> do
fmap attrsToSpec <$> tryEvalUpdate fmap attrsToSpec <$> tryEvalUpdate
(specToLockedAttrs cliSpec <> specToFreeAttrs defaultSpec) (specToLockedAttrs cliSpec <> specToFreeAttrs defaultSpec)
(githubUpdate nixPrefetchURL githubLatestRev githubRepo) (githubUpdate nixPrefetchURL githubLatestRev githubRepo)
Nothing -> abortCannotUpdateNoSuchPackage packageName Nothing -> abortCannotUpdateNoSuchPackage packageName
case eFinalSpec of case eFinalSpec of
Left e -> abortUpdateFailed [(packageName, e)] Left e -> abortUpdateFailed [(packageName, e)]
Right finalSpec -> Right finalSpec ->
setSources $ Sources $ setSources $ Sources $
HMS.insert packageName finalSpec sources HMS.insert packageName finalSpec sources
Nothing -> do Nothing -> job "Updating all packages" $ do
sources <- unSources <$> getSources sources <- unSources <$> getSources
esources' <- forWithKeyM sources $ esources' <- forWithKeyM sources $

40
src/Niv/Logger.hs Normal file
View File

@ -0,0 +1,40 @@
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Niv.Logger where
import qualified System.Console.ANSI as ANSI
import Data.String (IsString)
import UnliftIO
job :: String -> IO () -> IO ()
job str act = do
say (bold str)
tryAny act >>= \case
Right () -> say $ green "Done" <> ": " <> Log str
Left e -> say $ red "ERROR" <> ":\n" <> Log (show e)
newtype Log = Log { unLog :: String }
deriving newtype (Semigroup, Monoid, IsString)
say :: Log -> IO ()
say = putStrLn . unLog
green :: String -> Log
green str = Log $
ANSI.setSGRCode [ANSI.SetConsoleIntensity ANSI.BoldIntensity] <>
ANSI.setSGRCode [ANSI.SetColor ANSI.Foreground ANSI.Vivid ANSI.Green] <>
str <> ANSI.setSGRCode [ANSI.Reset]
red :: String -> Log
red str = Log $
ANSI.setSGRCode [ANSI.SetColor ANSI.Foreground ANSI.Vivid ANSI.Red] <>
str <> ANSI.setSGRCode [ANSI.Reset]
bold :: String -> Log
bold str = Log $
ANSI.setSGRCode [ANSI.SetConsoleIntensity ANSI.BoldIntensity] <>
ANSI.setSGRCode [ANSI.SetColor ANSI.Foreground ANSI.Vivid ANSI.White] <>
str <> ANSI.setSGRCode [ANSI.Reset]