From 7e35a571fb3c5b631a195852657c4bfad38c2b36 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Mon, 19 Aug 2024 16:42:27 +0200 Subject: [PATCH] refactor: remove most `getChalkScope` (#395) Previous preparation for the new con4m culminated in nearly every get and set occurring from the top level [1]. Refactor so that helper procs always get and set from the top level, so that we generally no longer write `getChalkScope()` when getting or setting. Before this commit, there was a lot of `getChalkScope()` noise: $ git grep --ignore-case 'getChalkScope' -- '*.nim' | wc -l 266 With this commit, there's much less: $ git grep --ignore-case 'getChalkScope' -- '*.nim' | wc -l 14 $ git grep --break --heading --ignore-case 'getChalkScope' -- '*.nim' src/chalk_common.nim 484: if getChalkScope() != nil and attrGet[bool]("chalk_debug"): src/confload.nim 109: if tb != "" and getChalkScope() == nil or attrGet[bool]("chalk_debug"): src/run_management.nim 20:proc getChalkScope*(): AttrScope = 24: getChalkScope().getObjectOpt(s).isSome() 27: get[T](getChalkScope(), fqn) 30: getOpt[T](getChalkScope(), fqn) 33: getObject(getChalkScope(), fqn) 55: doAssert attrSet(getChalkScope(), fqn, value, attrType).code == errOk 58: discard attrLookup(getChalkScope(), fqn.split('.'), ix = 0, op = vlSecDef) src/selfextract.nim 153: if getChalkScope() != nil and attrGet[bool]("chalk_debug"): src/sinks.nim 47: if (llStr in toLogLevelMap) and getChalkScope() != nil and 153: if getChalkScope() != nil and attrGet[bool]("chalk_debug"): 185: attrRoot = if attr != nil: attr else: getChalkScope() 273: discard setOverride(getChalkScope(), section & ".pinned_cert_file", some(pack(path))) This commit squashes the below refactors: - Replace top-level get with attrGet. - Replace top-level getOpt with attrGetOpt. - Replace top-level getObject with attrGetObject. - run_management: make getChalkSubsections use attrGetObject. - conffile: replace a top-level attrLookup with attrGetOpt. - Make sectionExists work from top level. And for now, inline the use of sectionExists in sinks.nim that wasn't necessarily from the top level. - Make con4mAttrSet work from top level. - config, run_management: add a con4mSectionCreate helper proc. Refs: https://github.com/crashappsec/chalk/issues/269 [1] https://github.com/crashappsec/chalk/commit/6ce76790a1901b6f81c32f40e8ee6503a88b8274 --- src/attestation/backup.nim | 8 ++-- src/attestation/embed.nim | 2 +- src/attestation/get.nim | 6 +-- src/attestation_api.nim | 12 +++--- src/chalk.nim | 8 ++-- src/chalk_common.nim | 2 +- src/chalkjson.nim | 10 ++--- src/collect.nim | 64 ++++++++++++++++---------------- src/commands/cmd_exec.nim | 28 +++++++------- src/commands/cmd_help.nim | 20 +++++----- src/commands/cmd_insert.nim | 2 +- src/config.nim | 36 ++++++------------ src/confload.nim | 18 ++++----- src/docker/base.nim | 2 +- src/docker/build.nim | 16 ++++---- src/docker/entrypoint.nim | 4 +- src/docker/exe.nim | 2 +- src/docker/json.nim | 4 +- src/docker/platform.nim | 10 ++--- src/docker/util.nim | 4 +- src/docker/wrap.nim | 4 +- src/normalize.nim | 2 +- src/plugin_api.nim | 14 +++---- src/plugins/cloudMetadata.nim | 8 ++-- src/plugins/codecPythonPyc.nim | 2 +- src/plugins/codecSource.nim | 14 +++---- src/plugins/codecZip.nim | 10 ++--- src/plugins/conffile.nim | 10 ++--- src/plugins/externalTool.nim | 22 +++++------ src/plugins/system.nim | 16 ++++---- src/plugins/techStackGeneric.nim | 45 +++++++++++----------- src/plugins/vctlGit.nim | 2 +- src/reportcache.nim | 16 ++++---- src/reporting.nim | 16 ++++---- src/run_management.nim | 30 ++++++++++----- src/selfextract.nim | 18 ++++----- src/sinks.nim | 62 +++++++++++++++---------------- src/subscan.nim | 4 +- src/util.nim | 14 +++---- 39 files changed, 283 insertions(+), 284 deletions(-) diff --git a/src/attestation/backup.nim b/src/attestation/backup.nim index d611a09a..e2b7e35e 100644 --- a/src/attestation/backup.nim +++ b/src/attestation/backup.nim @@ -117,11 +117,11 @@ proc restore(self: Backup, proc initCallback(this: AttestationKeyProvider) = let self = Backup(this) - authName = get[string](getChalkScope(), "attestation.attestation_key_backup.auth") - location = get[string](getChalkScope(), "attestation.attestation_key_backup.location") + authName = attrGet[string]("attestation.attestation_key_backup.auth") + location = attrGet[string]("attestation.attestation_key_backup.location") authOpt = getAuthConfigByName(authName) - url = get[string](getChalkScope(), "attestation.attestation_key_backup.uri").removeSuffix("/") - timeout = cast[int](get[Con4mDuration](getChalkScope(), "attestation.attestation_key_backup.timeout")) + url = attrGet[string]("attestation.attestation_key_backup.uri").removeSuffix("/") + timeout = cast[int](attrGet[Con4mDuration]("attestation.attestation_key_backup.timeout")) if authOpt.isNone(): raise newException(ValueError, diff --git a/src/attestation/embed.nim b/src/attestation/embed.nim index a925482d..8a5cc009 100644 --- a/src/attestation/embed.nim +++ b/src/attestation/embed.nim @@ -14,7 +14,7 @@ type Embed = ref object of AttestationKeyProvider proc initCallback(this: AttestationKeyProvider) = let self = Embed(this) - location = get[string](getChalkScope(), "attestation.attestation_key_embed.location") + location = attrGet[string]("attestation.attestation_key_embed.location") self.location = location proc generateKeyCallback(this: AttestationKeyProvider): AttestationKey = diff --git a/src/attestation/get.nim b/src/attestation/get.nim index 8c1b2afb..df2103fb 100644 --- a/src/attestation/get.nim +++ b/src/attestation/get.nim @@ -49,10 +49,10 @@ proc request(self: Get, query = ""): JsonNode = proc initCallback(this: AttestationKeyProvider) = let self = Get(this) - authName = get[string](getChalkScope(), "attestation.attestation_key_get.auth") + authName = attrGet[string]("attestation.attestation_key_get.auth") authOpt = getAuthConfigByName(authName) - url = get[string](getChalkScope(), "attestation.attestation_key_get.uri").removeSuffix("/") - timeout = cast[int](get[Con4mDuration](getChalkScope(), "attestation.attestation_key_get.timeout")) + url = attrGet[string]("attestation.attestation_key_get.uri").removeSuffix("/") + timeout = cast[int](attrGet[Con4mDuration]("attestation.attestation_key_get.timeout")) if authOpt.isNone(): raise newException(ValueError, diff --git a/src/attestation_api.nim b/src/attestation_api.nim index 3a3f9f42..0d7a47b0 100644 --- a/src/attestation_api.nim +++ b/src/attestation_api.nim @@ -26,7 +26,7 @@ proc getCosignKey(chalk: ChalkObj): AttestationKey = return AttestationKey(publicKey: pubKey) proc getProvider(): AttestationKeyProvider = - let name = get[string](getChalkScope(), "attestation.key_provider") + let name = attrGet[string]("attestation.key_provider") if name notin keyProviders: raise newException(KeyError, "Unsupported attestation key provider: " & name) return keyProviders[name] @@ -165,7 +165,7 @@ proc willSign(): bool = if not cosignKey.canAttest(): return false # We sign artifacts if either condition is true. - if isSubscribedKey("SIGNATURE") or get[bool](getChalkScope(), "always_try_to_sign"): + if isSubscribedKey("SIGNATURE") or attrGet[bool]("always_try_to_sign"): return true trace("Artifact signing not configured.") return false @@ -182,7 +182,7 @@ proc willSignBySigStore*(chalk: ChalkObj): bool = proc verifyBySigStore(chalk: ChalkObj, key: AttestationKey, image: DockerImage): (ValidateResult, ChalkDict) = let spec = image.withDigest(chalk.imageDigest).asRepoDigest() - log = get[bool](getChalkScope(), "use_transparency_log") + log = attrGet[bool]("use_transparency_log") cosign = getCosignLocation() var dict = ChalkDict() @@ -255,7 +255,7 @@ proc signBySigStore*(chalk: ChalkObj): ChalkDict = name = image.repo spec = image.withDigest(chalk.imageDigest).asRepoDigest() mark = chalk.getChalkMarkAsStr() - log = get[bool](getChalkScope(), "use_transparency_log") + log = attrGet[bool]("use_transparency_log") cosign = getCosignLocation() args = @["attest", "--tlog-upload=" & $log, @@ -316,7 +316,7 @@ proc signByHash*(chalk: ChalkObj, mdHash : string): ChalkDict = "No hash available for this artifact at time of signing." ) let - log = get[bool](getChalkScope(), "use_transparency_log") + log = attrGet[bool]("use_transparency_log") args = @["sign-blob", "--tlog-upload=" & $log, "--yes", @@ -366,7 +366,7 @@ proc verifyByHash*(chalk: ChalkObj, mdHash: string): ValidateResult = let sig = unpack[string](chalk.extract["SIGNATURE"]) - noTlog = not get[bool](getChalkScope(), "use_transparency_log") + noTlog = not attrGet[bool]("use_transparency_log") args = @["verify-blob", "--insecure-ignore-tlog=" & $noTlog, "--key=chalk.pub", diff --git a/src/chalk.nim b/src/chalk.nim index d7012fcf..c89ffb6c 100644 --- a/src/chalk.nim +++ b/src/chalk.nim @@ -27,12 +27,12 @@ when isMainModule: setupDefaultLogConfigs() # src/sinks.nim loadAttestation() # attestation.nim case getCommandName() # config.nim - of "extract": runCmdExtract(get[seq[string]](getChalkScope(), "artifact_search_path")) + of "extract": runCmdExtract(attrGet[seq[string]]("artifact_search_path")) of "extract.containers": runCmdExtractContainers() of "extract.images": runCmdExtractImages() - of "extract.all": runCmdExtractAll(get[seq[string]](getChalkScope(), "artifact_search_path")) - of "insert": runCmdInsert(get[seq[string]](getChalkScope(), "artifact_search_path")) - of "delete": runCmdDelete(get[seq[string]](getChalkScope(), "artifact_search_path")) + of "extract.all": runCmdExtractAll(attrGet[seq[string]]("artifact_search_path")) + of "insert": runCmdInsert(attrGet[seq[string]]("artifact_search_path")) + of "delete": runCmdDelete(attrGet[seq[string]]("artifact_search_path")) of "env": runCmdEnv() of "dump": runCmdConfDump() of "dump.params": runCmdConfDumpParams() diff --git a/src/chalk_common.nim b/src/chalk_common.nim index c679c7ed..0a2d5aaa 100644 --- a/src/chalk_common.nim +++ b/src/chalk_common.nim @@ -481,7 +481,7 @@ var template dumpExOnDebug*() = when not defined(release): let stack = getCurrentException().getStackTrace() - if getChalkScope() != nil and get[bool](getChalkScope(), "chalk_debug"): + if getChalkScope() != nil and attrGet[bool]("chalk_debug"): let msg = "" # "Handling exception (msg = " & getCurrentExceptionMsg() & ")\n" tb = "Traceback (most recent call last)\n" & stack diff --git a/src/chalkjson.nim b/src/chalkjson.nim index cb3a3076..ef37d66d 100644 --- a/src/chalkjson.nim +++ b/src/chalkjson.nim @@ -445,9 +445,9 @@ proc orderKeys*(dict: ChalkDict, tplate: string): seq[string] = var tmp: seq[(int, string)] = @[] for k, _ in dict: - var order = get[int](getChalkScope(), "keyspec." & k & ".normalized_order") - if tplate != "" and sectionExists(getChalkScope(), tplate & ".key." & k): - let orderOpt = getOpt[int](getChalkScope(), tplate & ".key." & k & ".order") + var order = attrGet[int]("keyspec." & k & ".normalized_order") + if tplate != "" and sectionExists(tplate & ".key." & k): + let orderOpt = attrGetOpt[int](tplate & ".key." & k & ".order") if orderOpt.isSome(): order = orderOpt.get() tmp.add((order, k)) @@ -492,7 +492,7 @@ proc forcePrivateKeys() = forceChalkKeys(toForce) proc getChalkMark*(obj: ChalkObj): ChalkDict = - trace("Creating mark using template: " & get[string](getChalkScope(), getOutputConfig() & ".mark_template")) + trace("Creating mark using template: " & attrGet[string](getOutputConfig() & ".mark_template")) forcePrivateKeys() @@ -512,7 +512,7 @@ proc getChalkMarkAsStr*(obj: ChalkObj): string = trace("Chalk cachemark " & $obj.cachedMark) return obj.cachedMark trace("Converting Mark to JSON. Mark template is: " & - get[string](getChalkScope(), getOutputConfig() & ".mark_template")) + attrGet[string](getOutputConfig() & ".mark_template")) if obj.cachedMark != "": return obj.cachedMark diff --git a/src/collect.nim b/src/collect.nim index 7c408128..d8044643 100644 --- a/src/collect.nim +++ b/src/collect.nim @@ -16,9 +16,9 @@ proc hasSubscribedKey(p: Plugin, keys: seq[string], dict: ChalkDict): bool = # Decides whether to run a given plugin... does it export any key we # are subscribed to, that hasn't already been provided? for k in keys: - if k in get[seq[string]](getChalkScope(), "plugin." & p.name & ".ignore"): continue + if k in attrGet[seq[string]]("plugin." & p.name & ".ignore"): continue if k notin subscribedKeys and k != "*": continue - if k in get[seq[string]](getChalkScope(), "plugin." & p.name & ".overrides"): return true + if k in attrGet[seq[string]]("plugin." & p.name & ".overrides"): return true if k notin dict: return true return false @@ -28,12 +28,12 @@ proc canWrite(plugin: Plugin, key: string, decls: seq[string]): bool = # except that we do allow "*" fields for plugins, so we need the runtime # check to filter out inappropriate items. let section = "keyspec." & key - doAssert sectionExists(getChalkScope(), section) + doAssert sectionExists(section) - if key in get[seq[string]](getChalkScope(), "plugin." & plugin.name & ".ignore"): return false + if key in attrGet[seq[string]]("plugin." & plugin.name & ".ignore"): return false - if get[bool](getChalkScope(), section & ".codec"): - if get[bool](getChalkScope(), "plugin." & plugin.name & ".codec"): + if attrGet[bool](section & ".codec"): + if attrGet[bool]("plugin." & plugin.name & ".codec"): return true else: error("Plugin '" & plugin.name & "' can't write codec key: '" & key & "'") @@ -42,14 +42,14 @@ proc canWrite(plugin: Plugin, key: string, decls: seq[string]): bool = if key notin decls and "*" notin decls: error("Plugin '" & plugin.name & "' produced undeclared key: '" & key & "'") return false - if not get[bool](getChalkScope(), section & ".system"): + if not attrGet[bool](section & ".system"): return true case plugin.name of "system", "metsys": return true of "conffile": - if get[bool](getChalkScope(), section & ".conf_as_system"): + if attrGet[bool](section & ".conf_as_system"): return true else: discard @@ -58,10 +58,10 @@ proc canWrite(plugin: Plugin, key: string, decls: seq[string]): bool = proc registerKeys(templ: string) = let section = templ & ".key" - if sectionExists(getChalkScope(), section): + if sectionExists(section): for name in getChalkSubsections(section): let content = section & "." & name - let useOpt = getOpt[bool](getChalkScope(), content & ".use") + let useOpt = attrGetOpt[bool](content & ".use") if useOpt.isSome() and useOpt.get(): subscribedKeys[name] = true @@ -77,11 +77,11 @@ proc registerOutconfKeys() = let outconf = getOutputConfig() - let markTemplate = get[string](getChalkScope(), outconf & ".mark_template") + let markTemplate = attrGet[string](outconf & ".mark_template") if markTemplate != "": registerKeys("mark_template." & markTemplate) - let reportTemplate = get[string](getChalkScope(), outconf & ".report_template") + let reportTemplate = attrGet[string](outconf & ".report_template") if reportTemplate != "": registerKeys("report_template." & reportTemplate) @@ -91,7 +91,7 @@ proc collectChalkTimeHostInfo*() = trace("Collecting chalk time artifact info") for plugin in getAllPlugins(): - let subscribed = get[seq[string]](getChalkScope(), "plugin." & plugin.name & ".pre_run_keys") + let subscribed = attrGet[seq[string]]("plugin." & plugin.name & ".pre_run_keys") if chalkCollectionSuspendedFor(plugin.name): continue if not plugin.hasSubscribedKey(subscribed, hostInfo): continue try: @@ -101,10 +101,10 @@ proc collectChalkTimeHostInfo*() = continue for k, v in dict: - if not plugin.canWrite(k, get[seq[string]](getChalkScope(), "plugin." & plugin.name & ".pre_run_keys")): + if not plugin.canWrite(k, attrGet[seq[string]]("plugin." & plugin.name & ".pre_run_keys")): continue if k notin hostInfo or - k in get[seq[string]](getChalkScope(), "plugin." & plugin.name & ".overrides") or + k in attrGet[seq[string]]("plugin." & plugin.name & ".overrides") or plugin.isSystem(): hostInfo[k] = v except: @@ -128,13 +128,13 @@ proc initCollection*() = # Next, register for any custom reports. for name in getChalkSubsections("custom_report"): let report = "custom_report." & name - let useWhenOpt = getOpt[seq[string]](getChalkScope(), report & ".use_when") + let useWhenOpt = attrGetOpt[seq[string]](report & ".use_when") if useWhenOpt.isSome(): let useWhen = useWhenOpt.get() if (getBaseCommandName() notin useWhen and "*" notin useWhen): continue - let templNameOpt = getOpt[string](getChalkScope(), report & ".report_template") + let templNameOpt = attrGetOpt[string](report & ".report_template") if templNameOpt.isSome(): let templName = templNameOpt.get() if templName != "": @@ -148,20 +148,20 @@ proc collectRunTimeArtifactInfo*(artifact: ChalkObj) = for plugin in getAllPlugins(): let data = artifact.collectedData - subscribed = get[seq[string]](getChalkScope(), "plugin." & plugin.name & ".post_chalk_keys") + subscribed = attrGet[seq[string]]("plugin." & plugin.name & ".post_chalk_keys") if chalkCollectionSuspendedFor(plugin.name): continue if not plugin.hasSubscribedKey(subscribed, data): continue - if get[bool](getChalkScope(), "plugin." & plugin.name & ".codec") and plugin != artifact.myCodec: continue + if attrGet[bool]("plugin." & plugin.name & ".codec") and plugin != artifact.myCodec: continue trace("Running plugin: " & plugin.name) try: let dict = plugin.callGetRunTimeArtifactInfo(artifact, isChalkingOp()) if dict == nil or len(dict) == 0: continue for k, v in dict: - if not plugin.canWrite(k, get[seq[string]](getChalkScope(), "plugin." & plugin.name & ".post_chalk_keys")): continue + if not plugin.canWrite(k, attrGet[seq[string]]("plugin." & plugin.name & ".post_chalk_keys")): continue if k notin artifact.collectedData or - k in get[seq[string]](getChalkScope(), "plugin." & plugin.name & ".overrides") or + k in attrGet[seq[string]]("plugin." & plugin.name & ".overrides") or plugin.isSystem(): artifact.collectedData[k] = v trace(plugin.name & ": Plugin called.") @@ -198,9 +198,9 @@ proc collectChalkTimeArtifactInfo*(obj: ChalkObj, override = false) = if obj.fsRef != "": data["PATH_WHEN_CHALKED"] = pack(resolvePath(obj.fsRef)) - if get[bool](getChalkScope(), "plugin." & plugin.name & ".codec") and plugin != obj.myCodec: continue + if attrGet[bool]("plugin." & plugin.name & ".codec") and plugin != obj.myCodec: continue - let subscribed = get[seq[string]](getChalkScope(), "plugin." & plugin.name & ".pre_chalk_keys") + let subscribed = attrGet[seq[string]]("plugin." & plugin.name & ".pre_chalk_keys") if not plugin.hasSubscribedKey(subscribed, data) and not plugin.isSystem(): trace(plugin.name & ": Skipping plugin; its metadata wouldn't be used.") continue @@ -216,9 +216,9 @@ proc collectChalkTimeArtifactInfo*(obj: ChalkObj, override = false) = continue for k, v in dict: - if not plugin.canWrite(k, get[seq[string]](getChalkScope(), "plugin." & plugin.name & ".pre_chalk_keys")): continue + if not plugin.canWrite(k, attrGet[seq[string]]("plugin." & plugin.name & ".pre_chalk_keys")): continue if k notin obj.collectedData or - k in get[seq[string]](getChalkScope(), "plugin." & plugin.name & ".overrides") or + k in attrGet[seq[string]]("plugin." & plugin.name & ".overrides") or plugin.isSystem() or override: obj.collectedData[k] = v @@ -235,7 +235,7 @@ proc collectRunTimeHostInfo*() = ## artifact loop below. trace("Collecting run time host info") for plugin in getAllPlugins(): - let subscribed = get[seq[string]](getChalkScope(), "plugin." & plugin.name & ".post_run_keys") + let subscribed = attrGet[seq[string]]("plugin." & plugin.name & ".post_run_keys") if chalkCollectionSuspendedFor(plugin.name): continue if not plugin.hasSubscribedKey(subscribed, hostInfo): continue @@ -245,9 +245,9 @@ proc collectRunTimeHostInfo*() = if dict == nil or len(dict) == 0: continue for k, v in dict: - if not plugin.canWrite(k, get[seq[string]](getChalkScope(), "plugin." & plugin.name & ".post_run_keys")): continue + if not plugin.canWrite(k, attrGet[seq[string]]("plugin." & plugin.name & ".post_run_keys")): continue if k notin hostInfo or - k in get[seq[string]](getChalkScope(), "plugin." & plugin.name & ".overrides") or + k in attrGet[seq[string]]("plugin." & plugin.name & ".overrides") or plugin.isSystem(): hostInfo[k] = v except: @@ -271,7 +271,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: " & - get[seq[string]](getChalkScope(), "ignore_patterns")[i]) + attrGet[seq[string]]("ignore_patterns")[i]) trace("Developers: codecs should not be returning ignored artifacts.") return true @@ -283,7 +283,7 @@ proc artSetupForExtract(argv: seq[string]): ArtifactIterationInfo = let selfPath = resolvePath(getMyAppPath()) result.fileExclusions = @[selfPath] - result.recurse = get[bool](getChalkScope(), "recursive") + result.recurse = attrGet[bool]("recursive") for item in argv: let maybe = resolvePath(item) @@ -300,10 +300,10 @@ proc artSetupForInsertAndDelete(argv: seq[string]): ArtifactIterationInfo = let selfPath = resolvePath(getMyAppPath()) - skipList = get[seq[string]](getChalkScope(), "ignore_patterns") + skipList = attrGet[seq[string]]("ignore_patterns") result.fileExclusions = @[selfPath] - result.recurse = get[bool](getChalkScope(), "recursive") + result.recurse = attrGet[bool]("recursive") for item in skipList: result.skips.add(re(item)) diff --git a/src/commands/cmd_exec.nim b/src/commands/cmd_exec.nim index 4919b098..d26faf0b 100644 --- a/src/commands/cmd_exec.nim +++ b/src/commands/cmd_exec.nim @@ -163,7 +163,7 @@ proc doHeartbeatReport(chalkOpt: Option[ChalkObj]) = template doHeartbeat(chalkOpt: Option[ChalkObj], pid: Pid, fn: untyped) = let - inMicroSec = int(get[Con4mDuration](getChalkScope(), "exec.heartbeat_rate")) + inMicroSec = int(attrGet[Con4mDuration]("exec.heartbeat_rate")) sleepInterval = int(inMicroSec / 1000) setCommandName("heartbeat") @@ -187,16 +187,16 @@ proc runCmdExec*(args: seq[string]) = return let - fromArgs = get[bool](getChalkScope(), "exec.command_name_from_args") - cmdPath = get[seq[string]](getChalkScope(), "exec.search_path") - defaults = get[seq[string]](getChalkScope(), "exec.default_args") - appendArgs = get[bool](getChalkScope(), "exec.append_command_line_args") - overrideOk = get[bool](getChalkScope(), "exec.override_ok") - usePath = get[bool](getChalkScope(), "exec.use_path") - pct = get[int](getChalkScope(), "exec.reporting_probability") + fromArgs = attrGet[bool]("exec.command_name_from_args") + cmdPath = attrGet[seq[string]]("exec.search_path") + defaults = attrGet[seq[string]]("exec.default_args") + appendArgs = attrGet[bool]("exec.append_command_line_args") + overrideOk = attrGet[bool]("exec.override_ok") + usePath = attrGet[bool]("exec.use_path") + pct = attrGet[int]("exec.reporting_probability") ppid = getpid() # Get the current pid before we fork. var - cmdName = get[string](getChalkScope(), "exec.command_name") + cmdName = attrGet[string]("exec.command_name") var argsToPass = defaults @@ -238,7 +238,7 @@ proc runCmdExec*(args: seq[string]) = let pid = fork() - if get[bool](getChalkScope(), "exec.chalk_as_parent"): + if attrGet[bool]("exec.chalk_as_parent"): if pid == 0: handleExec(allOpts, argsToPass) elif pid == -1: @@ -253,7 +253,7 @@ proc runCmdExec*(args: seq[string]) = # # Yes this is also racy but a proper fix will be more complicated. let - inMicroSec = int(get[Con4mDuration](getChalkScope(), "exec.initial_sleep_time")) + inMicroSec = int(attrGet[Con4mDuration]("exec.initial_sleep_time")) initialSleep = int(inMicroSec / 1000) sleep(initialSleep) @@ -265,7 +265,7 @@ proc runCmdExec*(args: seq[string]) = chalkOpt.get().collectRunTimeArtifactInfo() doReporting() - if get[bool](getChalkScope(), "exec.heartbeat"): + if attrGet[bool]("exec.heartbeat"): chalkOpt.doHeartbeatAsParent(pid) else: trace("Waiting for spawned process to exit.") @@ -286,7 +286,7 @@ proc runCmdExec*(args: seq[string]) = trace("Chalk is child process: " & $(cpid)) let - inMicroSec = int(get[Con4mDuration](getChalkScope(), "exec.initial_sleep_time")) + inMicroSec = int(attrGet[Con4mDuration]("exec.initial_sleep_time")) initialSleep = int(inMicroSec / 1000) sleep(initialSleep) @@ -305,5 +305,5 @@ proc runCmdExec*(args: seq[string]) = chalkOpt.get().collectRunTimeArtifactInfo() doReporting() - if get[bool](getChalkScope(), "exec.heartbeat"): + if attrGet[bool]("exec.heartbeat"): chalkOpt.doHeartbeatAsChild(ppid) diff --git a/src/commands/cmd_help.nim b/src/commands/cmd_help.nim index 249d5a5a..c6e60212 100644 --- a/src/commands/cmd_help.nim +++ b/src/commands/cmd_help.nim @@ -344,12 +344,12 @@ proc formatOneTemplate(state: ConfigState, var keysToReport: seq[string] - let docOpt = getOpt[string](getChalkScope(), tmpl & ".doc") + let docOpt = attrGetOpt[string](tmpl & ".doc") let doc = if docOpt.isSome(): docOpt.get() else: "No description available." result = markdown(doc) for k in getChalkSubsections(tmpl & ".key"): - if get[bool](getChalkScope(), tmpl & ".key." & k & ".use") == true: + if attrGet[bool](tmpl & ".key." & k & ".use") == true: keysToReport.add(k) if len(keysToReport) == 0: @@ -382,13 +382,13 @@ See `chalk help reporting` for more information on templates. reportTemplates.add(k) else: for item in args: - if item notin getObject(getChalkScope(), "mark_template").getContents() and - item notin getObject(getChalkScope(), "report_template").getContents(): + if item notin attrGetObject("mark_template").getContents() and + item notin attrGetObject("report_template").getContents(): result += h3("No template found named: " & item ) else: - if item in getObject(getChalkScope(), "mark_template").getContents(): + if item in attrGetObject("mark_template").getContents(): markTemplates.add(item) - if item in getObject(getChalkScope(), "report_template").getContents(): + if item in attrGetObject("report_template").getContents(): reportTemplates.add(item) if len(markTemplates) + len(reportTemplates) == 0: @@ -485,7 +485,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 = getOpt[string](getChalkScope(), "default_command").get("") + let defaultCmd = attrGetOpt[string]("default_command").get("") if defaultCmd != "" and defaultCmd notin commandLineParams(): toOut = con4mRuntime.getHelpOverview() else: @@ -545,7 +545,7 @@ proc runChalkHelp*(cmdName = "help") {.noreturn.} = toOut = con4mRuntime.fullTextSearch(args) break - if get[bool](getChalkScope(), "use_pager"): + if attrGet[bool]("use_pager"): runPager($(toOut)) else: print(toOut) @@ -644,11 +644,11 @@ proc getConfigValues(): Rope = proc showConfigValues*(force = false) = once: - if not (get[bool](getChalkScope(), "show_config") or force): + if not (attrGet[bool]("show_config") or force): return let toOut = getConfigValues() - if get[bool](getChalkScope(), "use_pager"): + if attrGet[bool]("use_pager"): runPager($(toOut)) else: print(toOut) diff --git a/src/commands/cmd_insert.nim b/src/commands/cmd_insert.nim index 848b0679..6a494bbe 100644 --- a/src/commands/cmd_insert.nim +++ b/src/commands/cmd_insert.nim @@ -13,7 +13,7 @@ import ".."/[config, selfextract, collect, reporting, chalkjson, plugin_api] proc runCmdInsert*(path: seq[string]) {.exportc,cdecl.} = setContextDirectories(path) initCollection() - let virtual = get[bool](getChalkScope(), "virtual_chalk") + let virtual = attrGet[bool]("virtual_chalk") for item in artifacts(path): trace(item.name & ": begin chalking") diff --git a/src/config.nim b/src/config.nim index 8637e164..cf77195e 100644 --- a/src/config.nim +++ b/src/config.nim @@ -51,20 +51,20 @@ proc filterByTemplate*(dict: ChalkDict, p: string): ChalkDict = result = ChalkDict() for k, v in dict: let section = p & ".key" - if sectionExists(getChalkScope(), section): + if sectionExists(section): let ss = section & "." & k - if sectionExists(getChalkScope(), ss) and get[bool](getChalkScope(), ss & ".use"): + if sectionExists(ss) and attrGet[bool](ss & ".use"): result[k] = v proc getOutputConfig*(): string = return "outconf." & getBaseCommandName() template getMarkTemplate*(): string = - let tmplName = get[string](getChalkScope(), getOutputConfig() & ".mark_template") + let tmplName = attrGet[string](getOutputConfig() & ".mark_template") "mark_template." & tmplName template getReportTemplate*(): string = - let tmplName = get[string](getChalkScope(), getOutputConfig() & ".report_template") + let tmplName = attrGet[string](getOutputConfig() & ".report_template") "report_template." & tmplName template forceReportKeys*(keynames: openarray[string]) = @@ -72,22 +72,16 @@ template forceReportKeys*(keynames: openarray[string]) = let section = templateRef & ".key" # Create the "key" section if required. - if not sectionExists(getChalkScope(), section) and keynames.len > 0: - discard attrLookup( - getObject(getChalkScope(), templateRef), - ["key"], - ix = 0, - op = vlSecDef, - ) + if not sectionExists(section) and keynames.len > 0: + con4mSectionCreate(section) - let keys = getObject(getChalkScope(), section).getContents() + let keys = attrGetObject(section).getContents() for item in keynames: # Create the item section if required. if item notin keys: - discard attrLookup(getChalkScope(), [templateRef, "key", item], ix = 0, op = vlSecDef) + con4mSectionCreate(section & "." & item) con4mAttrSet( - getChalkScope(), section & "." & item & ".use", pack(true), Con4mType(kind: TypeBool), @@ -99,22 +93,16 @@ template forceChalkKeys*(keynames: openarray[string]) = let section = templateRef & ".key" # Create the "key" section if required. - if not sectionExists(getChalkScope(), section) and keynames.len > 0: - discard attrLookup( - getObject(getChalkScope(), templateRef), - ["key"], - ix = 0, - op = vlSecDef, - ) + if not sectionExists(section) and keynames.len > 0: + con4mSectionCreate(section) - let keys = getObject(getChalkScope(), section).getContents() + let keys = attrGetObject(section).getContents() for item in keynames: # Create the item section if required. if item notin keys: - discard attrLookup(getChalkScope(), [templateRef, "key", item], ix = 0, op = vlSecDef) + con4mSectionCreate(section & "." & item) con4mAttrSet( - getChalkScope(), section & "." & item & ".use", pack(true), Con4mType(kind: TypeBool), diff --git a/src/confload.nim b/src/confload.nim index 26ee982d..d6b7aaf2 100644 --- a/src/confload.nim +++ b/src/confload.nim @@ -85,10 +85,10 @@ proc findOptionalConf(state: ConfigState): Option[string] = trace(fname & ": No configuration file found.") proc loadLocalStructs*(state: ConfigState) = - if getOpt[bool](getChalkScope(), "color").isSome(): setShowColor(get[bool](getChalkScope(), "color")) - setLogLevel(get[string](getChalkScope(), "log_level")) + if attrGetOpt[bool]("color").isSome(): setShowColor(attrGet[bool]("color")) + setLogLevel(attrGet[string]("log_level")) var configPath: seq[string] = @[] - for path in get[seq[string]](getChalkScope(), "config_path"): + for path in attrGet[seq[string]]("config_path"): try: configPath.add(path.resolvePath()) except: @@ -98,15 +98,15 @@ proc loadLocalStructs*(state: ConfigState) = # and any logs are very verbose continue state.con4mAttrSet("config_path", pack(configPath)) - var c4errLevel = if get[bool](getChalkScope(), "con4m_pinpoint"): c4vShowLoc else: c4vBasic + var c4errLevel = if attrGet[bool]("con4m_pinpoint"): c4vShowLoc else: c4vBasic - if get[bool](getChalkScope(), "chalk_debug"): + if attrGet[bool]("chalk_debug"): c4errLevel = if c4errLevel == c4vBasic: c4vTrace else: c4vMax setCon4mVerbosity(c4errLevel) proc handleCon4mErrors(err, tb: string): bool = - if tb != "" and getChalkScope() == nil or get[bool](getChalkScope(), "chalk_debug"): + if tb != "" and getChalkScope() == nil or attrGet[bool]("chalk_debug"): echo(formatCompilerError(err, nil, tb, default(InstInfo))) else: error(err) @@ -190,12 +190,12 @@ proc loadAllConfigs*() = # The embedded config has already been validated. let configFile = getEmbeddedConfig() - if get[bool](getChalkScope(), "load_embedded_config"): + if attrGet[bool]("load_embedded_config"): stack.addConfLoad(embeddedConfName, toStream(configFile)). addCallback(loadLocalStructs) doRun() - if get[bool](getChalkScope(), "load_external_config"): + if attrGet[bool]("load_external_config"): let optConf = stack.configState.findOptionalConf() if optConf.isSome(): let fName = optConf.get() @@ -206,7 +206,7 @@ proc loadAllConfigs*() = doRun() hostInfo["_OP_CONFIG"] = pack(configFile) - if commandName == "not_supplied" and getOpt[string](getChalkScope(), "default_command").isSome(): + if commandName == "not_supplied" and attrGetOpt[string]("default_command").isSome(): setErrorHandler(stack, handleOtherErrors) addFinalizeGetOpts(stack, printAutoHelp = false) addCallback(stack, loadLocalStructs) diff --git a/src/docker/base.nim b/src/docker/base.nim index a99a1f8d..0a5f40d5 100644 --- a/src/docker/base.nim +++ b/src/docker/base.nim @@ -47,7 +47,7 @@ proc dockerPassThrough*(ctx: DockerInvocation) {.noreturn.} = exitCode = runCmdNoOutputCapture(exe, ctx.originalArgs, ctx.originalStdIn) - if get[bool](getChalkScope(), "docker.report_unwrapped_commands"): + if attrGet[bool]("docker.report_unwrapped_commands"): reporting.doReporting("report") except: dumpExOnDebug() diff --git a/src/docker/build.nim b/src/docker/build.nim index d0c80138..b9e73a8b 100644 --- a/src/docker/build.nim +++ b/src/docker/build.nim @@ -96,10 +96,10 @@ proc processCmdLine(ctx: DockerInvocation) = proc addVirtualLabels(ctx: DockerInvocation, chalk: ChalkObj) = trace("docker: adding virtual label args via --label flags") - let labelOpt = getOpt[TableRef[string, string]](getChalkScope(), "docker.custom_labels") + let labelOpt = attrGetOpt[TableRef[string, string]]("docker.custom_labels") if labelOpt.isSome(): ctx.newCmdLine.addLabelArgs(labelOpt.get()) - let labelTemplName = get[string](getChalkScope(), "docker.label_template") + let labelTemplName = attrGet[string]("docker.label_template") if labelTemplName == "": return let @@ -115,10 +115,10 @@ proc addVirtualLabels(ctx: DockerInvocation, chalk: ChalkObj) = proc addLabels(ctx: DockerInvocation, chalk: ChalkObj) = trace("docker: adding labels to Dockerfile") - let labelOpt = getOpt[TableRef[string, string]](getChalkScope(), "docker.custom_labels") + let labelOpt = attrGetOpt[TableRef[string, string]]("docker.custom_labels") if labelOpt.isSome(): ctx.addedInstructions.addLabelCmds(labelOpt.get()) - let labelTemplName = get[string](getChalkScope(), "docker.label_template") + let labelTemplName = attrGet[string]("docker.label_template") if labelTemplName == "": return let @@ -137,7 +137,7 @@ proc addEnvVars(ctx: DockerInvocation, chalk: ChalkObj) = var toAdd: seq[string] = @[] value: string - for k, v in get[TableRef[string, string]](getChalkScope(), "docker.additional_env_vars"): + for k, v in attrGet[TableRef[string, string]]("docker.additional_env_vars"): if not k.isValidEnvVarName(): warn("docker: ENV var " & k & " NOT added. Environment vars may only have " & "Letters (which will be upper-cased), numbers, and underscores.") @@ -350,9 +350,9 @@ proc dockerBuild*(ctx: DockerInvocation): int = # multi-platform builds should have same chalk id chalkId = ctx.chalkId, ) - wrapVirtual = get[bool](getChalkScope(), "virtual_chalk") - wrapEntrypoint = get[bool](getChalkScope(), "docker.wrap_entrypoint") - dockerSubscan = get[bool](getChalkScope(), "chalk_contained_items") + wrapVirtual = attrGet[bool]("virtual_chalk") + wrapEntrypoint = attrGet[bool]("docker.wrap_entrypoint") + dockerSubscan = attrGet[bool]("chalk_contained_items") trace("docker: processing build CLI args") ctx.processGitContext() diff --git a/src/docker/entrypoint.nim b/src/docker/entrypoint.nim index 1e23c6a7..2e13ef75 100644 --- a/src/docker/entrypoint.nim +++ b/src/docker/entrypoint.nim @@ -71,8 +71,8 @@ proc rewriteEntryPoint*(ctx: DockerInvocation, binaries: TableRef[DockerPlatform, string], user: string) = let - fromArgs = get[bool](getChalkScope(), "exec.command_name_from_args") - wrapCmd = get[bool](getChalkScope(), "docker.wrap_cmd") + fromArgs = attrGet[bool]("exec.command_name_from_args") + wrapCmd = attrGet[bool]("docker.wrap_cmd") (entrypoint, cmd, shell) = entrypoint if not fromArgs: diff --git a/src/docker/exe.nim b/src/docker/exe.nim index e157be04..90c34035 100644 --- a/src/docker/exe.nim +++ b/src/docker/exe.nim @@ -18,7 +18,7 @@ var proc getDockerExeLocation*(): string = once: let - dockerConfigPath = getOpt[string](getChalkScope(), "docker_exe") + dockerConfigPath = attrGetOpt[string]("docker_exe") dockerExeOpt = findExePath("docker", configPath = dockerConfigPath, ignoreChalkExes = true) diff --git a/src/docker/json.nim b/src/docker/json.nim index ed7eeb90..8afe4bba 100644 --- a/src/docker/json.nim +++ b/src/docker/json.nim @@ -67,7 +67,7 @@ proc mapFromJson(self: ChalkDict, discard let boxedValue = value.nimJsonToBox() - let t = get[Con4mType](getChalkScope(), "keyspec." & chalkKey & ".type") + let t = attrGet[Con4mType]("keyspec." & chalkKey & ".type") if not boxedValue.checkAutoType(t): warn("docker: JSON key " & jsonKey & " associated with chalk key '" & chalkKey & @@ -79,7 +79,7 @@ proc mapFromJson*(self: ChalkDict, node: JsonNode, map: JsonToChalkKeysMapping) = let - reportEmpty = get[bool](getChalkScope(), "docker.report_empty_fields") + reportEmpty = attrGet[bool]("docker.report_empty_fields") lowerJson = node.toLowerKeysJsonNode() for jsonKey, (chalkKey, transformer) in map: try: diff --git a/src/docker/platform.nim b/src/docker/platform.nim index 311ea917..a98c1c81 100644 --- a/src/docker/platform.nim +++ b/src/docker/platform.nim @@ -130,7 +130,7 @@ proc downloadPlatformBinary(targetPlatform: DockerPlatform): string = urls: seq[string] = @[] # attempt to donwload from all urls in the order they are defined - for config in get[seq[string]](getChalkScope(), "docker.download_arch_binary_urls"): + for config in attrGet[seq[string]]("docker.download_arch_binary_urls"): let url = (config .replace("{version}", getChalkExeVersion()) .replace("{commit}", getChalkCommitId()) @@ -145,7 +145,7 @@ proc downloadPlatformBinary(targetPlatform: DockerPlatform): string = continue let - base = get[string](getChalkScope(), "docker.arch_binary_locations_path") + base = attrGet[string]("docker.arch_binary_locations_path") folder = if base == "": writeNewTempFile("") @@ -175,8 +175,8 @@ proc findPlatformBinaries(): TableRef[DockerPlatform, string] = result = newTable[DockerPlatform, string]() let - basePath = get[string](getChalkScope(), "docker.arch_binary_locations_path") - locOpt = getOpt[TableRef[string, string]](getChalkScope(), "docker.arch_binary_locations") + basePath = attrGet[string]("docker.arch_binary_locations_path") + locOpt = attrGetOpt[TableRef[string, string]]("docker.arch_binary_locations") if basePath != "": let base = basePath.resolvePath() @@ -222,7 +222,7 @@ proc findPlatformBinary(ctx: DockerInvocation, targetPlatform: DockerPlatform): ) return path - if get[bool](getChalkScope(), "docker.download_arch_binary"): + if attrGet[bool]("docker.download_arch_binary"): trace("docker: no chalk binary found for " & "TARGETPLATFORM (" & $targetPlatform & "). " & "Attempting to download chalk binary.") diff --git a/src/docker/util.nim b/src/docker/util.nim index 75da1498..f9f75b80 100644 --- a/src/docker/util.nim +++ b/src/docker/util.nim @@ -27,7 +27,7 @@ proc isValidEnvVarName*(s: string): bool = var labelPrefix: string proc formatLabelKey(s: string): string = once: - labelPrefix = get[string](getChalkScope(), "docker.label_prefix") + labelPrefix = attrGet[string]("docker.label_prefix") result = labelPrefix @@ -119,7 +119,7 @@ proc getChalkKey*(chalk: ChalkObj, k: string): string = if key.startsWith("_"): raise newException(KeyError, "Invalid key; cannot use run-time keys, only chalk-time keys.") - if key notin getContents(getChalkScope().getObject("keyspec")): + if key notin getContents(attrGetObject("keyspec")): raise newException(KeyError, "Invalid for env var; Chalk key doesn't exist.") if key in hostInfo: diff --git a/src/docker/wrap.nim b/src/docker/wrap.nim index 6ff0ef9b..d5a06f9f 100644 --- a/src/docker/wrap.nim +++ b/src/docker/wrap.nim @@ -263,8 +263,8 @@ proc makeChalkAvailableToDocker*(ctx: DockerInvocation, "recent version of buildx is required for copying chalk by platform into Dockerfile", ) let - validate = get[bool](getChalkScope(), "load.validate_configs_on_load") - binfmt = get[bool](getChalkScope(), "docker.install_binfmt") + validate = attrGet[bool]("load.validate_configs_on_load") + binfmt = attrGet[bool]("docker.install_binfmt") # other chalks might have different config for validate_configs_on_load # so we ensure we honor self config via CLI arg check = if validate: "--validation" else: "--no-validation" diff --git a/src/normalize.nim b/src/normalize.nim index f0e58ae3..ac5a981f 100644 --- a/src/normalize.nim +++ b/src/normalize.nim @@ -84,5 +84,5 @@ proc normalizeChalk*(dict: ChalkDict): string = # Currently, this is only called for the METADATA_ID field, which only # signs things actually being written out. We skip MAGIC, SIGNATURE # and SIGN_PARAMS. - let ignoreList = get[seq[string]](getChalkScope(), "ignore_when_normalizing") + let ignoreList = attrGet[seq[string]]("ignore_when_normalizing") return binEncodeObj(dict, ignoreList) diff --git a/src/plugin_api.nim b/src/plugin_api.nim index edf43de2..734346e9 100644 --- a/src/plugin_api.nim +++ b/src/plugin_api.nim @@ -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: " & - get[seq[string]](getChalkScope(), "ignore_patterns")[i]) + attrGet[seq[string]]("ignore_patterns")[i]) trace("We will NOT report additional path skips.") return true @@ -441,7 +441,7 @@ proc scanArtifactLocations*(self: Plugin, state: ArtifactIterationInfo): followFLinks = false if isChalkingOp(): - let symLinkBehavior = get[string](getChalkScope(), "symlink_behavior") + let symLinkBehavior = attrGet[string]("symlink_behavior") if symLinkBehavior == "skip": skipLinks = true elif symLinkBehavior == "clobber": @@ -548,20 +548,20 @@ var installedPlugins: Table[string, Plugin] codecs: seq[Plugin] = @[] -template isCodec*(plugin: Plugin): bool = get[bool](getChalkScope(), "plugin." & plugin.name & ".codec") +template isCodec*(plugin: Plugin): bool = attrGet[bool]("plugin." & plugin.name & ".codec") proc checkPlugin(plugin: Plugin, codec: bool): bool {.inline.} = let name = plugin.name section = "plugin." & name - if not sectionExists(getChalkScope(), section): + if not sectionExists(section): error("No config provided for plugin " & name & ". Plugin ignored.") - elif not get[bool](getChalkScope(), section & ".enabled"): + elif not attrGet[bool](section & ".enabled"): trace("Plugin " & name & " is disabled via config file.") elif name in installedPlugins: error("Double install of plugin named: " & name) - elif get[bool](getChalkScope(), section & ".codec") != codec: + elif attrGet[bool](section & ".codec") != codec: if codec: error("Codec expected, but the config file does not declare that it " & "is a codec.") @@ -575,7 +575,7 @@ proc checkPlugin(plugin: Plugin, codec: bool): bool {.inline.} = proc getAllPlugins*(): seq[Plugin] = var preResult: seq[(int, Plugin)] = @[] for name, plugin in installedPlugins: - let tup = (get[int](getChalkScope(), "plugin." & plugin.name & ".priority"), plugin) + let tup = (attrGet[int]("plugin." & plugin.name & ".priority"), plugin) preResult.add(tup) preResult.sort() diff --git a/src/plugins/cloudMetadata.nim b/src/plugins/cloudMetadata.nim index f9b7c374..2e4b54e3 100644 --- a/src/plugins/cloudMetadata.nim +++ b/src/plugins/cloudMetadata.nim @@ -402,7 +402,7 @@ proc isAwsEc2Host(vendor: string): bool = # ref: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/identify_ec2_instances.html # older Xen instances - let uuid = tryToLoadFile(get[string](getChalkScope(), "cloud_provider.cloud_instance_hw_identifiers.sys_hypervisor_path")) + let uuid = tryToLoadFile(attrGet[string]("cloud_provider.cloud_instance_hw_identifiers.sys_hypervisor_path")) if uuid.toLowerAscii().startsWith("ec2"): return true @@ -412,7 +412,7 @@ proc isAwsEc2Host(vendor: string): bool = # this will only work if we have root, normally sudo dmidecode --string system-uuid # gives the same output - let productUuid = tryToLoadFile(get[string](getChalkScope(), "cloud_provider.cloud_instance_hw_identifiers.sys_product_path")) + let productUuid = tryToLoadFile(attrGet[string]("cloud_provider.cloud_instance_hw_identifiers.sys_product_path")) if productUuid.toLowerAscii().startsWith("ec2"): return true @@ -462,8 +462,8 @@ proc getHostKind(vendor: string, resolvContents: string): HostKind = proc cloudMetadataGetrunTimeHostInfo*(self: Plugin, objs: seq[ChalkObj]): ChalkDict {.cdecl.} = let - vendor = tryToLoadFile(get[string](getChalkScope(), "cloud_provider.cloud_instance_hw_identifiers.sys_vendor_path")) - resolv = tryToLoadFile(get[string](getChalkScope(), "cloud_provider.cloud_instance_hw_identifiers.sys_resolv_path")) + vendor = tryToLoadFile(attrGet[string]("cloud_provider.cloud_instance_hw_identifiers.sys_vendor_path")) + resolv = tryToLoadFile(attrGet[string]("cloud_provider.cloud_instance_hw_identifiers.sys_resolv_path")) result = case getHostKind(vendor, resolv) diff --git a/src/plugins/codecPythonPyc.nim b/src/plugins/codecPythonPyc.nim index a5d7901b..1aed02e2 100644 --- a/src/plugins/codecPythonPyc.nim +++ b/src/plugins/codecPythonPyc.nim @@ -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 get[seq[string]](getChalkScope(), "pyc_extensions"): + if not ext.startsWith(".") or ext[1..^1] notin attrGet[seq[string]]("pyc_extensions"): return none(ChalkObj) withFileStream(loc, mode = fmRead, strict = false): diff --git a/src/plugins/codecSource.nim b/src/plugins/codecSource.nim index e8a6ef6d..598c94ac 100644 --- a/src/plugins/codecSource.nim +++ b/src/plugins/codecSource.nim @@ -69,7 +69,7 @@ proc sourceScan*(self: Plugin, path: string): Option[ChalkObj] {.cdecl.} = hasBang = false if not isExe and isChalkingOp() and - get[bool](getChalkScope(), "source_marks.only_mark_when_execute_set"): + attrGet[bool]("source_marks.only_mark_when_execute_set"): return none(ChalkObj) var @@ -80,10 +80,10 @@ proc sourceScan*(self: Plugin, path: string): Option[ChalkObj] {.cdecl.} = if ext != "": ext = ext[1 .. ^1] # No need for the period. - if ext in get[seq[string]](getChalkScope(), "source_marks.text_only_extensions"): + if ext in attrGet[seq[string]]("source_marks.text_only_extensions"): return none(ChalkObj) - let exts = get[TableRef[string, string]](getChalkScope(), "source_marks.extensions_to_languages_map") + let exts = attrGet[TableRef[string, string]]("source_marks.extensions_to_languages_map") if ext in exts: # We might revise this if there's a shebang line; it takes precidence. lang = exts[ext] @@ -97,7 +97,7 @@ proc sourceScan*(self: Plugin, path: string): Option[ChalkObj] {.cdecl.} = let bytes = stream.peekStr(2) if bytes != "#!": - if isChalkingOp() and get[bool](getChalkScope(), "source_marks.only_mark_shebangs"): + if isChalkingOp() and attrGet[bool]("source_marks.only_mark_shebangs"): return none(ChalkObj) elif not stream.seemsToBeUtf8(): return none(ChalkObj) @@ -111,7 +111,7 @@ proc sourceScan*(self: Plugin, path: string): Option[ChalkObj] {.cdecl.} = # While we already checked this above, if the shebang was there, # but was invalid, we'll behave as if it wasn't there at all. if not hasBang and isChalkingOp() and - get[bool](getChalkScope(), "source_marks.only_mark_shebangs"): + attrGet[bool]("source_marks.only_mark_shebangs"): return none(ChalkObj) if lang == "": @@ -121,7 +121,7 @@ proc sourceScan*(self: Plugin, path: string): Option[ChalkObj] {.cdecl.} = # At this point, *if* there's a custom_logic callback, we need to # call it, otherwise we are done. - let opt = getOpt[CallbackObj](getChalkScope(), "source_marks.custom_logic") + let opt = attrGetOpt[CallbackObj]("source_marks.custom_logic") if opt.isSome(): let @@ -131,7 +131,7 @@ proc sourceScan*(self: Plugin, path: string): Option[ChalkObj] {.cdecl.} = if not proceed: return none(ChalkObj) - let langs = get[TableRef[string, string]](getChalkScope(), "source_marks.language_to_comment_map") + let langs = attrGet[TableRef[string, string]]("source_marks.language_to_comment_map") if lang in langs: commentPrefix = langs[lang] else: diff --git a/src/plugins/codecZip.nim b/src/plugins/codecZip.nim index a2179409..2d231cff 100644 --- a/src/plugins/codecZip.nim +++ b/src/plugins/codecZip.nim @@ -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 get[seq[string]](getChalkScope(), "zip_extensions"): + if not ext.startsWith(".") or ext[1..^1] notin attrGet[seq[string]]("zip_extensions"): return none(ChalkObj) withFileStream(loc, mode = fmRead, strict = false): @@ -93,7 +93,7 @@ proc zipScan*(self: Plugin, loc: string): Option[ChalkObj] {.cdecl.} = cache = ZipCache() origD = tmpDir.joinPath("contents") hashD = tmpDir.joinPath("hash") - subscans = get[bool](getChalkScope(), "chalk_contained_items") + subscans = attrGet[bool]("chalk_contained_items") chalk = newChalk(name = loc, cache = cache, fsRef = loc, @@ -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 get[bool](getChalkScope(), "chalk_debug"): + if not attrGet[bool]("chalk_debug"): toggleLoggingEnabled() discard runChalkSubScan(hashD, "delete") - if not get[bool](getChalkScope(), "chalk_debug"): + if not attrGet[bool]("chalk_debug"): toggleLoggingEnabled() if zipChalkFile in cache.onDisk.contents: @@ -215,7 +215,7 @@ proc zipGetChalkTimeArtifactInfo*(self: Plugin, obj: ChalkObj): let cache = ZipCache(obj.cache) result = ChalkDict() - if get[bool](getChalkScope(), "chalk_contained_items") and cache.embeddedChalk.kind != MkObj: + if attrGet[bool]("chalk_contained_items") and cache.embeddedChalk.kind != MkObj: result["EMBEDDED_CHALK"] = cache.embeddedChalk result["EMBEDDED_TMPDIR"] = pack(cache.tmpDir) diff --git a/src/plugins/conffile.nim b/src/plugins/conffile.nim index 72e5f110..8214410c 100644 --- a/src/plugins/conffile.nim +++ b/src/plugins/conffile.nim @@ -16,14 +16,14 @@ proc scanForWork(kt: auto, opt: Option[ChalkObj], args: seq[Box]): ChalkDict = let v = "keyspec." & k if opt.isNone() and k in hostInfo: continue if opt.isSome() and k in opt.get().collectedData: continue - if get[int](getChalkScope(), v & ".kind") != int(kt): continue + if attrGet[int](v & ".kind") != int(kt): continue if k notin subscribedKeys: continue - let valueOpt = attrLookup(getChalkScope(), v & ".value") - let callbackOpt = attrLookup(getChalkScope(), v & ".callback") + let valueOpt = attrGetOpt[Box](v & ".value") + let callbackOpt = attrGetOpt[CallbackObj](v & ".callback") if valueOpt.isSome(): - result[k] = unpack[Box](valueOpt.get()) + result[k] = valueOpt.get() elif callbackOpt.isSome(): - let cbOpt = runCallback(unpack[CallbackObj](callbackOpt.get()), args) + let cbOpt = runCallback(callbackOpt.get(), args) if cbOpt.isSome(): result[k] = cbOpt.get() proc confGetChalkTimeHostInfo*(self: Plugin): ChalkDict {.cdecl.} = diff --git a/src/plugins/externalTool.nim b/src/plugins/externalTool.nim index 5f7b3ca3..f0774fbf 100644 --- a/src/plugins/externalTool.nim +++ b/src/plugins/externalTool.nim @@ -30,27 +30,27 @@ proc runOneTool(info: PIInfo, path: string): ChalkDict = result = ChalkDict() let args = @[pack(path)] let base = "tool." & info.name - var exe = ensureRunCallback[string](get[CallbackObj](getChalkScope(), base & ".get_tool_location"), args) + var exe = ensureRunCallback[string](attrGet[CallbackObj](base & ".get_tool_location"), args) if exe == "": - let installed = ensureRunCallback[bool](get[CallbackObj](getChalkScope(), base & ".attempt_install"), args) + let installed = ensureRunCallback[bool](attrGet[CallbackObj](base & ".attempt_install"), args) if not installed: trace(info.name & ": could not be installed. skipping") return - exe = ensureRunCallback[string](get[CallbackObj](getChalkScope(), base & ".get_tool_location"), args) + exe = ensureRunCallback[string](attrGet[CallbackObj](base & ".get_tool_location"), args) if exe == "": trace(info.name & ": could not be found. skipping") return let - argv = ensureRunCallback[string](get[CallbackObj](getChalkScope(), base & ".get_command_args"), args) + argv = ensureRunCallback[string](attrGet[CallbackObj](base & ".get_command_args"), args) cmd = exe & " " & argv.strip() trace(cmd) let outs = unpack[seq[Box]](c4mSystem(@[pack(cmd)]).get()) - let d = ensureRunCallback[ChalkDict](get[CallbackObj](getChalkScope(), base & ".produce_keys"), outs) + let d = ensureRunCallback[ChalkDict](attrGet[CallbackObj](base & ".produce_keys"), outs) if len(d) == 0: trace(info.name & ": produced no keys. skipping") return @@ -74,8 +74,8 @@ template toolBase(path: string) {.dirty.} = var toolInfo = initTable[string, seq[(int, PIInfo)]]() let - runSbom = get[bool](getChalkScope(), "run_sbom_tools") - runSast = get[bool](getChalkScope(), "run_sast_tools") + runSbom = attrGet[bool]("run_sbom_tools") + runSast = attrGet[bool]("run_sast_tools") # tools should only run during insert operations # note this is a subset of chalkable operations @@ -84,12 +84,12 @@ template toolBase(path: string) {.dirty.} = for k in getChalkSubsections("tool"): let v = "tool." & k - if not get[bool](getChalkScope(), v & ".enabled"): continue - let kind = get[string](getChalkScope(), v & ".kind") + if not attrGet[bool](v & ".enabled"): continue + let kind = attrGet[string](v & ".kind") if not runSbom and kind == "sbom": continue if not runSast and kind == "sast": continue - let tool = (get[int](getChalkScope(), v & ".priority"), PIInfo(name: k)) + let tool = (attrGet[int](v & ".priority"), PIInfo(name: k)) if kind notin toolInfo: toolInfo[kind] = @[tool] else: @@ -107,7 +107,7 @@ template toolBase(path: string) {.dirty.} = # merged structure should be: # { SBOM: { foo: {...}, bar: {...} } } result.merge(data.nestWith(info.name)) - if len(data) >= 0 and get[bool](getChalkScope(), "tool." & info.name & ".stop_on_success"): + if len(data) >= 0 and attrGet[bool]("tool." & info.name & ".stop_on_success"): break except: error(info.name & ": " & getCurrentExceptionMsg()) diff --git a/src/plugins/system.nim b/src/plugins/system.nim index 1d0defd6..b8b11c41 100644 --- a/src/plugins/system.nim +++ b/src/plugins/system.nim @@ -118,7 +118,7 @@ proc applySubstitutions(obj: ChalkObj) {.inline.} = for k, v in obj.collectedData: if v.kind != MkStr: continue # If it's not a string object, no sub to do. # Should have crashed by now if section does not exist :) - if not get[bool](getChalkScope(), "keyspec." & k & ".apply_substitutions"): continue + if not attrGet[bool]("keyspec." & k & ".apply_substitutions"): continue let s = unpack[string](v) if not s.contains("{"): continue obj.collectedData[k] = pack(s.multiReplace(subs)) @@ -147,7 +147,7 @@ proc sysGetRunTimeArtifactInfo*(self: Plugin, obj: ChalkObj, insert: bool): if insert: obj.applySubstitutions() result.setIfNeeded("_OP_CHALKED_KEYS", toSeq(obj.getChalkMark().keys)) - result.setIfNeeded("_VIRTUAL", get[bool](getChalkScope(), "virtual_chalk")) + result.setIfNeeded("_VIRTUAL", attrGet[bool]("virtual_chalk")) else: result.setValidated(obj, obj.validateMetaData()) @@ -155,7 +155,7 @@ proc sysGetRunTimeArtifactInfo*(self: Plugin, obj: ChalkObj, insert: bool): result.setIfNeeded("_OP_ARTIFACT_PATH", resolvePath(obj.fsRef)) var - templateName = get[string](getChalkScope(), getOutputConfig() & ".report_template") + templateName = attrGet[string](getOutputConfig() & ".report_template") if templateName != "": let @@ -185,10 +185,10 @@ proc getEnvDict(): Box = once: envdict = Con4mDict[string, string]() let - always = get[seq[string]](getChalkScope(), "env_always_show") - never = get[seq[string]](getChalkScope(), "env_never_show") - redact = get[seq[string]](getChalkScope(), "env_redact") - def = get[string](getChalkScope(), "env_default_action")[0] + always = attrGet[seq[string]]("env_always_show") + never = attrGet[seq[string]]("env_never_show") + redact = attrGet[seq[string]]("env_redact") + def = attrGet[string]("env_default_action")[0] for (k, v) in envPairs(): # TODO: could add some con4m to warn on overlap across these 3. For now, @@ -233,7 +233,7 @@ proc sysGetRunTimeHostInfo*(self: Plugin, objs: seq[ChalkObj]): if isSubscribedKey("_ENV"): result["_ENV"] = getEnvDict() - let templateName = get[string](getChalkScope(), getOutputConfig() & ".report_template") + let templateName = attrGet[string](getOutputConfig() & ".report_template") if isSubscribedKey("_OP_HOST_REPORT_KEYS") and templateName != "": let templateToUse = "report_template." & templateName diff --git a/src/plugins/techStackGeneric.nim b/src/plugins/techStackGeneric.nim index d1a3f419..83ad8140 100644 --- a/src/plugins/techStackGeneric.nim +++ b/src/plugins/techStackGeneric.nim @@ -118,7 +118,7 @@ proc scanFileStream(strm: FileStream, filePath: string, category: string, subcat var ignored: seq[Regex] = @[] proc getIgnored(): seq[Regex] = once: - for i in get[seq[string]](getChalkScope(), "ignore_patterns"): + for i in attrGet[seq[string]]("ignore_patterns"): ignored.add(re(i)) return ignored @@ -161,7 +161,7 @@ proc getProcNames(): HashSet[string] = # ps output etc in upcoming revisions proc hostHasTechStack(scope: string, proc_names: HashSet[string]): bool = # first check directories and filepaths, then processes - let scopedDirs = getOpt[seq[string]](getChalkScope(), scope & ".directories") + let scopedDirs = attrGetOpt[seq[string]](scope & ".directories") var fExists = false var dExists = false @@ -171,20 +171,20 @@ proc hostHasTechStack(scope: string, proc_names: HashSet[string]): bool = dExists = true break - let filepaths = getOpt[seq[string]](getChalkScope(), scope & ".filepaths") + let filepaths = attrGetOpt[seq[string]](scope & ".filepaths") if filepaths.isSome(): for path in filepaths.get(): if fileExists(path): fExists = true break - let names = getOpt[seq[string]](getChalkScope(), scope & ".process_names") + let names = attrGetOpt[seq[string]](scope & ".process_names") if names.isSome(): let rule_names = toHashSet(names.get()) intersection = proc_names * rule_names if len(intersection) > 0: - if get[bool](getChalkScope(), scope & ".strict"): + if attrGet[bool](scope & ".strict"): return fExists or dExists return true @@ -223,7 +223,7 @@ proc detectLanguages(): HashSet[string] = trace("tech stack: detecting languages") result = initHashSet[string]() - let canLoad = get[bool](getChalkScope(), "use_tech_stack_detection") + let canLoad = attrGet[bool]("use_tech_stack_detection") if not canLoad: return result @@ -280,8 +280,7 @@ proc detectTechCwd(): TableRef[string, seq[string]] = proc loadState() = once: for langName in getChalkSubsections("linguist_language"): - let ext = get[string](getChalkScope(), - "linguist_language." & langName & ".extension") + let ext = attrGet[string]("linguist_language." & langName & ".extension") languages[ext] = langName for key in getChalkSubsections("tech_stack_rule"): @@ -289,20 +288,20 @@ proc loadState() = trace("tech stack: loading " & key) let val = "tech_stack_rule." & key let - category = get[string](getChalkScope(), val & ".category") - subcategory = get[string](getChalkScope(), val & ".subcategory") + category = attrGet[string](val & ".category") + subcategory = attrGet[string](val & ".subcategory") categories. mgetOrPut(category, newTable[string, seq[string]]()). mgetOrPut(subcategory, @[]). add(key) - if sectionExists(getChalkScope(), val & ".host_scope"): + if sectionExists(val & ".host_scope"): if category notin inHostScope: inHostScope[category] = newTable[string, bool]() inHostScope[category][subcategory] = false else: - if not sectionExists(getChalkScope(), val & ".file_scope"): + if not sectionExists(val & ".file_scope"): error("One of file_scope, host_scope must be defined for rule " & key & ". Skipping") continue if category notin inFileScope: @@ -310,9 +309,9 @@ proc loadState() = inFileScope[category][subcategory] = false tsRules.incl(key) - regexes[key] = re(get[string](getChalkScope(), val & ".file_scope.regex")) - headLimits[key] = get[int](getChalkScope(), val & ".file_scope.head") - let filetypes = getOpt[seq[string]](getChalkScope(), val & ".file_scope.filetypes") + regexes[key] = re(attrGet[string](val & ".file_scope.regex")) + headLimits[key] = attrGet[int](val & ".file_scope.head") + let filetypes = attrGetOpt[seq[string]](val & ".file_scope.filetypes") if filetypes.isSome(): let ftypes = filetypes.get() ruleFiletypes[key] = ftypes @@ -323,7 +322,7 @@ proc loadState() = # XXX move to a template for looking things up and adding if # they don't exist ftRules.mgetOrPut(FT_ANY, initHashSet[string]()).incl(key) - let excludeFiletypes = getOpt[seq[string]](getChalkScope(), val & ".file_scope.excluded_filetypes") + let excludeFiletypes = attrGetOpt[seq[string]](val & ".file_scope.excluded_filetypes") if excludeFiletypes.isSome(): let exclFtps = excludeFiletypes.get() ruleExcludeFiletypes[key] = exclFtps @@ -331,13 +330,13 @@ proc loadState() = excludeFtRules.mgetOrPut(ft, initHashSet[string]()).incl(key) # get paths and excluded paths that need to always be considered - let filepaths = getOpt[seq[string]](getChalkScope(), val & ".file_scope.filepaths") + let filepaths = attrGetOpt[seq[string]](val & ".file_scope.filepaths") if filepaths.isSome(): let fpaths = filepaths.get() for path in fpaths: pthRules.mgetOrPut(path, initHashSet[string]()).incl(key) - let excludeFilepaths = getOpt[seq[string]](getChalkScope(), val & ".file_scope.excluded_filepaths") + let excludeFilepaths = attrGetOpt[seq[string]](val & ".file_scope.excluded_filepaths") if excludeFilepaths.isSome(): let excfpaths = excludeFilepaths.get() for path in excfpaths: @@ -345,7 +344,7 @@ proc loadState() = proc techStackRuntime*(self: Plugin, objs: seq[ChalkObj]): ChalkDict {.cdecl.} = result = ChalkDict() - let canLoad = get[bool](getChalkScope(), "use_tech_stack_detection") + let canLoad = attrGet[bool]("use_tech_stack_detection") if not canLoad: trace("Skipping tech stack runtime detection plugin") return result @@ -358,11 +357,11 @@ proc techStackRuntime*(self: Plugin, objs: seq[ChalkObj]): ChalkDict {.cdecl.} = when defined(debug): trace("tech stack: collecting " & key) let val = "tech_stack_rule." & key - if not sectionExists(getChalkScope(), val & ".host_scope"): + if not sectionExists(val & ".host_scope"): continue let - category = get[string](getChalkScope(), val & ".category") - subcategory = get[string](getChalkScope(), val & ".subcategory") + category = attrGet[string](val & ".category") + subcategory = attrGet[string](val & ".subcategory") if (category in inHostScope and subcategory in inHostScope[category] and not inHostScope[category][subcategory]): @@ -376,7 +375,7 @@ proc techStackRuntime*(self: Plugin, objs: seq[ChalkObj]): ChalkDict {.cdecl.} = proc techStackArtifact*(self: Plugin, objs: ChalkObj): ChalkDict {.cdecl.} = result = ChalkDict() - let canLoad = get[bool](getChalkScope(), "use_tech_stack_detection") + let canLoad = attrGet[bool]("use_tech_stack_detection") if not canLoad: trace("Skipping tech stack detection plugin for artifacts") return result diff --git a/src/plugins/vctlGit.nim b/src/plugins/vctlGit.nim index 3587477c..22ac11ca 100644 --- a/src/plugins/vctlGit.nim +++ b/src/plugins/vctlGit.nim @@ -593,7 +593,7 @@ proc loadTags(info: RepoInfo) = proc refetchTags(info: RepoInfo) = if info.origin == "" or info.origin == ghLocal: return - if not get[bool](getChalkScope(), "git.refetch_lightweight_tags"): + if not attrGet[bool]("git.refetch_lightweight_tags"): return var toRefetch: seq[GitTag] = @[] for _, tag in info.tags: diff --git a/src/reportcache.nim b/src/reportcache.nim index e0b73b1e..f362fc4b 100644 --- a/src/reportcache.nim +++ b/src/reportcache.nim @@ -112,7 +112,7 @@ template tracePublish(topic, m: string, prevSuccesses = false) = " subscribers)") if n == 0 and startSubscriptions != 0: - if get[bool](getChalkScope(), "use_report_cache"): + if attrGet[bool]("use_report_cache"): error("For topic '" & topic & "': No output config is working, but " & "failures will be stored in the report cache") else: @@ -120,7 +120,7 @@ template tracePublish(topic, m: string, prevSuccesses = false) = error("No output configuration is working for this report, and there " & "is no report cache configured, so no metadata was recorded.") - if get[bool](getChalkScope(), "force_output_on_reporting_fails"): + if attrGet[bool]("force_output_on_reporting_fails"): error("Here is the orphaned report:") doPanicWrite(msg) error("Run with --use-report-cache to automatically buffer failures " & @@ -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 get[bool](getChalkScope(), "use_report_cache"): + if attrGet[bool]("use_report_cache"): error("For topic '" & topic & "': publish failures will be cached.") else: error("No report cache is enabled; sink configs with failures will " & @@ -141,7 +141,7 @@ proc loadReportCache(fname: string) = once: try: let - retries = get[int](getChalkScope(), "report_cache_lock_timeout_sec") + retries = attrGet[int]("report_cache_lock_timeout_sec") lines = readViaLockFile(fname).strip().split("\n") for line in lines: let @@ -319,7 +319,7 @@ proc panicPublish(contents, tmpfilename, targetname, err: string) = doPanicWrite(s) proc safePublish*(topic, msg: string) = - if not get[bool](getChalkScope(), "use_report_cache"): + if not attrGet[bool]("use_report_cache"): tracePublish(topic, msg) return @@ -332,7 +332,7 @@ proc safePublish*(topic, msg: string) = sinkErrors = @[] - let fname = resolvePath(get[string](getChalkScope(), "report_cache_location")) + let fname = resolvePath(attrGet[string]("report_cache_location")) loadReportCache(fname) if reportCache.len() != 0: @@ -357,7 +357,7 @@ proc writeReportCache*() = # Everything else we might have published was probably mainly intended # for the console. - if not get[bool](getChalkScope(), "use_report_cache"): + if not attrGet[bool]("use_report_cache"): return # If nothing published, the reporting cache may not have been loaded, in @@ -365,7 +365,7 @@ proc writeReportCache*() = if cacheOpenFailed and len(reportCache) != 0: error(msgPossibleLoss) - let fname = resolvePath(get[string](getChalkScope(), "report_cache_location")) + let fname = resolvePath(attrGet[string]("report_cache_location")) if not dirtyCache and len(reportCache) != 0: warn("Report cache contains unreported message(s); Cached entries " & diff --git a/src/reporting.nim b/src/reporting.nim index 18655a61..77ae08b5 100644 --- a/src/reporting.nim +++ b/src/reporting.nim @@ -77,7 +77,7 @@ proc doCommandReport(): string {.inline.} = reportTemplate = getReportTemplate() # The above goes from the string name to the object. - if get[bool](getChalkScope(), "skip_command_report"): + if attrGet[bool]("skip_command_report"): info("Skipping the command report as per the `skip_command_report` directive") result = "" else: @@ -107,23 +107,23 @@ template doEmbeddedReport(): Box = template doCustomReporting() = for topic in getChalkSubsections("custom_report"): let spec = "custom_report." & topic - let enabledOpt = getOpt[bool](getChalkScope(), spec & ".enabled") + let enabledOpt = attrGetOpt[bool](spec & ".enabled") if enabledOpt.isNone() or not enabledOpt.get(): continue var - sinkConfs = get[seq[string]](getChalkScope(), spec & ".sink_configs") + sinkConfs = attrGet[seq[string]](spec & ".sink_configs") discard registerTopic(topic) - let useWhen = get[seq[string]](getChalkScope(), spec & ".use_when") + let useWhen = attrGet[seq[string]](spec & ".use_when") if getCommandName() notin useWhen and "*" notin useWhen: continue - if topic == "audit" and not get[bool](getChalkScope(), "publish_audit"): + if topic == "audit" and not attrGet[bool]("publish_audit"): continue if len(sinkConfs) == 0 and topic notin ["audit", "chalk_usage_stats"]: warn("Report '" & topic & "' has no configured sinks. Skipping.") let - reportTemplate = get[string](getChalkScope(), spec & ".report_template") + reportTemplate = attrGet[string](spec & ".report_template") templateToUse = "report_template." & reportTemplate for sinkConfName in sinkConfs: @@ -139,8 +139,8 @@ proc doReporting*(topic="report") {.exportc, cdecl.} = ctx.report = doEmbeddedReport() else: let - skipCommand = get[bool](getChalkScope(), "skip_command_report") - skipCustom = get[bool](getChalkScope(), "skip_custom_reports") + skipCommand = attrGet[bool]("skip_command_report") + skipCustom = attrGet[bool]("skip_custom_reports") if skipCommand and skipCustom: return trace("Collecting runtime host info.") diff --git a/src/run_management.nim b/src/run_management.nim index 8f6e9141..6adc32f5 100644 --- a/src/run_management.nim +++ b/src/run_management.nim @@ -20,16 +20,25 @@ var proc getChalkScope*(): AttrScope = con4mRuntime.configState.attrs +proc sectionExists*(s: string): bool = + getChalkScope().getObjectOpt(s).isSome() + +proc attrGet*[T](fqn: string): T = + get[T](getChalkScope(), fqn) + +proc attrGetOpt*[T](fqn: string): Option[T] = + getOpt[T](getChalkScope(), fqn) + +proc attrGetObject*(fqn: string): AttrScope = + getObject(getChalkScope(), fqn) + iterator getChalkSubsections*(s: string): string = ## Walks the contents of the given chalk config section, and yields the ## names of the subsections. - for k, v in con4mRuntime.configState.attrs.getObject(s).contents: + for k, v in attrGetObject(s).contents: if v.isA(AttrScope): yield k -proc sectionExists*(scope: AttrScope, s: string): bool = - scope.getObjectOpt(s).isSome() - proc con4mAttrSet*(ctx: ConfigState, fqn: string, value: Box) = ## Sets the value of the `fqn` attribute in `ctx.attrs` to `value`, raising ## `AssertionDefect` if unsuccessful. @@ -38,12 +47,15 @@ proc con4mAttrSet*(ctx: ConfigState, fqn: string, value: Box) = ## attribute isn't already set, use the other `con4mAttrSet` overload instead. doAssert attrSet(ctx, fqn, value).code == errOk -proc con4mAttrSet*(attrs: AttrScope, fqn: string, value: Box, attrType: Con4mType) = - ## Sets the value of the `fqn` attribute in `attrs` to `value`, raising - ## `AssertionDefect` if unsuccessful. +proc con4mAttrSet*(fqn: string, value: Box, attrType: Con4mType) = + ## Sets the value of the `fqn` attribute to `value`, raising `AssertionDefect` + ## if unsuccessful. ## ## This proc may be used if the attribute is not already set. - doAssert attrSet(attrs, fqn, value, attrType).code == errOk + doAssert attrSet(getChalkScope(), fqn, value, attrType).code == errOk + +proc con4mSectionCreate*(fqn: string) = + discard attrLookup(getChalkScope(), fqn.split('.'), ix = 0, op = vlSecDef) # This is for when we're doing a `conf load`. We force silence, turning off # all logging of merit. @@ -204,7 +216,7 @@ template trySetIfNeeded*(o: ChalkDict, k: string, code: untyped) = trace("Could not set chalk key " & k & " due to: " & getCurrentExceptionMsg()) proc isChalkingOp*(): bool = - return commandName in get[seq[string]](getChalkScope(), "valid_chalk_command_names") + return commandName in attrGet[seq[string]]("valid_chalk_command_names") proc lookupByPath*(obj: ChalkDict, path: string): Option[Box] = let diff --git a/src/selfextract.nim b/src/selfextract.nim index 00a4d7ae..116cf37b 100644 --- a/src/selfextract.nim +++ b/src/selfextract.nim @@ -150,7 +150,7 @@ proc getSelfExtraction*(): Option[ChalkObj] = # The rest of this is specific to writing the self-config. proc newConfFileError(err, tb: string): bool = - if getChalkScope() != nil and get[bool](getChalkScope(), "chalk_debug"): + if getChalkScope() != nil and attrGet[bool]("chalk_debug"): cantLoad(err & "\n" & tb) else: cantLoad(err) @@ -235,7 +235,7 @@ proc testConfigFile(newCon4m: string, let uri = "[testing config]" info(uri & ": Validating configuration.") - if get[bool](getChalkScope(), "load.validation_warning"): + if attrGet[bool]("load.validation_warning"): warn("Note: validation involves creating a new configuration context" & " and evaluating your code to make sure it at least evaluates " & "fine on a default path. subscribe() and unsubscribe() will " & @@ -318,7 +318,7 @@ proc handleConfigLoadAll*(inpath: string): bool = info("Replacing all chalk configuration from " & inpath) try: let - validate = get[bool](getChalkScope(), "load.validate_configs_on_load") + validate = attrGet[bool]("load.validate_configs_on_load") required = @[configKey, paramKey, cacheKey] data = if inpath == "-": stdin.readAll() else: tryToLoadFile(inpath.resolvePath()) jsonData = data.parseJson() @@ -372,12 +372,12 @@ proc handleConfigLoad*(inpath: string): bool = assert selfChalk != nil let - validate = get[bool](getChalkScope(), "load.validate_configs_on_load") - replace = get[bool](getChalkScope(), "load.replace_conf") - replaceAll = get[bool](getChalkScope(), "load.replace_all") - confPaths = get[seq[string]](getChalkScope(), "config_path").strip(leading = false, chars = {'/'}) - confFilename = get[string](getChalkScope(), "config_filename") - paramsViaStdin = get[bool](getChalkScope(), "load.params_via_stdin") + validate = attrGet[bool]("load.validate_configs_on_load") + replace = attrGet[bool]("load.replace_conf") + replaceAll = attrGet[bool]("load.replace_all") + confPaths = attrGet[seq[string]]("config_path").strip(leading = false, chars = {'/'}) + confFilename = attrGet[string]("config_filename") + paramsViaStdin = attrGet[bool]("load.params_via_stdin") if replace: info("Replacing base configuration with module from: " & inpath) diff --git a/src/sinks.nim b/src/sinks.nim index 91c14878..fcffcfd6 100644 --- a/src/sinks.nim +++ b/src/sinks.nim @@ -45,7 +45,7 @@ proc chalkErrFilter*(msg: string, info: StringTable): (string, bool) = let llStr = info[keyLogLevel] if (llStr in toLogLevelMap) and getChalkScope() != nil and - (toLogLevelMap[llStr] <= toLogLevelMap[get[string](getChalkScope(), "chalk_log_level")]): + (toLogLevelMap[llStr] <= toLogLevelMap[attrGet[string]("chalk_log_level")]): return (msg, true) return ("", false) @@ -88,7 +88,7 @@ template formatIo(cfg: SinkConfig, t: Topic, err: string, msg: string): string = line &= " (sink conf='" & cfg.name & "')" - if get[string](getChalkScope(), "log_level") == "trace": + if attrGet[string]("log_level") == "trace": case cfg.mySink.name of "post", "presign": let @@ -146,27 +146,27 @@ proc ioErrorHandler(cfg: SinkConfig, t: Topic, msg, err, tb: string) = let toOut = formatIo(cfg, t, err, msg) - if not quiet or get[bool](getChalkScope(), "chalk_debug"): + if not quiet or attrGet[bool]("chalk_debug"): error(toOut) else: trace(toOut) - if getChalkScope() != nil and get[bool](getChalkScope(), "chalk_debug"): + if getChalkScope() != nil and attrGet[bool]("chalk_debug"): publish("debug", tb) proc successHandler(cfg: SinkConfig, t: Topic, errmsg: string) = let quiet = t.name in quietTopics - if quiet and not get[bool](getChalkScope(), "chalk_debug"): + if quiet and not attrGet[bool]("chalk_debug"): return let toOut = formatIo(cfg, t, errmsg, "") - if get[string](getChalkScope(), "log_level") in ["trace", "info"]: + if attrGet[string]("log_level") in ["trace", "info"]: let section = "sink_config." & cfg.name - if sectionExists(getChalkScope(), section) and errmsg == "Write": - let msgOpt = getOpt[string](getChalkScope(), section & ".on_write_msg") + if sectionExists(section) and errmsg == "Write": + let msgOpt = attrGetOpt[string](section & ".on_write_msg") if msgOpt.isSome(): info(strutils.strip(msgOpt.get())) elif quiet: @@ -186,7 +186,7 @@ proc getAuthConfigByName*(name: string, attr: AttrScope = AttrScope(nil)): Optio section = "auth_config." & name opts = OrderedTableRef[string, string]() - if not sectionExists(attrRoot, section): + if attrRoot.getObjectOpt(section).isNone(): error(section & " is referenced but its missing in the config") return none(AuthConfig) @@ -232,7 +232,7 @@ proc getSinkConfigByName*(name: string): Option[SinkConfig] = let section = "sink_config." & name - if not sectionExists(getChalkScope(), section): + if not sectionExists(section): return none(SinkConfig) var @@ -245,22 +245,22 @@ proc getSinkConfigByName*(name: string): Option[SinkConfig] = priority: int deleteList: seq[string] - for k, _ in getObject(getChalkScope(), section).contents: + for k, _ in attrGetObject(section).contents: case k of "enabled": - if not get[bool](getChalkScope(), section & "." & k): + if not attrGet[bool](section & "." & k): error("Sink configuration '" & name & " is disabled.") enabled = false of "priority": - priority = getOpt[int](getChalkScope(), section & "." & k).getOrElse(0) + priority = attrGetOpt[int](section & "." & k).getOrElse(0) of "filters": - filterNames = getOpt[seq[string]](getChalkScope(), section & "." & k).getOrElse(@[]) + filterNames = attrGetOpt[seq[string]](section & "." & k).getOrElse(@[]) of "sink": - sinkName = getOpt[string](getChalkScope(), section & "." & k).getOrElse("") + sinkName = attrGetOpt[string](section & "." & k).getOrElse("") of "auth": - authName = getOpt[string](getChalkScope(), section & "." & k).getOrElse("") + authName = attrGetOpt[string](section & "." & k).getOrElse("") of "use_search_path", "disallow_http": - let boxOpt = getOpt[Box](getChalkScope(), section & "." & k) + let boxOpt = attrGetOpt[Box](section & "." & k) if boxOpt.isSome(): if boxOpt.get().kind != MkBool: error(k & " (sink config key) must be 'true' or 'false'") @@ -268,7 +268,7 @@ proc getSinkConfigByName*(name: string): Option[SinkConfig] = opts[k] = $(unpack[bool](boxOpt.get())) of "pinned_cert": let - certContents = getOpt[string](getChalkScope(), section & "." & k).getOrElse("") + certContents = attrGetOpt[string](section & "." & k).getOrElse("") path = writeNewTempFile(certContents, "pinned", ".pem") discard setOverride(getChalkScope(), section & ".pinned_cert_file", some(pack(path))) # Can't delete from a dict while we're iterating over it. @@ -276,7 +276,7 @@ proc getSinkConfigByName*(name: string): Option[SinkConfig] = of "on_write_msg": discard of "log_search_path": - let boxOpt = getOpt[Box](getChalkScope(), section & "." & k) + let boxOpt = attrGetOpt[Box](section & "." & k) if boxOpt.isSome(): try: let path = unpack[seq[string]](boxOpt.get()) @@ -284,7 +284,7 @@ proc getSinkConfigByName*(name: string): Option[SinkConfig] = except: error(k & " (sink config key) must be a list of string paths.") of "headers": - let boxOpt = getOpt[Box](getChalkScope(), section & "." & k) + let boxOpt = attrGetOpt[Box](section & "." & k) if boxOpt.isSome(): try: let hdrs = unpack[Con4mDict[string, string]](boxOpt.get()) @@ -296,7 +296,7 @@ proc getSinkConfigByName*(name: string): Option[SinkConfig] = error(k & " (sink config key) must be a dict that map " & "header names to values (which must be strings).") of "timeout", "truncation_amount": - let boxOpt = getOpt[Box](getChalkScope(), section & "." & k) + let boxOpt = attrGetOpt[Box](section & "." & k) if boxOpt.isSome(): # TODO: move this check to the spec. if boxOpt.get().kind != MkInt: @@ -308,23 +308,23 @@ proc getSinkConfigByName*(name: string): Option[SinkConfig] = try: # Todo: move this check to a type check in the spec. # This will accept con4m size types; they're auto-converted to int. - let asInt = getOpt[int64](getChalkScope(), section & "." & k).getOrElse(int64(10 * 1048576)) + let asInt = attrGetOpt[int64](section & "." & k).getOrElse(int64(10 * 1048576)) opts[k] = $(asInt) except: error(k & " (sink config key) must be a size specification") continue of "filename": - opts[k] = getOpt[string](getChalkScope(), section & "." & k).getOrElse("") + opts[k] = attrGetOpt[string](section & "." & k).getOrElse("") try: opts[k] = resolvePath(opts[k]) except: warn(opts[k] & ": could not resolve sink filename. disabling sink") enabled = false else: - opts[k] = getOpt[string](getChalkScope(), section & "." & k).getOrElse("") + opts[k] = attrGetOpt[string](section & "." & k).getOrElse("") for item in deleteList: - getObject(getChalkScope(), section).contents.del(item) + attrGetObject(section).contents.del(item) case sinkName of "": @@ -347,10 +347,10 @@ proc getSinkConfigByName*(name: string): Option[SinkConfig] = opts["content_type"] = "application/json" of "file": if "log_search_path" notin opts: - opts["log_search_path"] = get[seq[string]](getChalkScope(), "log_search_path").join(":") + opts["log_search_path"] = attrGet[seq[string]]("log_search_path").join(":") of "rotating_log": if "log_search_path" notin opts: - opts["log_search_path"] = get[seq[string]](getChalkScope(), "log_search_path").join(":") + opts["log_search_path"] = attrGet[seq[string]]("log_search_path").join(":") else: discard @@ -392,14 +392,14 @@ proc getSinkConfigs*(): Table[string, SinkConfig] = return availableSinkConfigs proc setupDefaultLogConfigs*() = let - auditFile = get[string](getChalkScope(), "audit_location") - doAudit = get[bool](getChalkScope(), "publish_audit") + auditFile = attrGet[string]("audit_location") + doAudit = attrGet[bool]("publish_audit") if doAudit and auditFile != "": let f = some(newOrderedTable({ "filename" : auditFile, "max" : - $(get[Con4mSize](getChalkScope(), "audit_file_size"))})) + $(attrGet[Con4mSize]("audit_file_size"))})) sink = getSinkImplementation("rotating_log").get() auditConf = configSink(sink, "audit", f, handler=errCbOpt, logger=okCbOpt).get() @@ -410,7 +410,7 @@ proc setupDefaultLogConfigs*() = else: trace("Audit log subscription enabled") let - uri = get[string](getChalkScope(), "crashoverride_usage_reporting_url") + uri = attrGet[string]("crashoverride_usage_reporting_url") params = some(newOrderedTable({ "uri": uri, "content_type": "application/json" })) sink = getSinkImplementation("post").get() diff --git a/src/subscan.nim b/src/subscan.nim index 1ef09b6d..c21cc951 100644 --- a/src/subscan.nim +++ b/src/subscan.nim @@ -19,7 +19,7 @@ proc runChalkSubScan*(location: seq[string], cmd: string, suspendHost = true): CollectionCtx = let - oldRecursive = get[bool](getChalkScope(), "recursive") + oldRecursive = attrGet[bool]("recursive") oldCmd = getCommandName() oldArgs = getArgs() logLevel = getLogLevel() @@ -31,7 +31,7 @@ proc runChalkSubScan*(location: seq[string], var savedLogLevel: Option[LogLevel] - if logLevel > llError and not get[bool](getChalkScope(), "chalk_debug"): + if logLevel > llError and not attrGet[bool]("chalk_debug"): trace("*** Setting log-level = \"error\" for scan. Use --debug to turn on") savedLogLevel = some(logLevel) setLogLevel(llError) diff --git a/src/util.nim b/src/util.nim index 250bbb28..e62c94e0 100644 --- a/src/util.nim +++ b/src/util.nim @@ -29,7 +29,7 @@ proc regularTerminationSignal(signal: cint) {.noconv.} = try: error("pid: " & $(pid) & " - Aborting due to signal: " & sigNameMap[signal] & "(" & $(signal) & ")") - if get[bool](getChalkScope(), "chalk_debug"): + if attrGet[bool]("chalk_debug"): publish("debug", "Stack trace: \n" & getStackTrace()) except: @@ -73,13 +73,13 @@ proc reportTmpFileExitState*(files, dirs, errs: seq[string]) = for err in errs: error(err) - if get[bool](getChalkScope(), "chalk_debug") and len(dirs) + len(files) != 0: + if attrGet[bool]("chalk_debug") and len(dirs) + len(files) != 0: error("Due to --debug flag, skipping cleanup; moving the " & "following to ./chalk-tmp:") for item in files & dirs: error(item) - if get[bool](getChalkScope(), "report_total_time"): + if attrGet[bool]("report_total_time"): echo "Total run time: " & $(int(getMonoTime().ticks() - startTime) / 1000000000) & " seconds" @@ -103,7 +103,7 @@ proc canOpenFile*(path: string, mode: FileMode = FileMode.fmRead): bool = return canOpen proc setupManagedTemp*() = - let customTmpDirOpt = getOpt[string](getChalkScope(), "default_tmp_dir") + let customTmpDirOpt = attrGetOpt[string]("default_tmp_dir") if customTmpDirOpt.isSome() and not existsEnv("TMPDIR"): putenv("TMPDIR", customTmpDirOpt.get()) @@ -114,7 +114,7 @@ proc setupManagedTemp*() = if existsEnv("TMPDIR"): discard existsOrCreateDir(getEnv("TMPDIR")) - if get[bool](getChalkScope(), "chalk_debug"): + if attrGet[bool]("chalk_debug"): let tmpPath = resolvePath("chalk-tmp") tmpCheck = resolvePath(".chalk-tmp-check") @@ -220,7 +220,7 @@ const currentAutocompleteVersion = (0, 1, 3) proc validateMetaData*(obj: ChalkObj): ValidateResult {.importc.} proc autocompleteFileCheck*() = - if isatty(0) == 0 or get[bool](getChalkScope(), "install_completion_script") == false: + if isatty(0) == 0 or attrGet[bool]("install_completion_script") == false: return var dst = "" @@ -298,7 +298,7 @@ template otherSetupTasks*() = autocompleteFileCheck() if isatty(1) == 0: setShowColor(false) - limitFDCacheSize(get[int](getChalkScope(), "cache_fd_limit")) + limitFDCacheSize(attrGet[int]("cache_fd_limit")) var exitCode = 0