blob: 829ef5d275c765830cb64d8fc3d2a1828cc8051b [file] [log] [blame]
Matthias Andreas Benkard262110d2021-08-24 06:35:55 +02001{-# LANGUAGE TypeApplications #-}
2{-# LANGUAGE UndecidableInstances #-}
3
4module Mulkup.Main where
5
6import Colog (Message, richMessageAction, simpleMessageAction)
7import Colog.Polysemy.Effect (Log, runLogAction)
Matthias Andreas Benkard262110d2021-08-24 06:35:55 +02008import qualified Data.Set as Set
Matthias Andreas Benkard262110d2021-08-24 06:35:55 +02009import Mulkup.Bupstash
10import Mulkup.Config
11import Mulkup.Flags
12import Mulkup.Logging
13import Mulkup.Prelude
Matthias Andreas Benkard178f9912024-08-30 18:44:51 +020014import Mulkup.Ranking
Matthias Andreas Benkard262110d2021-08-24 06:35:55 +020015import Optics
16import Options.Applicative
17 ( execParser,
18 fullDesc,
19 helper,
20 info,
21 )
22import Polysemy (Member, Sem)
23import Polysemy.Error
24import Polysemy.Final
25import Polysemy.Reader (Reader, asks, runReader)
26
27main :: IO ()
28main = 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
52main' :: (Member (Log Message) r, Member (Reader MulkupConfig) r, Member Bupstash r) => Sem r ()
53main' = 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 Benkard178f9912024-08-30 18:44:51 +020060 let (keepIds, rmIds) = rankBupItems (stash ^. #tiers) currentItems
Matthias Andreas Benkard262110d2021-08-24 06:35:55 +020061
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