-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Types.hs
326 lines (277 loc) · 10.3 KB
/
Types.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# OPTIONS_GHC -Wno-orphans -Wno-unused-pattern-binds #-}
module Smos.Scheduler.OptParse.Types where
import Autodocodec
import Control.Arrow (left)
import Control.Monad
import Crypto.Hash.SHA256 as SHA256
import Data.Aeson (FromJSON, ToJSON)
import Data.ByteString (ByteString)
import qualified Data.ByteString as SB
import qualified Data.ByteString.Base64 as Base64
import qualified Data.ByteString.Lazy as LB
import Data.Map (Map)
import qualified Data.Map as M
import Data.Maybe
import Data.Set (Set)
import qualified Data.Set as S
import Data.String
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import Data.Tree
import Data.Validity
import Data.Validity.Path ()
import qualified Data.Yaml as Yaml
import GHC.Generics (Generic)
import Path
import Path.IO
import Smos.CLI.Colour
import Smos.CLI.OptParse
import Smos.Data
import Smos.Directory.OptParse.Types
import Smos.Report.Time
import System.Cron (CronSchedule, parseCronSchedule, serializeCronSchedule)
import UnliftIO.IO.File
data Arguments = Arguments Command (FlagsWithConfigFile Flags)
deriving (Show, Eq)
data Command
= CommandCheck
| CommandNext
| CommandSample !FilePath !(Maybe FilePath)
| CommandSchedule
deriving (Show, Eq)
data Flags = Flags
{ flagDirectoryFlags :: !DirectoryFlags
}
deriving (Show, Eq)
data Configuration = Configuration
{ confDirectoryConfiguration :: !DirectoryConfiguration,
confColourConfiguration :: !(Maybe ColourConfiguration),
confSchedulerConfiguration :: !(Maybe SchedulerConfiguration)
}
deriving (Show, Eq)
deriving (FromJSON, ToJSON) via (Autodocodec Configuration)
instance HasCodec Configuration where
codec =
object "Configuration" $
Configuration
<$> objectCodec
.= confDirectoryConfiguration
<*> colourConfigurationTopLevelObjectCodec
.= confColourConfiguration
<*> optionalFieldOrNull "scheduler" "The scheduler configuration"
.= confSchedulerConfiguration
data SchedulerConfiguration = SchedulerConfiguration
{ schedulerConfSchedule :: !(Maybe Schedule)
}
deriving (Show, Eq)
deriving (FromJSON, ToJSON) via (Autodocodec SchedulerConfiguration)
instance HasCodec SchedulerConfiguration where
codec =
object "SchedulerConfiguration" $
SchedulerConfiguration
<$> optionalFieldOrNull "schedule" "The scheduler schedule"
.= schedulerConfSchedule
newtype Schedule = Schedule
{ scheduleItems :: [ScheduleItem]
}
deriving (Show, Eq, Generic)
instance HasCodec Schedule where
codec = dimapCodec Schedule scheduleItems codec
data ScheduleItem = ScheduleItem
{ scheduleItemDescription :: !(Maybe Text),
scheduleItemTemplate :: !FilePath,
scheduleItemDestination :: !DestinationPathTemplate,
scheduleItemRecurrence :: !Recurrence
}
deriving (Show, Eq, Generic)
deriving (FromJSON, ToJSON) via (Autodocodec ScheduleItem)
instance Validity ScheduleItem
serialiseScheduleItemConsistently :: ScheduleItem -> LB.ByteString
serialiseScheduleItemConsistently ScheduleItem {..} =
let ScheduleItem _ _ _ _ = undefined
in mconcat
[ LB.fromStrict $ TE.encodeUtf8 $ fromMaybe "" scheduleItemDescription,
LB.fromStrict $ TE.encodeUtf8 $ T.pack scheduleItemTemplate,
serialiseDestinationPathTemplateConsistently scheduleItemDestination,
serialiseRecurrenceConsistently scheduleItemRecurrence
]
instance HasCodec ScheduleItem where
codec =
object "ScheduleItem" $
ScheduleItem
<$> optionalFieldOrNull "description" "A description of this item"
.= scheduleItemDescription
<*> requiredField "template" "The file to copy from (absolute or relative, inside the workflow directory)"
.= scheduleItemTemplate
<*> requiredField "destination" "The file to copy to (relative, inside the workflow directory)"
.= scheduleItemDestination
<*> requiredField "schedule" "The schedule on which to do the copying"
.= scheduleItemRecurrence
instance Validity CronSchedule where
validate = trivialValidation
newtype DestinationPathTemplate = DestinationPathTemplate {destinationPathTemplatePath :: Path Rel File}
deriving (Show, Eq, Generic)
instance Validity DestinationPathTemplate
instance HasCodec DestinationPathTemplate where
codec = dimapCodec DestinationPathTemplate destinationPathTemplatePath codec
serialiseDestinationPathTemplateConsistently :: DestinationPathTemplate -> LB.ByteString
serialiseDestinationPathTemplateConsistently =
LB.fromStrict
. TE.encodeUtf8
. T.pack
. fromRelFile
. destinationPathTemplatePath
data Environment = Environment
{ envDirectoryEnvironment :: !DirectoryEnvironment
}
deriving (Show, Eq)
data Instructions = Instructions Dispatch Settings
deriving (Show, Eq)
data Dispatch
= DispatchCheck
| DispatchNext
| DispatchSample !(Path Abs File) !(Maybe DestinationPathTemplate)
| DispatchSchedule
deriving (Show, Eq)
data Settings = Settings
{ setDirectorySettings :: !DirectorySettings,
setSchedule :: !Schedule,
setColourSettings :: !ColourSettings
}
deriving (Show, Eq)
newtype ScheduleItemHash = ScheduleItemHash {unScheduleItemHash :: ByteString}
deriving stock (Show, Eq, Ord, Generic)
instance Validity ScheduleItemHash
hashScheduleItem :: ScheduleItem -> ScheduleItemHash
hashScheduleItem = ScheduleItemHash . SHA256.hashlazy . serialiseScheduleItemConsistently
renderScheduleItemHash :: ScheduleItemHash -> Text
renderScheduleItemHash = TE.decodeUtf8 . Base64.encode . unScheduleItemHash
parseScheduleItemHash :: Text -> Maybe ScheduleItemHash
parseScheduleItemHash t = case Base64.decode (TE.encodeUtf8 t) of
Left _ -> Nothing
Right sb -> pure $ ScheduleItemHash sb
writeScheduleTemplate :: Path Abs File -> ScheduleTemplate -> IO ()
writeScheduleTemplate p a = do
ensureDir $ parent p
writeBinaryFileDurableAtomic (fromAbsFile p) (Yaml.encode a)
readScheduleTemplate :: Path Abs File -> IO (Maybe (Either String ScheduleTemplate))
readScheduleTemplate f = do
mContents <- forgivingAbsence $ SB.readFile $ fromAbsFile f
forM mContents $ \cts ->
pure $ left Yaml.prettyPrintParseException $ Yaml.decodeEither' cts
newtype ScheduleTemplate = ScheduleTemplate
{ scheduleTemplateForest :: Forest EntryTemplate
}
deriving (Show, Eq, Generic)
deriving (FromJSON, ToJSON) via (Autodocodec ScheduleTemplate)
instance Validity ScheduleTemplate
instance HasCodec ScheduleTemplate where
codec =
dimapCodec ScheduleTemplate scheduleTemplateForest $
entryForestCodec "EntryTemplate" codec
data EntryTemplate = EntryTemplate
{ entryTemplateHeader :: Header,
entryTemplateContents :: Maybe Contents,
entryTemplateTimestamps :: Map TimestampName TimestampTemplate,
entryTemplateProperties :: Map PropertyName PropertyValue,
entryTemplateState :: Maybe (Maybe TodoState),
entryTemplateTags :: Set Tag
}
deriving (Show, Eq, Generic)
deriving (FromJSON, ToJSON) via (Autodocodec EntryTemplate)
instance Validity EntryTemplate
newEntryTemplate :: Header -> EntryTemplate
newEntryTemplate h =
EntryTemplate
{ entryTemplateHeader = h,
entryTemplateContents = Nothing,
entryTemplateTimestamps = M.empty,
entryTemplateProperties = M.empty,
entryTemplateState = Nothing,
entryTemplateTags = S.empty
}
instance HasCodec EntryTemplate where
codec =
named "EntryTemplate" $
dimapCodec f g $
eitherCodec
(codec <?> "A header-only entry template")
( object "EntryTemplate" $
EntryTemplate
<$> optionalFieldOrNullWithOmittedDefault' "header" emptyHeader
.= entryTemplateHeader
<*> optionalFieldOrNull' "contents"
.= entryTemplateContents
<*> optionalFieldOrNullWithOmittedDefault' "timestamps" M.empty
.= entryTemplateTimestamps
<*> optionalFieldOrNullWithOmittedDefault' "properties" M.empty
.= entryTemplateProperties
<*> optionalField' "state"
.= entryTemplateState
<*> optionalFieldOrNullWithOmittedDefault' "tags" S.empty
.= entryTemplateTags
)
where
f = \case
Left h -> newEntryTemplate h
Right et -> et
g et =
if et == newEntryTemplate (entryTemplateHeader et)
then Left (entryTemplateHeader et)
else Right et
newtype TimestampTemplate = TimestampTemplate
{ timestampTemplateText :: Text
}
deriving stock (Show, Eq, Ord, Generic)
deriving newtype (IsString)
deriving (FromJSON, ToJSON) via (Autodocodec TimestampTemplate)
instance Validity TimestampTemplate
instance HasCodec TimestampTemplate where
codec = dimapCodec TimestampTemplate timestampTemplateText codec
newtype UTCTimeTemplate = UTCTimeTemplate
{ utcTimeTemplateText :: Text
}
deriving stock (Show, Eq, Ord, Generic)
deriving newtype (IsString)
deriving (FromJSON, ToJSON) via (Autodocodec UTCTimeTemplate)
instance Validity UTCTimeTemplate
instance HasCodec UTCTimeTemplate where
codec = dimapCodec UTCTimeTemplate utcTimeTemplateText codec
data Recurrence
= -- | This much time after the last one
HaircutRecurrence !Time
| -- | At these times
RentRecurrence !CronSchedule
deriving stock (Show, Eq, Generic)
deriving (FromJSON, ToJSON) via (Autodocodec Recurrence)
instance Validity Recurrence
serialiseRecurrenceConsistently :: Recurrence -> LB.ByteString
serialiseRecurrenceConsistently =
LB.fromStrict . TE.encodeUtf8 . \case
HaircutRecurrence t -> renderTime t
RentRecurrence cs -> serializeCronSchedule cs
instance HasCodec Recurrence where
codec =
dimapCodec f g $
eitherCodec
(codec <?> "Haircut recurrence")
(codec <?> "Rent recurrence")
where
f = \case
Left ndc -> HaircutRecurrence ndc
Right cs -> RentRecurrence cs
g = \case
HaircutRecurrence ndc -> Left ndc
RentRecurrence cs -> Right cs
instance HasCodec CronSchedule where
codec =
bimapCodec parseCronSchedule serializeCronSchedule codec
<?> "Cron schedule, see https://en.wikipedia.org/wiki/Cron#Overview"