@@ -17,27 +17,40 @@ import fs2.io.file.Path
17
17
import fs2 .io .file .Flags
18
18
19
19
trait ConfigLoader [F [_]] {
20
- def saveConfig (config : Config ): F [Unit ]
20
+
21
+ def saveConfig (
22
+ config : Config
23
+ ): F [Unit ]
24
+
21
25
def loadConfig : F [Config ]
22
26
}
23
27
24
28
object ConfigLoader {
25
- def apply [F [_]](using F : ConfigLoader [F ]): ConfigLoader [F ] = F
29
+
30
+ def apply [F [_]](
31
+ using F : ConfigLoader [F ]
32
+ ): ConfigLoader [F ] = F
26
33
27
34
def cached [F [_]: Ref .Make : FlatMap ]: ConfigLoader [F ] => F [ConfigLoader [F ]] =
28
35
underlying =>
29
36
underlying.loadConfig.flatMap(Ref [F ].of(_)).map { ref =>
30
37
new ConfigLoader [F ] {
31
- def saveConfig (config : Config ): F [Unit ] = underlying.saveConfig(config) *> ref.set(config)
38
+ def saveConfig (
39
+ config : Config
40
+ ): F [Unit ] = underlying.saveConfig(config) *> ref.set(config)
32
41
val loadConfig : F [Config ] = ref.get
33
42
}
34
43
}
35
44
36
- def withCreateFileIfMissing [F [_]: UserOutput : Console : MonadThrow ](configPath : Path ): ConfigLoader [F ] => ConfigLoader [F ] = {
45
+ def withCreateFileIfMissing [F [_]: UserOutput : Console : MonadThrow ](
46
+ configPath : Path
47
+ ): ConfigLoader [F ] => ConfigLoader [F ] = {
37
48
38
49
val validInput = " Y"
39
50
40
- def askToCreateFile (originalException : NoSuchFileException ): F [Config ] =
51
+ def askToCreateFile (
52
+ originalException : NoSuchFileException
53
+ ): F [Config ] =
41
54
for {
42
55
_ <- UserOutput [F ].print(UserMessage .ConfigFileNotFound (configPath, validInput))
43
56
_ <- Console [F ].readLine.map(_.trim).ensure(originalException)(_.equalsIgnoreCase(validInput))
@@ -64,11 +77,17 @@ object ConfigLoader {
64
77
UserOutput [F ].print(UserMessage .SavedConfig (configPath))
65
78
}
66
79
67
- def saveConfig (config : Config ): F [Unit ] = underlying.saveConfig(config)
80
+ def saveConfig (
81
+ config : Config
82
+ ): F [Unit ] = underlying.saveConfig(config)
68
83
}
69
84
}
70
85
71
- def default [F [_]: Files : MonadThrow ](configPath : Path )(using fs2.Compiler [F , F ]): ConfigLoader [F ] =
86
+ def default [F [_]: Files : MonadThrow ](
87
+ configPath : Path
88
+ )(
89
+ using fs2.Compiler [F , F ]
90
+ ): ConfigLoader [F ] =
72
91
new ConfigLoader [F ] {
73
92
74
93
private val createOrOverwriteFile : Pipe [F , Byte , Nothing ] = bytes =>
@@ -77,7 +96,9 @@ object ConfigLoader {
77
96
Files [F ].writeAll(configPath, Flags .Write )
78
97
)
79
98
80
- def saveConfig (config : Config ): F [Unit ] =
99
+ def saveConfig (
100
+ config : Config
101
+ ): F [Unit ] =
81
102
fs2
82
103
.Stream
83
104
.emit(config)
@@ -97,6 +118,8 @@ object ConfigLoader {
97
118
98
119
}
99
120
100
- extension [F [_]: Applicative ](cl : ConfigLoader [F ]) def configAsk : Config .Ask [F ] = Config .askLiftF(cl.loadConfig)
121
+ extension [F [_]: Applicative ](
122
+ cl : ConfigLoader [F ]
123
+ ) def configAsk : Config .Ask [F ] = Config .askLiftF(cl.loadConfig)
101
124
102
125
}
0 commit comments