Skip to content

Commit

Permalink
refactor: remove remaining uses of ChalkConfig (#330)
Browse files Browse the repository at this point in the history
Continue (from past commits [1][2][3][4]) to prepare for the new con4m,
removing the last usage of autogenerated types: ChalkConfig itself.

In particular, this commit removes from the confload module's
loadLocalStructs() procedure:

    chalkConfig = state.attrs.loadChalkConfig()

but intentionally makes minimal changes there otherwise. That is, all
the addCallback(loadLocalStructs) calls still exist for now, but they
now do less.

After this commit it's straightforward to remove c4autoconf entirely,
because there's exactly zero remaining use of autogenerated procs and
types.

Refs: #214

[1] 66a5bb4, "refactor: begin to avoid autogenerated getters"
[2] 4157b58, "refactor: reduce usage of autogenerated getters, part 2"
[3] 855197e, "refactor(confload, subscan): remove chalkConfig field accesses"
[4] bad5618, "refactor: remove remaining chalkConfig field accesses"
  • Loading branch information
ee7 authored Jun 18, 2024
1 parent bad5618 commit 9df364b
Show file tree
Hide file tree
Showing 36 changed files with 147 additions and 156 deletions.
8 changes: 4 additions & 4 deletions src/attestation/backup.nim
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ proc restore(self: Backup,
proc initCallback(this: AttestationKeyProvider) =
let
self = Backup(this)
authName = get[string](chalkConfig, "attestation.attestation_key_backup.auth")
location = get[string](chalkConfig, "attestation.attestation_key_backup.location")
authName = get[string](getChalkScope(), "attestation.attestation_key_backup.auth")
location = get[string](getChalkScope(), "attestation.attestation_key_backup.location")
authOpt = getAuthConfigByName(authName)
url = get[string](chalkConfig, "attestation.attestation_key_backup.uri").removeSuffix("/")
timeout = cast[int](get[Con4mDuration](chalkConfig, "attestation.attestation_key_backup.timeout"))
url = get[string](getChalkScope(), "attestation.attestation_key_backup.uri").removeSuffix("/")
timeout = cast[int](get[Con4mDuration](getChalkScope(), "attestation.attestation_key_backup.timeout"))

if authOpt.isNone():
raise newException(ValueError,
Expand Down
2 changes: 1 addition & 1 deletion src/attestation/embed.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Embed = ref object of AttestationKeyProvider
proc initCallback(this: AttestationKeyProvider) =
let
self = Embed(this)
location = get[string](chalkConfig, "attestation.attestation_key_embed.location")
location = get[string](getChalkScope(), "attestation.attestation_key_embed.location")
self.location = location

proc generateKeyCallback(this: AttestationKeyProvider): AttestationKey =
Expand Down
6 changes: 3 additions & 3 deletions src/attestation/get.nim
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ proc request(self: Get, query = ""): JsonNode =
proc initCallback(this: AttestationKeyProvider) =
let
self = Get(this)
authName = get[string](chalkConfig, "attestation.attestation_key_get.auth")
authName = get[string](getChalkScope(), "attestation.attestation_key_get.auth")
authOpt = getAuthConfigByName(authName)
url = get[string](chalkConfig, "attestation.attestation_key_get.uri").removeSuffix("/")
timeout = cast[int](get[Con4mDuration](chalkConfig, "attestation.attestation_key_get.timeout"))
url = get[string](getChalkScope(), "attestation.attestation_key_get.uri").removeSuffix("/")
timeout = cast[int](get[Con4mDuration](getChalkScope(), "attestation.attestation_key_get.timeout"))

if authOpt.isNone():
raise newException(ValueError,
Expand Down
12 changes: 6 additions & 6 deletions src/attestation_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ proc getCosignKey(chalk: ChalkObj): AttestationKey =
return AttestationKey(publicKey: pubKey)

proc getProvider(): AttestationKeyProvider =
let name = get[string](chalkConfig, "attestation.key_provider")
let name = get[string](getChalkScope(), "attestation.key_provider")
if name notin keyProviders:
raise newException(KeyError, "Unsupported attestation key provider: " & name)
return keyProviders[name]
Expand Down Expand Up @@ -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](chalkConfig, "always_try_to_sign"):
if isSubscribedKey("SIGNATURE") or get[bool](getChalkScope(), "always_try_to_sign"):
return true
trace("Artifact signing not configured.")
return false
Expand All @@ -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](chalkConfig, "use_transparency_log")
log = get[bool](getChalkScope(), "use_transparency_log")
cosign = getCosignLocation()
var
dict = ChalkDict()
Expand Down Expand Up @@ -255,7 +255,7 @@ proc signBySigStore*(chalk: ChalkObj): ChalkDict =
name = image.repo
spec = image.withDigest(chalk.imageDigest).asRepoDigest()
mark = chalk.getChalkMarkAsStr()
log = get[bool](chalkConfig, "use_transparency_log")
log = get[bool](getChalkScope(), "use_transparency_log")
cosign = getCosignLocation()
args = @["attest",
"--tlog-upload=" & $log,
Expand Down Expand Up @@ -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](chalkConfig, "use_transparency_log")
log = get[bool](getChalkScope(), "use_transparency_log")
args = @["sign-blob",
"--tlog-upload=" & $log,
"--yes",
Expand Down Expand Up @@ -366,7 +366,7 @@ proc verifyByHash*(chalk: ChalkObj, mdHash: string): ValidateResult =

let
sig = unpack[string](chalk.extract["SIGNATURE"])
noTlog = not get[bool](chalkConfig, "use_transparency_log")
noTlog = not get[bool](getChalkScope(), "use_transparency_log")
args = @["verify-blob",
"--insecure-ignore-tlog=" & $noTlog,
"--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(get[seq[string]](chalkConfig, "artifact_search_path"))
of "extract": runCmdExtract(get[seq[string]](getChalkScope(), "artifact_search_path"))
of "extract.containers": runCmdExtractContainers()
of "extract.images": runCmdExtractImages()
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 "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 "env": runCmdEnv()
of "dump": runCmdConfDump()
of "dump.params": runCmdConfDumpParams()
Expand Down
3 changes: 1 addition & 2 deletions src/chalk_common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -471,15 +471,14 @@ var
doingTestRun* = false
nativeCodecsOnly* = false
passedHelpFlag* = false
chalkConfig*: ChalkConfig
con4mRuntime*: ConfigStack
commandName*: string
gitExeLocation*: string = ""
sshKeyscanExeLocation*: string = ""
dockerInvocation*: DockerInvocation

template dumpExOnDebug*() =
if chalkConfig != nil and get[bool](chalkConfig, "chalk_debug"):
if getChalkScope() != nil and get[bool](getChalkScope(), "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 @@ -261,7 +261,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]](chalkConfig, "ignore_patterns")[i])
get[seq[string]](getChalkScope(), "ignore_patterns")[i])
trace("Developers: codecs should not be returning ignored artifacts.")
return true

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

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

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

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

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

for item in skipList:
result.skips.add(re(item))
Expand Down
28 changes: 14 additions & 14 deletions src/commands/cmd_exec.nim
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ proc doHeartbeatReport(chalkOpt: Option[ChalkObj]) =

template doHeartbeat(chalkOpt: Option[ChalkObj], pid: Pid, fn: untyped) =
let
inMicroSec = int(get[Con4mDuration](chalkConfig, "exec.heartbeat_rate"))
inMicroSec = int(get[Con4mDuration](getChalkScope(), "exec.heartbeat_rate"))
sleepInterval = int(inMicroSec / 1000)

setCommandName("heartbeat")
Expand All @@ -187,16 +187,16 @@ proc runCmdExec*(args: seq[string]) =
return

let
fromArgs = get[bool](chalkConfig, "exec.command_name_from_args")
cmdPath = get[seq[string]](chalkConfig, "exec.search_path")
defaults = get[seq[string]](chalkConfig, "exec.default_args")
appendArgs = get[bool](chalkConfig, "exec.append_command_line_args")
overrideOk = get[bool](chalkConfig, "exec.override_ok")
usePath = get[bool](chalkConfig, "exec.use_path")
pct = get[int](chalkConfig, "exec.reporting_probability")
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")
ppid = getpid() # Get the current pid before we fork.
var
cmdName = get[string](chalkConfig, "exec.command_name")
cmdName = get[string](getChalkScope(), "exec.command_name")

var argsToPass = defaults

Expand Down Expand Up @@ -238,7 +238,7 @@ proc runCmdExec*(args: seq[string]) =

let pid = fork()

if get[bool](chalkConfig, "exec.chalk_as_parent"):
if get[bool](getChalkScope(), "exec.chalk_as_parent"):
if pid == 0:
handleExec(allOpts, argsToPass)
elif pid == -1:
Expand All @@ -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](chalkConfig, "exec.initial_sleep_time"))
inMicroSec = int(get[Con4mDuration](getChalkScope(), "exec.initial_sleep_time"))
initialSleep = int(inMicroSec / 1000)

sleep(initialSleep)
Expand All @@ -265,7 +265,7 @@ proc runCmdExec*(args: seq[string]) =
chalkOpt.get().collectRunTimeArtifactInfo()
doReporting()

if get[bool](chalkConfig, "exec.heartbeat"):
if get[bool](getChalkScope(), "exec.heartbeat"):
chalkOpt.doHeartbeatAsParent(pid)
else:
trace("Waiting for spawned process to exit.")
Expand All @@ -286,7 +286,7 @@ proc runCmdExec*(args: seq[string]) =
trace("Chalk is child process: " & $(cpid))

let
inMicroSec = int(get[Con4mDuration](chalkConfig, "exec.initial_sleep_time"))
inMicroSec = int(get[Con4mDuration](getChalkScope(), "exec.initial_sleep_time"))
initialSleep = int(inMicroSec / 1000)

sleep(initialSleep)
Expand All @@ -305,5 +305,5 @@ proc runCmdExec*(args: seq[string]) =
chalkOpt.get().collectRunTimeArtifactInfo()
doReporting()

if get[bool](chalkConfig, "exec.heartbeat"):
if get[bool](getChalkScope(), "exec.heartbeat"):
chalkOpt.doHeartbeatAsChild(ppid)
8 changes: 4 additions & 4 deletions src/commands/cmd_help.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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](chalkConfig, "default_command").get("")
let defaultCmd = getOpt[string](getChalkScope(), "default_command").get("")
if defaultCmd != "" and defaultCmd notin commandLineParams():
toOut = con4mRuntime.getHelpOverview()
else:
Expand Down Expand Up @@ -545,7 +545,7 @@ proc runChalkHelp*(cmdName = "help") {.noreturn.} =
toOut = con4mRuntime.fullTextSearch(args)
break

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

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

let toOut = getConfigValues()
if get[bool](chalkConfig, "use_pager"):
if get[bool](getChalkScope(), "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, selfextract, collect, reporting, chalkjson, plugin_api]
proc runCmdInsert*(path: seq[string]) {.exportc,cdecl.} =
setContextDirectories(path)
initCollection()
let virtual = get[bool](chalkConfig, "virtual_chalk")
let virtual = get[bool](getChalkScope(), "virtual_chalk")

for item in artifacts(path):
trace(item.name & ": begin chalking")
Expand Down
1 change: 0 additions & 1 deletion src/config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

## Wrappers for more abstracted accessing of configuration information

from pkg/con4m import get, getOpt
import "."/[run_management, config_version]
export run_management

Expand Down
19 changes: 9 additions & 10 deletions src/confload.nim
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,10 @@ proc findOptionalConf(state: ConfigState): Option[string] =
trace(fname & ": No configuration file found.")

proc loadLocalStructs*(state: ConfigState) =
chalkConfig = state.attrs.loadChalkConfig()
if getOpt[bool](chalkConfig, "color").isSome(): setShowColor(get[bool](chalkConfig, "color"))
setLogLevel(get[string](chalkConfig, "log_level"))
if getOpt[bool](getChalkScope(), "color").isSome(): setShowColor(get[bool](getChalkScope(), "color"))
setLogLevel(get[string](getChalkScope(), "log_level"))
var configPath: seq[string] = @[]
for path in get[seq[string]](chalkConfig, "config_path"):
for path in get[seq[string]](getChalkScope(), "config_path"):
try:
configPath.add(path.resolvePath())
except:
Expand All @@ -99,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](chalkConfig, "con4m_pinpoint"): c4vShowLoc else: c4vBasic
var c4errLevel = if get[bool](getChalkScope(), "con4m_pinpoint"): c4vShowLoc else: c4vBasic

if get[bool](chalkConfig, "chalk_debug"):
if get[bool](getChalkScope(), "chalk_debug"):
c4errLevel = if c4errLevel == c4vBasic: c4vTrace else: c4vMax

setCon4mVerbosity(c4errLevel)

proc handleCon4mErrors(err, tb: string): bool =
if tb != "" and chalkConfig == nil or get[bool](chalkConfig, "chalk_debug"):
if tb != "" and getChalkScope() == nil or get[bool](getChalkScope(), "chalk_debug"):
echo(formatCompilerError(err, nil, tb, default(InstInfo)))
else:
error(err)
Expand Down Expand Up @@ -191,12 +190,12 @@ proc loadAllConfigs*() =
# The embedded config has already been validated.
let configFile = getEmbeddedConfig()

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

if get[bool](chalkConfig, "load_external_config"):
if get[bool](getChalkScope(), "load_external_config"):
let optConf = stack.configState.findOptionalConf()
if optConf.isSome():
let fName = optConf.get()
Expand All @@ -207,7 +206,7 @@ proc loadAllConfigs*() =
doRun()
hostInfo["_OP_CONFIG"] = pack(configFile)

if commandName == "not_supplied" and getOpt[string](chalkConfig, "default_command").isSome():
if commandName == "not_supplied" and getOpt[string](getChalkScope(), "default_command").isSome():
setErrorHandler(stack, handleOtherErrors)
addFinalizeGetOpts(stack, printAutoHelp = false)
addCallback(stack, loadLocalStructs)
Expand Down
2 changes: 1 addition & 1 deletion src/docker/base.nim
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ proc dockerPassThrough*(ctx: DockerInvocation) {.noreturn.} =
exitCode = runCmdNoOutputCapture(exe,
ctx.originalArgs,
ctx.originalStdIn)
if get[bool](chalkConfig, "docker.report_unwrapped_commands"):
if get[bool](getChalkScope(), "docker.report_unwrapped_commands"):
reporting.doReporting("report")
except:
dumpExOnDebug()
Expand Down
16 changes: 8 additions & 8 deletions src/docker/build.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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]](chalkConfig, "docker.custom_labels")
let labelOpt = getOpt[TableRef[string, string]](getChalkScope(), "docker.custom_labels")
if labelOpt.isSome():
ctx.newCmdLine.addLabelArgs(labelOpt.get())
let labelTemplName = get[string](chalkConfig, "docker.label_template")
let labelTemplName = get[string](getChalkScope(), "docker.label_template")
if labelTemplName == "":
return
let
Expand All @@ -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]](chalkConfig, "docker.custom_labels")
let labelOpt = getOpt[TableRef[string, string]](getChalkScope(), "docker.custom_labels")
if labelOpt.isSome():
ctx.addedInstructions.addLabelCmds(labelOpt.get())
let labelTemplName = get[string](chalkConfig, "docker.label_template")
let labelTemplName = get[string](getChalkScope(), "docker.label_template")
if labelTemplName == "":
return
let
Expand All @@ -137,7 +137,7 @@ proc addEnvVars(ctx: DockerInvocation, chalk: ChalkObj) =
var
toAdd: seq[string] = @[]
value: string
for k, v in get[TableRef[string, string]](chalkConfig, "docker.additional_env_vars"):
for k, v in get[TableRef[string, string]](getChalkScope(), "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.")
Expand Down Expand Up @@ -337,9 +337,9 @@ proc dockerBuild*(ctx: DockerInvocation): int =
# multi-platform builds should have same chalk id
chalkId = ctx.chalkId,
)
wrapVirtual = get[bool](chalkConfig, "virtual_chalk")
wrapEntrypoint = get[bool](chalkConfig, "docker.wrap_entrypoint")
dockerSubscan = get[bool](chalkConfig, "chalk_contained_items")
wrapVirtual = get[bool](getChalkScope(), "virtual_chalk")
wrapEntrypoint = get[bool](getChalkScope(), "docker.wrap_entrypoint")
dockerSubscan = get[bool](getChalkScope(), "chalk_contained_items")

trace("docker: processing build CLI args")
ctx.processGitContext()
Expand Down
Loading

0 comments on commit 9df364b

Please sign in to comment.