Matthias Andreas Benkard | 262110d | 2021-08-24 06:35:55 +0200 | [diff] [blame] | 1 | {-# LANGUAGE TypeApplications #-} |
| 2 | {-# LANGUAGE UndecidableInstances #-} |
| 3 | |
| 4 | module Mulkup.Main where |
| 5 | |
| 6 | import Colog (Message, richMessageAction, simpleMessageAction) |
| 7 | import Colog.Polysemy.Effect (Log, runLogAction) |
Matthias Andreas Benkard | 262110d | 2021-08-24 06:35:55 +0200 | [diff] [blame] | 8 | import qualified Data.Set as Set |
Matthias Andreas Benkard | 262110d | 2021-08-24 06:35:55 +0200 | [diff] [blame] | 9 | import Mulkup.Bupstash |
| 10 | import Mulkup.Config |
| 11 | import Mulkup.Flags |
| 12 | import Mulkup.Logging |
| 13 | import Mulkup.Prelude |
Matthias Andreas Benkard | 178f991 | 2024-08-30 18:44:51 +0200 | [diff] [blame^] | 14 | import Mulkup.Ranking |
Matthias Andreas Benkard | 262110d | 2021-08-24 06:35:55 +0200 | [diff] [blame] | 15 | import Optics |
| 16 | import Options.Applicative |
| 17 | ( execParser, |
| 18 | fullDesc, |
| 19 | helper, |
| 20 | info, |
| 21 | ) |
| 22 | import Polysemy (Member, Sem) |
| 23 | import Polysemy.Error |
| 24 | import Polysemy.Final |
| 25 | import Polysemy.Reader (Reader, asks, runReader) |
| 26 | |
| 27 | main :: IO () |
| 28 | main = do |
| 29 | flags <- execParser $ info (flagParser <**> helper) fullDesc |
| 30 | let messageAction = |
| 31 | if verbose flags |
| 32 | then richMessageAction |
| 33 | else simpleMessageAction |
| 34 | |
| 35 | config <- readConfig "./config.dhall" |
| 36 | |
| 37 | result <- |
| 38 | main' |
| 39 | & runBupstash |
| 40 | & runLogAction @IO messageAction |
| 41 | & runReader (config :: MulkupConfig) |
| 42 | & errorToIOFinal @Text |
| 43 | & embedToFinal @IO |
| 44 | & runFinal @IO |
| 45 | |
| 46 | case result of |
| 47 | Left err -> do |
| 48 | error err |
| 49 | Right () -> |
| 50 | return () |
| 51 | |
| 52 | main' :: (Member (Log Message) r, Member (Reader MulkupConfig) r, Member Bupstash r) => Sem r () |
| 53 | main' = do |
| 54 | stashes <- asks @MulkupConfig (^. #stashes) |
| 55 | forM_ stashes $ \stash -> do |
| 56 | let labels = [("name", stash ^. #name)] |
| 57 | |
| 58 | currentItems <- bupList (BupFilter labels Nothing) |
| 59 | |
Matthias Andreas Benkard | 178f991 | 2024-08-30 18:44:51 +0200 | [diff] [blame^] | 60 | let (keepIds, rmIds) = rankBupItems (stash ^. #tiers) currentItems |
Matthias Andreas Benkard | 262110d | 2021-08-24 06:35:55 +0200 | [diff] [blame] | 61 | |
| 62 | logInfo (show labels <> " Keeping: " <> show (Set.toList keepIds)) |
| 63 | logInfo (show labels <> " Removing: " <> show (Set.toList rmIds)) |
| 64 | bupRemove (Set.toList rmIds) |
| 65 | |
| 66 | logInfo (show labels <> " Creating backup.") |
| 67 | bupPut |
| 68 | (stash ^. #baseDir) |
| 69 | (stash ^. #exclusions) |
| 70 | labels |