This repository was archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 631
/
Copy pathConfiguration.hs
97 lines (77 loc) · 3.57 KB
/
Configuration.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
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE RecordWildCards #-}
-- | Propagation of runtime configuration.
module Pos.Chain.Update.Configuration
( UpdateConfiguration(..)
, HasUpdateConfiguration
, updateConfiguration
, withUpdateConfiguration
, ourAppName
, ourSystemTag
, lastKnownBlockVersion
, curSoftwareVersion
, currentSystemTag
) where
import Universum
import Data.Aeson (FromJSON (..), ToJSON (..), genericToJSON,
withObject, (.:), (.:?))
import Data.Aeson.Options (defaultOptions)
import Data.Maybe (fromMaybe)
import Data.Reflection (Given (..), give)
import Distribution.System (buildArch, buildOS)
import Pos.Chain.Update.ApplicationName (ApplicationName)
import Pos.Chain.Update.BlockVersion (BlockVersion (..))
import Pos.Chain.Update.SoftwareVersion (SoftwareVersion (..))
import Pos.Chain.Update.SystemTag (SystemTag (..), archHelper,
osHelper)
----------------------------------------------------------------------------
-- Config itself
----------------------------------------------------------------------------
type HasUpdateConfiguration = Given UpdateConfiguration
withUpdateConfiguration :: UpdateConfiguration -> (HasUpdateConfiguration => r) -> r
withUpdateConfiguration = give
updateConfiguration :: HasUpdateConfiguration => UpdateConfiguration
updateConfiguration = given
data UpdateConfiguration = UpdateConfiguration
{
-- | Name of this application.
ccApplicationName :: !ApplicationName
-- | Last known block version
, ccLastKnownBlockVersion :: !BlockVersion
-- | Application version
, ccApplicationVersion :: !Word32
-- | System tag.
, ccSystemTag :: !SystemTag
}
deriving (Eq, Generic, Show)
instance ToJSON UpdateConfiguration where
toJSON = genericToJSON defaultOptions
instance FromJSON UpdateConfiguration where
parseJSON = withObject "UpdateConfiguration" $ \o -> do
ccApplicationName <- o .: "applicationName"
ccLastKnownBlockVersion <- o .: "lastKnownBlockVersion"
ccApplicationVersion <- o .: "applicationVersion"
ccSystemTag <- fromMaybe currentSystemTag <$> o .:? "systemTag"
pure UpdateConfiguration {..}
----------------------------------------------------------------------------
-- Various constants
----------------------------------------------------------------------------
-- | Name of our application.
ourAppName :: HasUpdateConfiguration => ApplicationName
ourAppName = ccApplicationName updateConfiguration
-- | Last block version application is aware of.
lastKnownBlockVersion :: HasUpdateConfiguration => BlockVersion
lastKnownBlockVersion = ccLastKnownBlockVersion updateConfiguration
-- | Version of application (code running)
curSoftwareVersion :: HasUpdateConfiguration => SoftwareVersion
curSoftwareVersion = SoftwareVersion ourAppName (ccApplicationVersion updateConfiguration)
-- | @SystemTag@ corresponding to the operating system/architecture pair the program was
-- compiled in.
-- The @Distribution.System@ module
-- (https://hackage.haskell.org/package/Cabal-2.0.1.1/docs/Distribution-System.html)
-- from @Cabal@ was used to access to a build's host machine @OS@ and @Arch@itecture
-- information.
currentSystemTag :: SystemTag
currentSystemTag = SystemTag (toText (osHelper buildOS ++ archHelper buildArch))
ourSystemTag :: HasUpdateConfiguration => SystemTag
ourSystemTag = ccSystemTag updateConfiguration