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
|
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE DataKinds #-}
module Propellor.PropAccum
( host
, Props(..)
, props
, (&)
, (&^)
, (!)
, hostProps
, modifyHostProps
) where
import Propellor.Types
import Propellor.Types.MetaTypes
import Propellor.Property
import Data.Monoid
import Prelude
-- | Defines a host and its properties.
--
-- > host "example.com" $ props
-- > & someproperty
-- > ! oldproperty
-- > & otherproperty
host :: HostName -> Props metatypes -> Host
host hn (Props ps) = Host hn ps (mconcat (map getInfoRecursive ps))
-- | Note that the metatype of a Host's properties is not retained,
-- so this defaults to UnixLike. So, using this with modifyHostProps can
-- add properties to a Host that conflict with properties already in it.
-- Use caution when using this.
hostProps :: Host -> Props UnixLike
hostProps = Props . hostProperties
modifyHostProps :: Host -> Props metatypes -> Host
modifyHostProps h ps = host (hostName h) ps
-- | Props is a combination of a list of properties, with their combined
-- metatypes.
data Props metatypes = Props [ChildProperty]
-- | Start accumulating a list of properties.
--
-- Properties can be added to it using `(&)` etc.
props :: Props UnixLike
props = Props []
infixl 1 &
infixl 1 &^
infixl 1 !
type family GetMetaTypes x
type instance GetMetaTypes (Property (MetaTypes t)) = MetaTypes t
type instance GetMetaTypes (RevertableProperty (MetaTypes t) undo) = MetaTypes t
-- | Adds a property to a Props.
--
-- Can add Properties and RevertableProperties
(&)
::
( IsProp p
, MetaTypes y ~ GetMetaTypes p
, CheckCombinable x y ~ 'CanCombine
)
=> Props (MetaTypes x)
-> p
-> Props (MetaTypes (Combine x y))
Props c & p = Props (c ++ [toChildProperty p])
-- | Adds a property before any other properties.
(&^)
::
( IsProp p
, MetaTypes y ~ GetMetaTypes p
, CheckCombinable x y ~ 'CanCombine
)
=> Props (MetaTypes x)
-> p
-> Props (MetaTypes (Combine x y))
Props c &^ p = Props (toChildProperty p : c)
-- | Adds a property in reverted form.
(!)
:: (CheckCombinable x z ~ 'CanCombine)
=> Props (MetaTypes x)
-> RevertableProperty (MetaTypes y) (MetaTypes z)
-> Props (MetaTypes (Combine x z))
Props c ! p = Props (c ++ [toChildProperty (revert p)])
-- addPropsHost :: Host -> [Prop] -> Host
-- addPropsHost (Host hn ps i) p = Host hn ps' i'
-- where
-- ps' = ps ++ [toChildProperty p]
-- i' = i <> getInfoRecursive p
|