Skip to content

Added possibility to add a config file to the working directoy #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fun main(args: Array<String>) {
```

### Using configuration file
Otherwise, Kotlog will try to load the configuration file (`~/.kotlog`) from the file system. Run `./kotlog` to create a new skeleton configuration file.
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.

```json
{
Expand Down
28 changes: 19 additions & 9 deletions src/main/kotlin/io/github/tscholze/kotlog/Generator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import kotlin.io.path.*
* of the developer behind this spare time project.
*
* @param args CLI arguments which will be processed
* @param presetConfiguration Optional blog configuration, if empty config file is required.
* @param presetConfiguration Optional blog configuration, if empty a config file is required.
*
* Possible CLI arguments:
* - `-c 'My awesome title'`: Creates a new blog post
Expand All @@ -49,7 +49,8 @@ class Kotlog(args: Array<String>, presetConfiguration: BlogConfiguration? = null

// MARK: - Private constants -

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

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

if (boolString == "y") {
shellRun {
command("code", listOf(ABSOLUTE_CONFIG_PATH))
command("code", listOf(ABSOLUTE_HOME_CONFIG_PATH))
}
}
}
Expand Down Expand Up @@ -328,14 +329,14 @@ class Kotlog(args: Array<String>, presetConfiguration: BlogConfiguration? = null
private fun printNewConfigFileCreateMessage() {
println("")
println("A new configuration file has been created!")
println("Path to file: $ABSOLUTE_CONFIG_PATH")
println("Path to file: $ABSOLUTE_HOME_CONFIG_PATH")
println("")
}

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

private fun createConfigFile() {
writeToPath(
ABSOLUTE_CONFIG_PATH,
ABSOLUTE_HOME_CONFIG_PATH,
ConfigurationHomeFile().render()
)

printNewConfigFileCreateMessage()
processCliNewConfigInput()
}

/**
* Tries to load configuration json file from disk.
*
* It first tries to load from working directory
* and secondly from user's directory.
*
* @return Found and decoded configuration, null if no files exists or wrong formatted.
*/
private fun loadConfigFromFile(): BlogConfiguration? {
val json = readFromFile(ABSOLUTE_CONFIG_PATH)
return if (json == null) {
val jsonString = readFromFile(ABSOLUT_WORKING_DIRECTORY_CONFIG_PATH) ?: readFromFile(ABSOLUTE_HOME_CONFIG_PATH)

return if (jsonString == null) {
null
} else {
try {
Json.decodeFromString(json)
Json.decodeFromString(jsonString)
} catch (e: java.lang.Exception) {
null
}
Expand Down