Skip to content

Commit

Permalink
refactor: avoid c4autoconf getters: chalkConfig top level
Browse files Browse the repository at this point in the history
  • Loading branch information
ee7 committed Mar 28, 2024
1 parent d5bc6c1 commit fe97d3a
Show file tree
Hide file tree
Showing 21 changed files with 72 additions and 72 deletions.
8 changes: 4 additions & 4 deletions src/chalk.nim
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ when isMainModule:
setupDefaultLogConfigs() # src/sinks.nim
loadAttestation() # attestation.nim
case getCommandName() # config.nim
of "extract": runCmdExtract(chalkConfig.getArtifactSearchPath())
of "extract": runCmdExtract(get[seq[string]](chalkConfig, "artifact_search_path"))
of "extract.containers": runCmdExtractContainers()
of "extract.images": runCmdExtractImages()
of "extract.all": runCmdExtractAll(chalkConfig.getArtifactSearchPath())
of "insert": runCmdInsert(chalkConfig.getArtifactSearchPath())
of "delete": runCmdDelete(chalkConfig.getArtifactSearchPath())
of "extract.all": runCmdExtractAll(get[seq[string]](chalkConfig, "artifact_search_path"))
of "insert": runCmdInsert(get[seq[string]](chalkConfig, "artifact_search_path"))
of "delete": runCmdDelete(get[seq[string]](chalkConfig, "artifact_search_path"))
of "env": runCmdEnv()
of "dump": runCmdConfDump()
of "dump.params": runCmdConfDumpParams()
Expand Down
2 changes: 1 addition & 1 deletion src/chalk_common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ var
sshKeyscanExeLocation*: string = ""

template dumpExOnDebug*() =
if chalkConfig != nil and chalkConfig.getChalkDebug():
if chalkConfig != nil and get[bool](chalkConfig, "chalk_debug"):
let
msg = "" # "Handling exception (msg = " & getCurrentExceptionMsg() & ")\n"
tb = "Traceback (most recent call last)\n" &
Expand Down
8 changes: 4 additions & 4 deletions src/collect.nim
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ proc ignoreArtifact(path: string, regexps: seq[Regex]): bool {.inline.} =
for i, item in regexps:
if path.match(item):
trace(path & ": returned artifact ignored due to matching: " &
chalkConfig.getIgnorePatterns()[i])
get[seq[string]](chalkConfig, "ignore_patterns")[i])
trace("Developers: codecs should not be returning ignored artifacts.")
return true

Expand All @@ -256,7 +256,7 @@ proc artSetupForExtract(argv: seq[string]): ArtifactIterationInfo =
let selfPath = resolvePath(getMyAppPath())

result.fileExclusions = @[selfPath]
result.recurse = chalkConfig.getRecursive()
result.recurse = get[bool](chalkConfig, "recursive")

for item in argv:
let maybe = resolvePath(item)
Expand All @@ -273,10 +273,10 @@ proc artSetupForInsertAndDelete(argv: seq[string]): ArtifactIterationInfo =

let
selfPath = resolvePath(getMyAppPath())
skipList = chalkConfig.getIgnorePatterns()
skipList = get[seq[string]](chalkConfig, "ignore_patterns")

result.fileExclusions = @[selfPath]
result.recurse = chalkConfig.getRecursive()
result.recurse = get[bool](chalkConfig, "recursive")

for item in skipList:
result.skips.add(re(item))
Expand Down
6 changes: 3 additions & 3 deletions src/commands/cmd_docker.nim
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ proc runBuild(ctx: DockerInvocation): int =
ctx.addBackBuildWithoutPushFlags()
else:
ctx.addBackBuildWithPushFlags()
if chalkConfig.getChalkContainedItems():
if get[bool](chalkConfig, "chalk_contained_items"):
info("Docker is starting a recursive chalk of context directories.")
var contexts: seq[string] = @[ctx.foundContext]

Expand All @@ -494,7 +494,7 @@ proc runBuild(ctx: DockerInvocation): int =
trace("Creating chalk mark.")
let chalkMark = chalk.getChalkMarkAsStr()

if chalkConfig.getVirtualChalk():
if get[bool](chalkConfig, "virtual_chalk"):
ctx.prepVirtualInsertion()
else:
ctx.handleTrueInsertion(chalkMark)
Expand All @@ -503,7 +503,7 @@ proc runBuild(ctx: DockerInvocation): int =

result = ctx.runMungedDockerInvocation()

if chalkConfig.getVirtualChalk() and result == 0:
if get[bool](chalkConfig, "virtual_chalk") and result == 0:
publish("virtual", chalkMark)

