Skip to content
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

refactor: reduce usage of autogenerated getters, part 2 #236

Merged
merged 4 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions src/attestation_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ proc writeInToto(info: DockerInvocation,
raise newException(OSError, "could not write toto to file: " & getCurrentExceptionMsg())

let
log = $(chalkConfig.getUseTransparencyLog())
log = $(get[bool](chalkConfig, "use_transparency_log"))
args = @["attest", ("--tlog-upload=" & log), "--yes", "--key",
"chalk.key", "--type", "custom", "--predicate", path,
digestStr]
Expand Down Expand Up @@ -242,7 +242,7 @@ proc coreVerify(key: AttestationKey, chalk: ChalkObj): bool =
## Used both for validation, and for downloading just the signature
## after we've signed.
const fName = "chalk.pub"
let noTlog = not chalkConfig.getUseTransparencyLog()
let noTlog = not get[bool](chalkConfig, "use_transparency_log")

key.withCosignKey:
let
Expand Down Expand Up @@ -356,7 +356,7 @@ proc willSignNonContainer*(chalk: ChalkObj): string =
return ""

# We sign non-container artifacts if either condition is true.
if not (isSubscribedKey("SIGNATURE") or chalkConfig.getAlwaysTryToSign()):
if not (isSubscribedKey("SIGNATURE") or get[bool](chalkConfig, "always_try_to_sign")):
trace("File artifact signing not configured.")
return ""

Expand All @@ -374,7 +374,7 @@ proc willSignNonContainer*(chalk: ChalkObj): string =
proc signNonContainer*(chalk: ChalkObj, unchalkedMD, metadataMD : string):
string =
let
log = $(chalkConfig.getUseTransparencyLog())
log = $(get[bool](chalkConfig, "use_transparency_log"))
args = @["sign-blob", ("--tlog-upload=" & log), "--yes", "--key",
"chalk.key", "-"]
blob = unchalkedMD & metadataMD
Expand All @@ -394,7 +394,7 @@ proc cosignNonContainerVerify*(chalk: ChalkObj,
artHash, mdHash, sig, pk: string):
ValidateResult =
let
log = $(not chalkConfig.getUseTransparencyLog())
log = $(not get[bool](chalkConfig, "use_transparency_log"))
args = @["verify-blob",
"--insecure-ignore-tlog=" & log,
"--key=chalk.pub",
Expand Down
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
6 changes: 0 additions & 6 deletions src/config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,3 @@ proc getPluginConfig*(name: string): Option[PluginSpec] =

var autoHelp*: string = ""
proc getAutoHelp*(): string = autoHelp

proc get*[T](chalkConfig: ChalkConfig, fqn: string): T =
get[T](chalkConfig.`@@attrscope@@`, fqn)

proc getOpt*[T](chalkConfig: ChalkConfig, fqn: string): Option[T] =
getOpt[T](chalkConfig.`@@attrscope@@`, fqn)
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
Loading
Loading