Skip to content

Commit 5c9c3e9

Browse files
committed
Better handling of cli args and SnapshotType
1 parent ac7d818 commit 5c9c3e9

File tree

2 files changed

+54
-47
lines changed

2 files changed

+54
-47
lines changed

src/Main.hs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,31 @@ import Data.Semigroup ((<>))
99
import Options.Applicative
1010
import System.Exit (ExitCode (ExitSuccess, ExitFailure), exitWith)
1111

12+
data RequestedSnapshots = RequestedSnapshots
13+
{ yearly :: Bool
14+
, monthly :: Bool
15+
, weekly :: Bool
16+
, daily :: Bool
17+
, hourly :: Bool
18+
, quater_hourly :: Bool
19+
} deriving (Show)
20+
21+
22+
reqToTypes :: RequestedSnapshots -> [SnapshotType]
23+
reqToTypes req
24+
| yearly req = Yearly : (reqToTypes req { yearly = False })
25+
| monthly req = Monthly : (reqToTypes req { monthly = False })
26+
| weekly req = Weekly : (reqToTypes req { weekly = False })
27+
| daily req = Daily : (reqToTypes req { daily = False })
28+
| hourly req = Hourly : (reqToTypes req { hourly = False })
29+
| quater_hourly req = QuaterHourly : (reqToTypes req { quater_hourly = False })
30+
| otherwise = []
31+
1232

1333
-- TODO use submcommands, so you can have other commands for explictly
1434
-- pruning, listing, purging, etc.
15-
cliParser :: Parser SnapshotType
16-
cliParser = SnapshotType
35+
cliParser :: Parser RequestedSnapshots
36+
cliParser = RequestedSnapshots
1737
<$> switch
1838
( long "yearly"
1939
<> help "This snapshot is a yearly snapshot")
@@ -33,10 +53,8 @@ cliParser = SnapshotType
3353
( long "quater_hourly"
3454
<> help "This snapshot is a quater_hourly snapshot")
3555

36-
--handleCli :: SnapshotType -> IO ()
37-
handleCli :: (MonadError String m, MonadIO m) => SnapshotType -> m ()
38-
handleCli (SnapshotType False False False False False False) =
39-
throwError "Specify at least one snapshot type (see --help)"
56+
handleCli :: (MonadError String m, MonadIO m) => [SnapshotType] -> m ()
57+
handleCli [] = throwError "Specify at least one snapshot type (see --help)"
4058
handleCli types = do
4159
snapshot <- createSnapshot
4260
liftIO $ storeSnapshot snapshot types
@@ -57,8 +75,9 @@ main = do
5775
( fullDesc
5876
<> progDesc "Create a new snapshot in the given timelines"
5977
<> header "apfs-auto-snapshot - Automaticaaly craete and delete APFS snapshots")
60-
parsedCli <- execParser opts
61-
result <- runExceptT $ handleCli parsedCli
78+
req <- execParser opts
79+
let snapshotTypes = reqToTypes req
80+
result <- runExceptT $ handleCli snapshotTypes
6281
case result of
6382
Right _ -> return ()
6483
Left err -> printAndExit err

src/Snapshot.hs

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Snapshot where
66
import Control.Monad (when)
77
import Control.Monad.Except
88
import Database.SQLite.Simple
9+
import Database.SQLite.Simple.ToField
910
import Data.List.Split (splitOn)
1011
import Data.Monoid ((<>))
1112
import System.Exit (ExitCode (ExitSuccess, ExitFailure))
@@ -27,15 +28,20 @@ instance Show SnapshotDate where
2728
show d = printf "%04d-%02d-%02d-%02d%02d%02d" (year d) (month d) (day d)
2829
(hour d) (minute d) (second d)
2930