chalk.marked = true
Expand Down
8 changes: 4 additions & 4 deletions src/commands/cmd_help.nim
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ proc runChalkHelp*(cmdName = "help") {.noreturn.} =
# see if the command was explicitly passed, or if it was implicit.
# If it was implicit, give the help overview instead of the command
# overview.
let defaultCmd = chalkConfig.getDefaultCommand().getOrElse("")
let defaultCmd = getOpt[string](chalkConfig, "default_command").get("")
if defaultCmd != "" and defaultCmd notin commandLineParams():
toOut = con4mRuntime.getHelpOverview()
else:
Expand Down Expand Up @@ -543,7 +543,7 @@ proc runChalkHelp*(cmdName = "help") {.noreturn.} =
toOut = con4mRuntime.fullTextSearch(args)
break

if chalkConfig.getUsePager():
if get[bool](chalkConfig, "use_pager"):
runPager($(toOut))
else:
print(toOut)
Expand Down Expand Up @@ -643,11 +643,11 @@ proc getConfigValues(): Rope =

proc showConfigValues*(force = false) =
once:
if not (chalkConfig.getShowConfig() or force): return
if not (get[bool](chalkConfig, "show_config") or force): return

let toOut = getConfigValues()

if chalkConfig.getUsePager():
if get[bool](chalkConfig, "use_pager"):
runPager($(toOut))
else:
print(toOut)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/cmd_insert.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import ".."/[config, collect, reporting, chalkjson, plugin_api]
proc runCmdInsert*(path: seq[string]) {.exportc,cdecl.} =
setContextDirectories(path)
initCollection()
let virtual = chalkConfig.getVirtualChalk()
let virtual = get[bool](chalkConfig, "virtual_chalk")

for item in artifacts(path):
trace(item.name & ": begin chalking")
Expand Down
4 changes: 2 additions & 2 deletions src/confload.nim
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,12 @@ proc loadAllConfigs*() =
# The embedded config has already been validated.
let configFile = getEmbeddedConfig()

if chalkConfig.getLoadEmbeddedConfig():
if get[bool](chalkConfig, "load_embedded_config"):
stack.addConfLoad(embeddedConfName, toStream(configFile)).
addCallback(loadLocalStructs)
doRun()

if chalkConfig.getLoadExternalConfig():
if get[bool](chalkConfig, "load_external_config"):
let optConf = stack.configState.findOptionalConf()
if optConf.isSome():
let fName = optConf.get()
Expand Down
2 changes: 1 addition & 1 deletion src/docker_base.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ template extractBoxedDockerHash*(value: Box): Box =
proc getDockerExeLocation*(): string =
once:
let
dockerConfigPath = chalkConfig.getDockerExe()
dockerConfigPath = getOpt[string](chalkConfig, "docker_exe")
dockerExeOpt = findExePath("docker",
configPath = dockerConfigPath,
ignoreChalkExes = true)
Expand Down
2 changes: 1 addition & 1 deletion src/normalize.nim
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ proc normalizeChalk*(dict: ChalkDict): string =
# and SIGN_PARAMS.

var fieldCount = 0
let ignoreList = chalkConfig.getIgnoreWhenNormalizing()
let ignoreList = get[seq[string]](chalkConfig, "ignore_when_normalizing")

# Count how many fields we will write.
for key, _ in dict:
Expand Down
4 changes: 2 additions & 2 deletions src/plugin_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ proc mustIgnore(path: string, regexes: seq[Regex]): bool {.inline.} =
if path.match(item):
once:
trace(path & ": ignored due to matching ignore pattern: " &
chalkConfig.getIgnorePatterns()[i])
get[seq[string]](chalkConfig, "ignore_patterns")[i])
trace("We will NOT report additional path skips.")
return true

Expand All @@ -441,7 +441,7 @@ proc scanArtifactLocations*(self: Plugin, state: ArtifactIterationInfo):
followFLinks = false

if isChalkingOp():
let symLinkBehavior = chalkConfig.getSymlinkBehavior()
let symLinkBehavior = get[string](chalkConfig, "symlink_behavior")
if symLinkBehavior == "skip":
skipLinks = true
elif symLinkBehavior == "clobber":
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/codecPythonPyc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ proc pycScan*(self: Plugin, loc: string): Option[ChalkObj] {.cdecl.} =
# if so chalk it, else skip
#TODO validate PYC header / magic ?

if not ext.startsWith(".") or ext[1..^1] notin chalkConfig.getPycExtensions():
if not ext.startsWith(".") or ext[1..^1] notin get[seq[string]](chalkConfig, "pyc_extensions"):
return none(ChalkObj)

withFileStream(loc, mode = fmRead, strict = false):
Expand Down
10 changes: 5 additions & 5 deletions src/plugins/codecZip.nim
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ proc zipScan*(self: Plugin, loc: string): Option[ChalkObj] {.cdecl.} =
ext = loc.splitFile().ext.strip()
extractCtx: CollectionCtx

