Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow bypassing chalk via CHALK_BYPASS environment variable #228

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/bypass.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import std/[os, osproc, parseopt, strformat, strutils]

type
bypassCmd = enum
bcNone
bcDocker = "docker"
bcExec = "exec"

proc err(msg: string) =
## Writes `msg` to stderr, then quits with an exit code of 1.
stderr.writeLine &"error: {msg}"
quit 1

proc handleBypass*() =
## Does nothing when the `CHALK_BYPASS` environment variable is unset.
##
## However, if that environment variable is set, and the command is:
##
## - `docker`: executes the rest of the command line with docker, without chalk.
##
## - `exec`: executes the rest of the command line, without chalk.
##
## - Something else or missing: quits with exit code 1.
const bypassEnvVar = "CHALK_BYPASS"

if getEnv(bypassEnvVar) == "":
return

var p = initOptParser()
var bypassCmd = bcNone
const msg =
&"{bypassEnvVar} was set, but the chalk command is not '{bcDocker}' or '{bcExec}'. Quitting."

for kind, key, _ in getopt(p):
case kind
of cmdArgument:
case key
of $bcDocker, $bcExec:
bypassCmd = parseEnum[bypassCmd](key)
break
else:
err(msg)
of cmdShortOption, cmdLongOption:
# Ignore any option that appears before an argument.
discard
of cmdEnd:
err(msg)

if bypassCmd == bcNone:
err(msg)

# Run the rest of the command line that appears after `docker` or `exec`.
let cmd =
case bypassCmd
of bcDocker:
$bcDocker & " " & cmdLineRest(p)
of bcExec:
cmdLineRest(p)
of bcNone:
doAssert false
"" # Cannot happen.

stderr.writeLine &"chalk: the {bypassEnvVar} environment variable is set."
stderr.writeLine &"Running the following command without chalk:\n {cmd}"
let exitCode = execCmd(cmd)
quit exitCode
3 changes: 2 additions & 1 deletion src/chalk.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

# Note that imports cause topics and plugins to register.
{.warning[UnusedImport]: off.}
import "."/[config, confload, commands, norecurse, sinks, docker_base,
import "."/[bypass, config, confload, commands, norecurse, sinks, docker_base,
attestation, util]

when isMainModule:
handleBypass() # bypass.nim
setupSignalHandlers() # util.nim
setupTerminal() # util.nim
ioSetup() # sinks.nim
Expand Down
Loading