30-
-- What categories the current snapshot will fall under
31-
data SnapshotType = SnapshotType
32-
{ yearly :: Bool
33-
, monthly :: Bool
34-
, weekly :: Bool
35-
, daily :: Bool
36-
, hourly :: Bool
37-
, quater_hourly :: Bool
38-
} deriving (Show)
31+
data SnapshotType = Yearly
32+
| Monthly
33+
| Weekly
34+
| Daily
35+
| Hourly
36+
| QuaterHourly
37+
38+
instance ToField SnapshotType where
39+
toField Yearly = toField ("yearly" :: String)
40+
toField Monthly = toField ("monthly" :: String)
41+
toField Weekly = toField ("weekly" :: String)
42+
toField Daily = toField ("daily" :: String)
43+
toField Hourly = toField ("hourly" :: String)
44+
toField QuaterHourly = toField ("quater_hourly" :: String)
3945

4046

4147
parseSnapshotDates :: (MonadError String m) => [String] -> m [SnapshotDate]
@@ -88,8 +94,8 @@ instance FromRow IdField where
8894

8995
-- TODO switch to opaleye or something that gives us better type support
9096
--storeSnapshot :: (MonadError String m, MonadIO m) -> SnapshotDate -> SnapshotType -> m ()
91-
storeSnapshot :: SnapshotDate -> SnapshotType -> IO ()
92-
storeSnapshot s flags = do
97+
storeSnapshot :: SnapshotDate -> [SnapshotType] -> IO ()
98+
storeSnapshot s types = do
9399
-- Open db with foreign key support
94100
conn <- open "apfs-auto-snapshot.db"
95101
execute_ conn "PRAGMA foreign_keys = ON"
@@ -98,34 +104,16 @@ storeSnapshot s flags = do
98104
execute conn "INSERT INTO snapshots (name) VALUES (?)"
99105
(Only (show s :: String))
100106
res <- query_ conn "SELECT last_insert_rowid()" :: IO [IdField]
101-
let snapshot_id = primaryKey $ head res
102-
103-
-- Insert the types of snapshots this is for in the timelines. Can be
104-
-- multiple timelines at the same time.
105-
when (yearly flags)
106-
(execute conn
107-
"INSERT INTO timelines (snapshot_id, snapshot_type) VALUES (?, 'yearly')"
108-
(Only (show snapshot_id :: String)))
109-
when (monthly flags)
110-
(execute conn
111-
"INSERT INTO timelines (snapshot_id, snapshot_type) VALUES (?, 'monthly')"
112-
(Only (show snapshot_id :: String)))
113-
when (weekly flags)
114-
(execute conn
115-
"INSERT INTO timelines (snapshot_id, snapshot_type) VALUES (?, 'weekly')"
116-
(Only (show snapshot_id :: String)))
117-
when (daily flags)
118-
(execute conn
119-
"INSERT INTO timelines (snapshot_id, snapshot_type) VALUES (?, 'daily')"
120-
(Only (show snapshot_id :: String)))
121-
when (hourly flags)
122-
(execute conn
123-
"INSERT INTO timelines (snapshot_id, snapshot_type) VALUES (?, 'hourly')"
124-
(Only (show snapshot_id :: String)))
125-
when (quater_hourly flags)
126-
(execute conn
127-
"INSERT INTO timelines (snapshot_id, snapshot_type) VALUES (?, 'quater-hourly')"
128-
(Only (show snapshot_id :: String)))
107+
let snapshotId = primaryKey $ head res
108+
109+
-- Insert what type of snapshot this is
110+
mapM_ (storeSnapshotType conn snapshotId) types
129111

130112
-- Close the connection
131113
close conn
114+
where
115+
storeSnapshotType :: Connection -> Int -> SnapshotType -> IO ()
116+
storeSnapshotType conn snapshotId snapshotType = execute conn query args
117+
where
118+
query = "INSERT INTO timelines (snapshot_id, snapshot_type) VALUES (?, ?)"
119+
args = (snapshotId, snapshotType)

0 commit comments

Comments
 (0)