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

How to do something based on exit code of command? #1436

Closed
Boscop opened this issue Dec 4, 2022 · 7 comments
Closed

How to do something based on exit code of command? #1436

Boscop opened this issue Dec 4, 2022 · 7 comments

Comments

@Boscop
Copy link

Boscop commented Dec 4, 2022

How can I do something based on the exit code of a command?
E.g. I have to call an exe that returns 1 upon success and 0 upon failure, and I want to check its exit code
if ["{{COMPILER}}" "/compile:{{DATA_DIR}}/file" /log -ne 1]; then echo "Failed"; else echo "Finished"; fi

Also, in case of error, this exe writes to a file at the path specified by env var LOGFILE.
But it seems even if I export it (export LOGFILE := "foo.log"), the process doesn't see this env var, and thus the file doesn't get written to. Any idea why? When called from a bat file, it works. But not when called from sh via just.

This is the bat file I want to replace by my justfile, how can I do the equivalent checks? I'd greatly appreciate any hints :)

@echo off

if "%~1" == "" (
	echo "You must specify the first parameter to this batch file."
	goto end
)

set LOGFILE="%~dpn1.log"

"%COMPILER%" /compile:%1 /log
:: 0 = failure
:: 1 = success
set ERR=%ERRORLEVEL%

:: This dumps the log file to stdout so it can be seen
type "%LOGFILE%"

:: Once shown, get rid of the .log file
erase "%LOGFILE%"

if %ERR% NEQ 1 (
	echo "Failed"
	exit /b 1
) else (
	echo "Finished"
)

:end
@casey
Copy link
Owner

casey commented Dec 4, 2022

I suggest writing a bash recipe:

foo:
  #!/usr/bin/env bash
  some_command
  if [[ $? == 100 ]]; then
    do_something
  fi

@casey casey closed this as completed Dec 4, 2022
@ajeetdsouza
Copy link

@casey bash recipes aren't portable to Windows, and defeat the purpose of a justfile for me. Would you reconsider this issue?

Related: #1187

@casey
Copy link
Owner

casey commented Jan 18, 2023

@ajeetdsouza Can you use powershell? If you don't have bash, powershell is pretty reasonable.

set windows-shell := ["powershell.exe", "-NoLogo", "-Command"]

hello:
  Write-Host "Hello, world!"

@ajeetdsouza
Copy link

I did consider using PowerShell / Python / Nushell, but that would mean yet another tool I have to reliably install in CI.

I'm currently using an xtask to avoid this, but I'd be willing to migrate if just provided a few more advanced features.

@casey
Copy link
Owner

casey commented Jan 18, 2023

This is windows CI? I'm not familiar with what ships natively, but it might already have powershell.exe or pwsh.exe.

@ajeetdsouza
Copy link

I want CI to run on Windows / macOS / Linux uniformly, and PowerShell is not bundled with macOS / Linux.

@casey
Copy link
Owner

casey commented Jan 18, 2023

Ahh, I see. Can you open a new issue with your specific use case? I'd have to see exactly what you're trying to do to see if there's a good way to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants