Refactor utils out into cliutils package.

This commit is contained in:
Jonathan Bernard 2017-08-15 14:30:03 -05:00
parent 0a6023c656
commit e39c1186c8
8 changed files with 24 additions and 109 deletions

View File

@ -1,6 +1,5 @@
import docopt, os, sequtils, tempfile, uuids import cliutils, docopt, os, sequtils, tempfile, uuids
import strawbosspkg/private/util
import strawbosspkg/configuration import strawbosspkg/configuration
import strawbosspkg/core import strawbosspkg/core
import strawbosspkg/server import strawbosspkg/server
@ -65,7 +64,7 @@ Options
forceRebuild: args["--force-rebuild"], forceRebuild: args["--force-rebuild"],
workspaceDir: wkspDir) workspaceDir: wkspDir)
let status = core.initiateBuild(cfg, req, logProcOutput) let status = core.initiateRun(cfg, req, logProcOutput)
if status.state == "failed": raiseEx status.details if status.state == "failed": raiseEx status.details
echo "strawboss: build passed." echo "strawboss: build passed."
except: except:

View File

@ -1,5 +1,4 @@
import logging, json, os, nre, sequtils, strtabs, tables, times, uuids import cliutils, logging, json, os, nre, sequtils, strtabs, tables, times, uuids
import private/util
from langutils import sameContents from langutils import sameContents
from typeinfo import toAny from typeinfo import toAny
@ -96,6 +95,10 @@ proc setProject*(cfg: var StrawBossConfig, projectName: string, newDef: ProjectD
if not found: cfg.projects.add(newDef) if not found: cfg.projects.add(newDef)
# other utils
proc raiseEx*(reason: string): void =
raise newException(Exception, reason)
# internal utils # internal utils
let nullNode = newJNull() let nullNode = newJNull()
proc getIfExists(n: JsonNode, key: string): JsonNode = proc getIfExists(n: JsonNode, key: string): JsonNode =
@ -245,17 +248,3 @@ proc `$`*(s: BuildStatus): string = result = pretty(%s)
proc `$`*(req: RunRequest): string = result = pretty(%req) proc `$`*(req: RunRequest): string = result = pretty(%req)
proc `$`*(pd: ProjectDef): string = result = pretty(%pd) proc `$`*(pd: ProjectDef): string = result = pretty(%pd)
proc `$`*(cfg: StrawBossConfig): string = result = pretty(%cfg) proc `$`*(cfg: StrawBossConfig): string = result = pretty(%cfg)
# TODO: maybe a macro for more general-purpose, shallow object comparison?
#proc `==`*(a, b: ProjectDef): bool =
template shallowEquals(a, b: RootObj): bool =
if type(a) != type(b): return false
var anyB = toAny(b)
for name, value in a.fieldPairs:
if value != b[name]: return false
return true
#proc `==`*(a, b: ProjectDef): bool = result = shallowEquals(a, b)

View File

@ -1,6 +1,5 @@
import logging, nre, os, osproc, sequtils, streams, strtabs, strutils, tables, uuids import cliutils, logging, nre, os, osproc, sequtils, streams, strtabs, strutils, tables, uuids
import private/util
import configuration import configuration
from posix import link from posix import link
@ -20,6 +19,12 @@ type
step*: Step ## the step we're building step*: Step ## the step we're building
version*: string ## project version as returned by versionCmd version*: string ## project version as returned by versionCmd
proc sendMsg(h: HandleProcMsgCB, msg: TaintedString): void =
h.sendMsg(msg, nil, "strawboss")
proc sendErrMsg(h: HandleProcMsgCB, msg: TaintedString): void =
h.sendMsg(nil, msg, "strawboss")
proc resolveEnvVars(line: string, env: StringTableRef): string = proc resolveEnvVars(line: string, env: StringTableRef): string =
result = line result = line
for found in line.findAll(re"\$\w+|\$\{[^}]+\}"): for found in line.findAll(re"\$\w+|\$\{[^}]+\}"):
@ -277,7 +282,7 @@ proc initiateRun*(cfg: StrawBossConfig, req: RunRequest,
removeFile(wksp.artifactsDir & "/" & fn) removeFile(wksp.artifactsDir & "/" & fn)
if link(wksp.dir & "/" & fn, wksp.artifactsDir & "/" & fn) != 0: if link(wksp.dir & "/" & fn, wksp.artifactsDir & "/" & fn) != 0:
wksp.outputHandler.sendMsg(nil, wksp.outputHandler.sendErrMsg(
"WARN: could not link " & fn & " to artifacts dir.") "WARN: could not link " & fn & " to artifacts dir.")
runStep(wksp, step) runStep(wksp, step)

View File

@ -1,78 +0,0 @@
import os, osproc, streams, strtabs
from posix import kill
type HandleProcMsgCB* = proc (outMsg: TaintedString, errMsg: TaintedString, cmd: string): void
proc sendMsg*(h: HandleProcMsgCB, outMsg: TaintedString, errMsg: TaintedString = nil, cmd: string = "strawboss"): void =
if h != nil: h(outMsg, errMsg, cmd)
proc raiseEx*(reason: string): void =
raise newException(Exception, reason)
proc envToTable*(): StringTableRef =
result = newStringTable()
for k, v in envPairs():
result[k] = v
proc waitForWithOutput*(p: Process, msgCB: HandleProcMsgCB,
procCmd: string = ""):
tuple[output: TaintedString, error: TaintedString, exitCode: int] =
var pout = outputStream(p)
var perr = errorStream(p)
result = (TaintedString"", TaintedString"", -1)
var line = newStringOfCap(120).TaintedString
while true:
if pout.readLine(line):
msgCB.sendMsg(line, nil, procCmd)
result[0].string.add(line.string)
result[0].string.add("\n")
elif perr.readLine(line):
msgCB.sendMsg(nil, line, procCmd)
result[1].string.add(line.string)
result[1].string.add("\n")
else:
result[2] = peekExitCode(p)
if result[2] != -1: break
close(p)
proc exec*(command: string, workingDir: string = "",
args: openArray[string] = [], env: StringTableRef = nil,
options: set[ProcessOption] = {poUsePath},
msgCB: HandleProcMsgCB = nil):
tuple[output: TaintedString, error: TaintedString, exitCode: int]
{.tags: [ExecIOEffect, ReadIOEffect], gcsafe.} =
var p = startProcess(command, workingDir, args, env, options)
result = waitForWithOutput(p, msgCb, command)
proc loadEnv*(): StringTableRef =
result = newStringTable()
for k, v in envPairs():
result[k] = v
proc makeProcMsgHandler*(outSink, errSink: File): HandleProcMsgCB =
result = proc(outMsg, errMsg: TaintedString, cmd: string): void {.closure.} =
let prefix = if cmd != nil: cmd & ": " else: ""
if outMsg != nil: outSink.writeLine(prefix & outMsg)
if errMsg != nil: errSink.writeLine(prefix & errMsg)
proc makeProcMsgHandler*(outSink, errSink: Stream): HandleProcMsgCB =
result = proc(outMsg, errMsg: TaintedString, cmd: string): void {.closure.} =
let prefix = if cmd != nil: cmd & ": " else: ""
if outMsg != nil: outSink.writeLine(prefix & outMsg)
if errMsg != nil: errSink.writeLine(prefix & errMsg)
proc combineProcMsgHandlers*(a, b: HandleProcMsgCB): HandleProcMsgCB =
if a == nil: result = b
elif b == nil: result = a
else:
result = proc(cmd: string, outMsg, errMsg: TaintedString): void =
a(cmd, outMsg, errMsg)
b(cmd, outMsg, errMsg)

View File

@ -1,7 +1,7 @@
import algorithm, asyncdispatch, bcrypt, jester, json, jwt, logging, os, osproc, import algorithm, asyncdispatch, bcrypt, cliutils, jester, json, jwt, logging,
sequtils, strutils, tempfile, times, unittest, uuids os, osproc, sequtils, strutils, tempfile, times, unittest, uuids
import ./configuration, ./core, private/util import ./configuration, ./core
type Worker = object type Worker = object
runId*: UUID runId*: UUID

View File

@ -1,10 +1,10 @@
import httpclient, json, os, osproc, sequtils, strutils, tempfile, times, unittest, untar import cliutils, httpclient, json, os, osproc, sequtils, strutils, tempfile,
times, unittest, untar
from langutils import sameContents from langutils import sameContents
import ../testutil import ../testutil
import ../../../main/nim/strawbosspkg/configuration import ../../../main/nim/strawbosspkg/configuration
import ../../../main/nim/strawbosspkg/private/util
let apiBase = "http://localhost:8180/api" let apiBase = "http://localhost:8180/api"
let cfgFilePath = "src/test/json/strawboss.config.json" let cfgFilePath = "src/test/json/strawboss.config.json"

View File

@ -1,12 +1,11 @@
import asyncdispatch, httpclient, json, os, osproc, sequtils, strutils, import asyncdispatch, cliutils, httpclient, json, os, osproc, sequtils,
times, unittest strutils, times, unittest
from langutils import sameContents from langutils import sameContents
import ../testutil import ../testutil
import ../../../main/nim/strawbosspkg/configuration import ../../../main/nim/strawbosspkg/configuration
import ../../../main/nim/strawbosspkg/server import ../../../main/nim/strawbosspkg/server
import ../../../main/nim/strawbosspkg/private/util
let apiBase = "http://localhost:8180/api" let apiBase = "http://localhost:8180/api"
let cfgFilePath = "src/test/json/strawboss.config.json" let cfgFilePath = "src/test/json/strawboss.config.json"

View File

@ -9,11 +9,12 @@ srcDir = "src/main/nim"
# Dependencies # Dependencies
requires @["nim >= 0.16.1", "docopt >= 0.1.0", "tempfile", "jester", "bcrypt", requires @["nim >= 0.16.1", "docopt >= 0.6.5", "isaac >= 0.1.2", "tempfile", "jester", "bcrypt",
"untar", "uuids"] "untar", "uuids"]
requires "https://github.com/yglukhov/nim-jwt" requires "https://github.com/yglukhov/nim-jwt"
requires "https://git.jdb-labs.com/jdb/nim-lang-utils.git" requires "https://git.jdb-labs.com/jdb/nim-lang-utils.git"
requires "https://git.jdb-labs.com/jdb/nim-cli-utils.git"
# Tasks # Tasks
# #