import os, times, parsecfg, strutils, json import types proc readConfig* (path: string): types.Config = if not fileExists(path): echo "Error: config-file does not exist!" quit(QuitFailure) let configFile = loadConfig(path) let outputDirectory = configFile.getSectionValue("", "outputDirectory", "") title = configFile.getSectionValue("", "title", "nimstagit") description = configFile.getSectionValue("", "description", "") scanPath = configFile.getSectionValue("", "scanPath", "") projectsFile = configFile.getSectionValue("", "projectsFile", "") readmeFiles = configFile.getSectionValue("", "readmeFiles", "README, readme, README.md, readme.md") renderMarkdown = configFile.getSectionValue("", "renderMarkdown", "true").parseBool var projectsList: seq[string] if scanPath == "": echo "Error: config-value 'outputDirectory' has to be set!" quit(QuitFailure) if scanPath == "": echo "Error: config-value 'scanPath' has to be set!" quit(QuitFailure) if projectsFile != "": if not fileExists(projectsFile): echo "Error: projectsFile does not exist!" quit(QuitFailure) for element in readFile(projectsFile).splitLines: if element != "": projectsList.add(element) else: for kind, element in walkDir(scanPath): if kind != pcDir: continue projectsList.add(splitPath(element).tail) result = types.Config( outputDirectory: outputDirectory, title: title, description: description, scanPath: scanPath, projectsList: projectsList, readmeFiles: readmeFiles.split(", "), renderMarkdown: renderMarkdown ) proc mergeJson* (a: JsonNode, b: JsonNode): JsonNode = result = a for key, val in b: result[key] = val proc nameFromPath* (path: string): string = result = splitPath(path).tail; removeSuffix(result, ".git") proc relativeTimeFromNow* (time: Time): string = let timeNow = now().toTime timeDiff = timeNow - time weeks = inWeeks(timeDiff) days = inDays(timeDiff) hours = inHours(timeDiff) minutes = inMinutes(timeDiff) if weeks > 0: result = $weeks & " week(s)" elif days > 0: result = $days & " day(s)" elif hours > 0: result = $hours & " hour(s)" elif minutes > 2: result = $minutes & " Minutes" else: result = "now"