if not ext.startsWith(".") or ext[1..^1] notin chalkConfig.getZipExtensions():
if not ext.startsWith(".") or ext[1..^1] notin get[seq[string]](chalkConfig, "zip_extensions"):
return none(ChalkObj)

withFileStream(loc, mode = fmRead, strict = false):
Expand All @@ -93,7 +93,7 @@ proc zipScan*(self: Plugin, loc: string): Option[ChalkObj] {.cdecl.} =
cache = ZipCache()
origD = tmpDir.joinPath("contents")
hashD = tmpDir.joinPath("hash")
subscans = chalkConfig.getChalkContainedItems()
subscans = get[bool](chalkConfig, "chalk_contained_items")
chalk = newChalk(name = loc,
cache = cache,
fsRef = loc,
Expand All @@ -109,10 +109,10 @@ proc zipScan*(self: Plugin, loc: string): Option[ChalkObj] {.cdecl.} =
cache.onDisk.extractAll(hashD)

# Even if subscans are off, we do this delete for the purposes of hashing.
if not chalkConfig.getChalkDebug():
if not get[bool](chalkConfig, "chalk_debug"):
toggleLoggingEnabled()
discard runChalkSubScan(hashD, "delete")
if not chalkConfig.getChalkDebug():
if not get[bool](chalkConfig, "chalk_debug"):
toggleLoggingEnabled()

if zipChalkFile in cache.onDisk.contents:
Expand Down Expand Up @@ -215,7 +215,7 @@ proc zipGetChalkTimeArtifactInfo*(self: Plugin, obj: ChalkObj):
let cache = ZipCache(obj.cache)
result = ChalkDict()

if chalkConfig.getChalkContainedItems() and cache.embeddedChalk.kind != MkObj:
if get[bool](chalkConfig, "chalk_contained_items") and cache.embeddedChalk.kind != MkObj:
result["EMBEDDED_CHALK"] = cache.embeddedChalk
result["EMBEDDED_TMPDIR"] = pack(cache.tmpDir)

Expand Down
4 changes: 2 additions & 2 deletions src/plugins/externalTool.nim
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ template toolBase(s: string) {.dirty.} =
var
toolInfo: Table[string, seq[(int, PIInfo)]]
let
runSbom = chalkConfig.getRunSbomTools()
runSast = chalkConfig.getRunSastTools()
runSbom = get[bool](chalkConfig, "run_sbom_tools")
runSast = get[bool](chalkConfig, "run_sast_tools")

# tools should only run during insert operations
if getCommandName() notin @["build", "insert"]:
Expand Down
10 changes: 5 additions & 5 deletions src/plugins/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ proc sysGetRunTimeArtifactInfo*(self: Plugin, obj: ChalkObj, ins: bool):
if isChalkingOp():
obj.applySubstitutions()
result.setIfNeeded("_OP_CHALKED_KEYS", toSeq(obj.getChalkMark().keys))
result.setIfNeeded("_VIRTUAL", chalkConfig.getVirtualChalk())
result.setIfNeeded("_VIRTUAL", get[bool](chalkConfig, "virtual_chalk"))
else:
case obj.validateMetaData()
of vOk:
Expand Down Expand Up @@ -196,10 +196,10 @@ proc getEnvDict(): Box =
once:
envdict = Con4mDict[string, string]()
let
always = chalkConfig.getEnvAlwaysShow()
never = chalkConfig.getEnvNeverShow()
redact = chalkConfig.getEnvRedact()
def = chalkConfig.getEnvDefaultAction()[0]
always = get[seq[string]](chalkConfig, "env_always_show")
never = get[seq[string]](chalkConfig, "env_never_show")
redact = get[seq[string]](chalkConfig, "env_redact")
def = get[string](chalkConfig, "env_default_action")[0]

for (k, v) in envPairs():
# TODO: could add some con4m to warn on overlap across these 3. For now,
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/techStackGeneric.nim
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ proc getLanguages(directory: string, langs: var HashSet[string]) =
proc detectLanguages(): HashSet[string] =
result = initHashSet[string]()

let canLoad = chalkConfig.getUseTechStackDetection()
let canLoad = get[bool](chalkConfig, "use_tech_stack_detection")
if not canLoad:
return result

Expand Down Expand Up @@ -302,7 +302,7 @@ proc loadState() =

proc techStackRuntime*(self: Plugin, objs: seq[ChalkObj]): ChalkDict {.cdecl.} =
result = ChalkDict()
let canLoad = chalkConfig.getUseTechStackDetection()
let canLoad = get[bool](chalkConfig, "use_tech_stack_detection")
if not canLoad:
trace("Skipping tech stack runtime detection plugin")
return result
Expand Down Expand Up @@ -330,7 +330,7 @@ proc techStackRuntime*(self: Plugin, objs: seq[ChalkObj]): ChalkDict {.cdecl.} =

proc techStackArtifact*(self: Plugin, objs: ChalkObj): ChalkDict {.cdecl.} =
result = ChalkDict()
let canLoad = chalkConfig.getUseTechStackDetection()
let canLoad = get[bool](chalkConfig, "use_tech_stack_detection")
if not canLoad:
trace("Skipping tech stack detection plugin for artifacts")
return result
Expand Down
16 changes: 8 additions & 8 deletions src/reportcache.nim
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ template tracePublish(topic, m: string, prevSuccesses = false) =
" subscribers)")

