Skip to content

Commit

Permalink
refactor: add plugin.enabled flag
Browse files Browse the repository at this point in the history
There are places where plugins are loaded explicitly by name
and so conditionally registering codecs leads to KeyErrors.
By adding enabled flag we can gracefully ignore some callbacks
from plugin definitions without breaking anything relying on plugin
existence.
  • Loading branch information
miki725 committed Mar 22, 2024
1 parent 3683886 commit c029f43
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/chalk_common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type
c: Option[string]) {.cdecl.}
Plugin* = ref object
name*: string
enabled*: bool
configInfo*: PluginSpec
getChalkTimeHostInfo*: ChalkTimeHostCb
getChalkTimeArtifactInfo*: ChalkTimeArtifactCb
Expand Down
21 changes: 18 additions & 3 deletions src/plugin_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import "."/[config, chalkjson, util]
# should all be pre-checked.

proc callGetChalkTimeHostInfo*(plugin: Plugin): ChalkDict =
if not plugin.enabled:
return ChalkDict()

let cb = plugin.getChalkTimeHostInfo

# explicit callback check - otherwise it results in segfault
Expand All @@ -31,6 +34,9 @@ proc callGetChalkTimeHostInfo*(plugin: Plugin): ChalkDict =

proc callGetChalkTimeArtifactInfo*(plugin: Plugin, obj: ChalkObj):
ChalkDict =
if not plugin.enabled:
return ChalkDict()

let cb = plugin.getChalkTimeArtifactInfo

# explicit callback check - otherwise it results in segfault
Expand All @@ -42,6 +48,9 @@ proc callGetChalkTimeArtifactInfo*(plugin: Plugin, obj: ChalkObj):

proc callGetRunTimeArtifactInfo*(plugin: Plugin, obj: ChalkObj, b: bool):
ChalkDict =
if not plugin.enabled:
return ChalkDict()

let cb = plugin.getRunTimeArtifactInfo

# explicit callback check - otherwise it results in segfault
Expand All @@ -53,6 +62,9 @@ proc callGetRunTimeArtifactInfo*(plugin: Plugin, obj: ChalkObj, b: bool):

proc callGetRunTimeHostInfo*(plugin: Plugin, objs: seq[ChalkObj]):
ChalkDict =
if not plugin.enabled:
return ChalkDict()

let cb = plugin.getRunTimeHostInfo

# explicit callback check - otherwise it results in segfault
Expand Down Expand Up @@ -594,7 +606,8 @@ proc newPlugin*(
getChalkTimeArtifactInfo: ctArtCallback,
getRunTimeArtifactInfo: rtArtCallback,
getRunTimeHostInfo: rtHostCallback,
internalState: cache)
internalState: cache,
enabled: true)

if not result.checkPlugin(codec = false):
result = Plugin(nil)
Expand All @@ -612,7 +625,8 @@ proc newCodec*(
handleWrite: HandleWriteCb = HandleWritecb(defaultCodecWrite),
nativeObjPlatforms: seq[string] = @[],
cache: RootRef = RootRef(nil),
commentStart: string = "#"):
commentStart: string = "#",
enabled: bool = true):
Plugin {.discardable, cdecl.} =

result = Plugin(name: name,
Expand All @@ -627,7 +641,8 @@ proc newCodec*(
handleWrite: handleWrite,
nativeObjPlatforms: nativeObjPlatforms,
internalState: cache,
commentStart: commentStart)
commentStart: commentStart,
enabled: enabled)

if not result.checkPlugin(codec = true):
result = Plugin(nil)
11 changes: 6 additions & 5 deletions src/plugins/codecDocker.nim
Original file line number Diff line number Diff line change
Expand Up @@ -601,9 +601,10 @@ proc dockerExtractChalkMark*(chalk: ChalkObj): ChalkDict {.exportc, cdecl.} =
proc loadCodecDocker*() =
# cant use getDockerExePath as that uses codecs to ignore chalk
# wrappings hence we just check if anything docker is on PATH here
if nimutils.findExePath("docker") == "":
let enabled = nimutils.findExePath("docker") != ""
if not enabled:
warn("Disabling docker codec as docker command is not available")
else:
newCodec("docker",
rtArtCallback = RunTimeArtifactCb(dockerGetRunTimeArtifactInfo),
getChalkId = ChalkIdCb(dockerGetChalkId))
newCodec("docker",
rtArtCallback = RunTimeArtifactCb(dockerGetRunTimeArtifactInfo),
getChalkId = ChalkIdCb(dockerGetChalkId),
enabled = enabled)

0 comments on commit c029f43

Please sign in to comment.