Skip to content

refactor #122

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

Closed
wants to merge 1 commit into from
Closed
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
69 changes: 40 additions & 29 deletions src/nimblepkg/config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,50 @@ proc initConfig(): Config =

result.chcp = true

proc parseConfigFile(confFile: string, curr: Config): Config =
result = curr # return same, but with overrides
var f = newFileStream(confFile, fmRead)
var p: CfgParser
open(p, f, confFile)
while true:
var e = next(p)
case e.kind
of cfgEof:
break
of cfgSectionStart: discard
of cfgKeyValuePair, cfgOption:
case e.key.normalize
of "nimbledir":
# Ensure we don't restore the deprecated nimble dir.
if e.value != getHomeDir() / ".babel":
result.nimbleDir = e.value
of "chcp":
result.chcp = parseBool(e.value)
else:
raise newException(NimbleError, "Unable to parse config file:" &
" Unknown key: " & e.key)
of cfgError:
raise newException(NimbleError, "Unable to parse config file: " & e.msg)
close(p)
close(f)

proc readable(fname: string): bool =
# TODO: Make this simpler.
var f = newFileStream(fname, fmRead)
if f != nil:
result = true
close(f)

proc parseConfig*(): Config =
result = initConfig()
var confFile = getConfigDir() / "nimble" / "nimble.ini"

var f = newFileStream(confFile, fmRead)
if f == nil:
if (not readable(confFile)):
# Try the old deprecated babel.ini
confFile = getConfigDir() / "babel" / "babel.ini"
f = newFileStream(confFile, fmRead)
if f != nil:
echo("[Warning] Using deprecated config file at ", confFile)
if (not readable(confFile)):
return
echo("[Warning] Using deprecated config file at ", confFile)

if f != nil:
echo("Reading from config file at ", confFile)
var p: CfgParser
open(p, f, confFile)
while true:
var e = next(p)
case e.kind
of cfgEof:
break
of cfgSectionStart: discard
of cfgKeyValuePair, cfgOption:
case e.key.normalize
of "nimbledir":
# Ensure we don't restore the deprecated nimble dir.
if e.value != getHomeDir() / ".babel":
result.nimbleDir = e.value
of "chcp":
result.chcp = parseBool(e.value)
else:
raise newException(NimbleError, "Unable to parse config file:" &
" Unknown key: " & e.key)
of cfgError:
raise newException(NimbleError, "Unable to parse config file: " & e.msg)
close(p)
echo("Reading from config file at ", confFile)
result = parseConfigFile(confFile, result)