-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.purs
95 lines (77 loc) · 3.06 KB
/
Main.purs
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
module Main where
import Prelude
import Control.Async (Async, runAsync, mapExceptT')
import Control.File (JsonParseErrorImpl(..), ReadFileErrorImpl(..), ReadJsonFileError, _readFileError, _readFileJsonParseError)
import Control.File as File
import Data.Either (Either(..))
import Data.Explain (class Explain, explain)
import Simple.JSON (class ReadForeign)
import Data.Maybe (Maybe)
import Data.Newtype (class Newtype, unwrap, wrap)
import Data.Variant (SProxy(..), Variant, default, inj, onMatch)
import Effect (Effect)
import Effect.Console (log)
import Effect.Exception (message)
import Github.Api.Api (AccessToken)
import Github.Api.Repository (Repository, GetRepoError, getRepo)
import Github.Entities (OrgName, RepoName)
import Github.Settings.BranchProtection (BranchProtectionSettings)
import Type.Row as R
-- Used to join error types together (unicode option+22C3)
infixr 0 type R.RowApply as ⋃
-- Empty Set (unicode option+00D8)
type Ø
= ()
newtype Config
= Config
{ githubToken :: Maybe AccessToken
, organization :: OrgName
, repository :: RepoName
, branchProtection :: BranchProtectionSettings
}
derive instance newtypeConfig :: Newtype Config _
derive newtype instance readForeignConfig :: ReadForeign Config
--
type ReadConfigError ρ
= ( readConfigError ∷ ReadConfigErrorImpl ρ | ρ )
_readConfigError :: SProxy "readConfigError"
_readConfigError = SProxy
readConfigError :: ∀ ρ. Variant (ReadJsonFileError ρ) -> Variant (ReadConfigError ρ)
readConfigError = inj _readConfigError <<< wrap
newtype ReadConfigErrorImpl e
= ReadConfigErrorImpl (Variant (ReadJsonFileError e))
derive instance newtypeReadConfigErrorImpl :: Newtype (ReadConfigErrorImpl e) _
instance explainReadConfigError :: Explain (ReadConfigErrorImpl e) where
explain err =
default "Unknown problem"
# onMatch
{ readFileError:
\(ReadFileErrorImpl { error }) ->
"Can't read the config file: " <> message error
, readFileJsonParseError:
\(JsonParseErrorImpl { path, error }) ->
"There was a problem parsing the config file '" <> path <> "':" <> explain error
}
$ unwrap err
groupByReadConfigError :: ∀ e. Variant (ReadJsonFileError ⋃ ReadConfigError ⋃ e) -> Variant (ReadConfigError ⋃ e)
groupByReadConfigError =
onMatch
{ readFileError: readConfigError <<< inj _readFileError
, readFileJsonParseError: readConfigError <<< inj _readFileJsonParseError
}
identity
--
readConfig :: ∀ e. String -> Async (ReadConfigError e) Config
readConfig path = File.readJsonFile path `mapExceptT'` groupByReadConfigError
-------------------------------------------------------------------------------
type ProgramErrors
= (ReadConfigError ⋃ GetRepoError ⋃ Ø)
program :: Async ProgramErrors Repository
program = do
Config c <- readConfig "./config.json"
getRepo c.githubToken c.organization c.repository
main :: Effect Unit
main = runAsync program resultCb
where
resultCb (Left err) = log $ "Buu: " <> explain err
resultCb (Right result) = log $ "Yeay: " <> show result