Skip to content

Commit a69cfb8

Browse files
authored
Added possibility to add a config file to the working directoy (#43)
2 parents da4fc0f + 4b4da6d commit a69cfb8

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fun main(args: Array<String>) {
5858
```
5959

6060
### Using configuration file
61-
Otherwise, Kotlog will try to load the configuration file (`~/.kotlog`) from the file system. Run `./kotlog` to create a new skeleton configuration file.
61+
Otherwise, Kotlog will try to load the configuration file from working directory's or user home's `~/.kotlog` file from the file system. Run `./kotlog` to create a new skeleton configuration file.
6262

6363
```json
6464
{

src/main/kotlin/io/github/tscholze/kotlog/Generator.kt

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import kotlin.io.path.*
3030
* of the developer behind this spare time project.
3131
*
3232
* @param args CLI arguments which will be processed
33-
* @param presetConfiguration Optional blog configuration, if empty config file is required.
33+
* @param presetConfiguration Optional blog configuration, if empty a config file is required.
3434
*
3535
* Possible CLI arguments:
3636
* - `-c 'My awesome title'`: Creates a new blog post
@@ -49,7 +49,8 @@ class Kotlog(args: Array<String>, presetConfiguration: BlogConfiguration? = null
4949

5050
// MARK: - Private constants -
5151

52-
private val ABSOLUTE_CONFIG_PATH = System.getProperty("user.home")+"/.kotlog"
52+
private val ABSOLUT_WORKING_DIRECTORY_CONFIG_PATH = "$WORKING_DIRECTORY/.kotlog"
53+
private val ABSOLUTE_HOME_CONFIG_PATH = System.getProperty("user.home")+"/.kotlog"
5354
private const val RELATIVE_POSTS_PATH = "__posts"
5455
private const val RELATIVE_STYLES_PATH = "__styles"
5556

@@ -209,7 +210,7 @@ class Kotlog(args: Array<String>, presetConfiguration: BlogConfiguration? = null
209210

210211
if (boolString == "y") {
211212
shellRun {
212-
command("code", listOf(ABSOLUTE_CONFIG_PATH))
213+
command("code", listOf(ABSOLUTE_HOME_CONFIG_PATH))
213214
}
214215
}
215216
}
@@ -328,14 +329,14 @@ class Kotlog(args: Array<String>, presetConfiguration: BlogConfiguration? = null
328329
private fun printNewConfigFileCreateMessage() {
329330
println("")
330331
println("A new configuration file has been created!")
331-
println("Path to file: $ABSOLUTE_CONFIG_PATH")
332+
println("Path to file: $ABSOLUTE_HOME_CONFIG_PATH")
332333
println("")
333334
}
334335

335336
private fun printConfigFileMissing() {
336337
println("")
337338
println("Error!")
338-
println("Cannot find any configuration file at: '$ABSOLUTE_CONFIG_PATH'")
339+
println("Cannot find any configuration file at: '$ABSOLUTE_HOME_CONFIG_PATH'")
339340
println("Create a skeleton for .kotlog file? (y/n)")
340341
if (readln() == "y") {
341342
createConfigFile()
@@ -355,21 +356,30 @@ class Kotlog(args: Array<String>, presetConfiguration: BlogConfiguration? = null
355356

356357
private fun createConfigFile() {
357358
writeToPath(
358-
ABSOLUTE_CONFIG_PATH,
359+
ABSOLUTE_HOME_CONFIG_PATH,
359360
ConfigurationHomeFile().render()
360361
)
361362

362363
printNewConfigFileCreateMessage()
363364
processCliNewConfigInput()
364365
}
365366

367+
/**
368+
* Tries to load configuration json file from disk.
369+
*
370+
* It first tries to load from working directory
371+
* and secondly from user's directory.
372+
*
373+
* @return Found and decoded configuration, null if no files exists or wrong formatted.
374+
*/
366375
private fun loadConfigFromFile(): BlogConfiguration? {
367-
val json = readFromFile(ABSOLUTE_CONFIG_PATH)
368-
return if (json == null) {
376+
val jsonString = readFromFile(ABSOLUT_WORKING_DIRECTORY_CONFIG_PATH) ?: readFromFile(ABSOLUTE_HOME_CONFIG_PATH)
377+
378+
return if (jsonString == null) {
369379
null
370380
} else {
371381
try {
372-
Json.decodeFromString(json)
382+
Json.decodeFromString(jsonString)
373383
} catch (e: java.lang.Exception) {
374384
null
375385
}

0 commit comments

Comments
 (0)