~kungtotte/dtt

71297dbda43709dbea6954fc5be9e0ba102bf04a — Thomas Landin 4 months ago a96ce78
Change manual quit codes to use an enum

So we use the same number to mean the same thing, and to make the code
a little bit more self-documenting.

Also fix a typo (missing ")
1 files changed, 21 insertions(+), 10 deletions(-)

M src/dtt.nim
M src/dtt.nim => src/dtt.nim +21 -10
@@ 17,6 17,17 @@ const
let
  version = "0.1.1"

type
  DttError {.pure.} = enum
    CreateDirectoryFailed,
    DirectoryNotFound,
    DirectoryNotEmpty,
    NotADttDirectory,
    RemoveDirectoryFailed,
    UnknownError

proc ord(dtterror: DttError): int =
  result = ord(dtterror)

proc initCmd(dir: string, force: bool = false) =
  var wd: string = dir


@@ 32,14 43,14 @@ proc initCmd(dir: string, force: bool = false) =
          of pcDir:
            if os.existsDir(wd / f):
              stderr.writeLine(fmt"{bin}: working directory not empty, add -f/--force to overwrite")
              quit(4)
              quit(ord(DttError.DirectoryNotEmpty))
          of pcFile:
            if os.existsFile(wd / f):
              stderr.writeLine(fmt"{bin}: working directory not empty, add -f/--force to overwrite")
              quit(4)
              quit(ord(DttError.DirectoryNotEmpty))
          else:
            stderr.writeLine(fmt"Unknown error occurred: {f} is neither file nor directory")
            quit(3)
            quit(ord(DttError.UnknownError))

  if os.existsDir(wd):
    try:


@@ 52,7 63,7 @@ proc initCmd(dir: string, force: bool = false) =
        e = repr(getCurrentException())
        msg = getCurrentExceptionMsg()
      stderr.writeLine(fmt"{bin}: Exception: {e} with message: {msg}")
      quit(1)
      quit(ord(DttError.CreateDirectoryFailed))

    writeFile(wd / "config.cfg", tmpl.cfg)
    writeFile(wd / template_dir / "page.mustache", tmpl.page)


@@ 61,14 72,14 @@ proc initCmd(dir: string, force: bool = false) =

  else:
    stderr.writeLine(fmt"{bin}: Directory '{wd}' does not exist.")
    quit(1)
    quit(ord(DttError.DirectoryNotFound))


proc cleanCmd(all: bool = false, force: bool = false) =
  let wd = if all: os.getCurrentDir() else: os.getCurrentDir() / output_dir
  if not force and not existsFile(os.getCurrentDir() / "config.cfg"):
    stderr.writeLine(fmt"""{bin}: config.cfg not found, this doesn't look like a {bin} directory. are you sure you wish to clean it? Confirm action with -f/--force.""")
    quit(6)
    quit(ord(DttError.NotADttDirectory))
  if os.existsDir(wd):
    try:
      os.removeDir(wd)


@@ 77,10 88,10 @@ proc cleanCmd(all: bool = false, force: bool = false) =
        e = repr(getCurrentException())
        msg = getCurrentExceptionMsg()
      stderr.writeLine(fmt"{bin}: Exception {e} with message: {msg}")
      quit(2)
      quit(ord(DttError.RemoveDirectoryFailed))
  else:
    stderr.writeLine(fmt"{bin}: {wd} does not exist.")
    quit(5)
    quit(ord(DttError.DirectoryNotFound))


proc loadMetaData(path: string, mcontext: var Context) =


@@ 131,7 142,7 @@ proc buildCmd() =
          copyFile(condir / f, savepath / fname & ext)
      else:
        stderr.writeLine(fmt"Unknown error occurred: {f} is neither file nor directory")
        quit(3)
        quit(ord(DttError.UnknownError))


let doc = """


@@ 160,6 171,6 @@ when isMainModule:
  if args["init"]:
    initCmd($args["DIR"], args["--force"])
  elif args["clean"]:
    cleanCmd(args["--all"], args["--force])
    cleanCmd(args["--all"], args["--force"])
  elif args["build"]:
    buildCmd()