if n == 0 and startSubscriptions != 0:
if chalkConfig.getUseReportCache():
if get[bool](chalkConfig, "use_report_cache"):
error("For topic '" & topic & "': No output config is working, but " &
"failures will be stored in the report cache")
else:
error("For topic '" & topic & "': ")
error("No output configuration is working for this report, and there " &
"is no report cache configured, so no metadata was recorded.")

if chalkConfig.getForceOutputOnReportingFails():
if get[bool](chalkConfig, "force_output_on_reporting_fails"):
error("Here is the orphaned report:")
doPanicWrite(msg)
error("Run with --use-report-cache to automatically buffer failures " &
Expand All @@ -129,7 +129,7 @@ template tracePublish(topic, m: string, prevSuccesses = false) =
error("Re-run with --force-output to try again, getting a report " &
"on the console if io config fails again.")
elif n != startSubscriptions:
if chalkConfig.getUseReportCache():
if get[bool](chalkConfig, "use_report_cache"):
error("For topic '" & topic & "': publish failures will be cached.")
else:
error("No report cache is enabled; sink configs with failures will " &
Expand All @@ -141,7 +141,7 @@ proc loadReportCache(fname: string) =
once:
try:
let
retries = chalkConfig.getReportCacheLockTimeoutSec()
retries = get[int](chalkConfig, "report_cache_lock_timeout_sec")
lines = readViaLockFile(fname).strip().split("\n")
for line in lines:
let
Expand Down Expand Up @@ -319,7 +319,7 @@ proc panicPublish(contents, tmpfilename, targetname, err: string) =
doPanicWrite(s)

proc safePublish*(topic, msg: string) =
if not chalkConfig.getUseReportCache():
if not get[bool](chalkConfig, "use_report_cache"):
tracePublish(topic, msg)
return

Expand All @@ -332,7 +332,7 @@ proc safePublish*(topic, msg: string) =

sinkErrors = @[]

let fname = resolvePath(chalkConfig.getReportCacheLocation())
let fname = resolvePath(get[string](chalkConfig, "report_cache_location"))
loadReportCache(fname)

if reportCache.len() != 0:
Expand All @@ -357,15 +357,15 @@ proc writeReportCache*() =
# Everything else we might have published was probably mainly intended
# for the console.

if not chalkConfig.getUseReportCache():
if not get[bool](chalkConfig, "use_report_cache"):
return

# If nothing published, the reporting cache may not have been loaded, in
# which case there's nothing do do.
if cacheOpenFailed and len(reportCache) != 0:
error(msgPossibleLoss)

let fname = resolvePath(chalkConfig.getReportCacheLocation())
let fname = resolvePath(get[string](chalkConfig, "report_cache_location"))

if not dirtyCache and len(reportCache) != 0:
warn("Report cache contains unreported message(s); Cached entries " &
Expand Down
4 changes: 2 additions & 2 deletions src/reporting.nim
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ proc doCommandReport(): string {.inline.} =
reportTemplate = getReportTemplate()
# The above goes from the string name to the object.

if chalkConfig.getSkipCommandReport():
if get[bool](chalkConfig, "skip_command_report"):
info("Skipping the command report as per the `skip_command_report` directive")
result = ""
else:
Expand Down Expand Up @@ -115,7 +115,7 @@ template doCustomReporting() =

if getCommandName() notin spec.useWhen and "*" notin spec.useWhen:
continue
if topic == "audit" and not chalkConfig.getPublishAudit():
if topic == "audit" and not get[bool](chalkConfig, "publish_audit"):
continue
if len(sinkConfs) == 0 and topic notin ["audit", "chalk_usage_stats"]:
warn("Report '" & topic & "' has no configured sinks. Skipping.")
Expand Down
2 changes: 1 addition & 1 deletion src/run_management.nim
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ template setIfNeeded*[T](o: ChalkObj, k: string, v: T) =
setIfNeeded(o.collectedData, k, v)

proc isChalkingOp*(): bool =
return commandName in chalkConfig.getValidChalkCommandNames()
return commandName in get[seq[string]](chalkConfig, "valid_chalk_command_names")

proc lookupByPath*(obj: ChalkDict, path: string): Option[Box] =
let
Expand Down
Loading

0 comments on commit fe97d3a

Please sign in to comment.