Skip to content

Commit 66f1c3b

Browse files
committed
slackme: exit with command code and quote command
1 parent 0068642 commit 66f1c3b

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

slackme/main.test.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe("slackme", async () => {
3535
expect(exec.exitCode).toBe(0);
3636
exec = await execContainer(id, ["sh", "-c", "slackme"]);
3737
expect(exec.stdout.trim()).toStartWith(
38-
"slackme — Send a Slack notification when a command finishes",
38+
"slackme — Send a Slack notification when a command finishes"
3939
);
4040
});
4141

@@ -56,6 +56,15 @@ describe("slackme", async () => {
5656
});
5757
});
5858

59+
it("exits with command code", async () => {
60+
const { instance, id } = await setupContainer();
61+
await writeCoder(id, "echo 'some-url' && exit 1");
62+
let exec = await execContainer(id, ["sh", "-c", instance.script]);
63+
expect(exec.exitCode).toBe(0);
64+
exec = await execContainer(id, ["sh", "-c", "slackme exit 1"]);
65+
expect(exec.exitCode).toBe(1);
66+
});
67+
5968
it("formats multiline message", async () => {
6069
await assertSlackMessage({
6170
command: "echo test",
@@ -107,7 +116,7 @@ executed`,
107116

108117
const setupContainer = async (
109118
image = "alpine",
110-
vars: Record<string, string> = {},
119+
vars: Record<string, string> = {}
111120
) => {
112121
const state = await runTerraformApply(import.meta.dir, {
113122
agent_id: "foo",
@@ -150,7 +159,7 @@ const assertSlackMessage = async (opts: {
150159
"alpine/curl",
151160
opts.format && {
152161
slack_message: opts.format,
153-
},
162+
}
154163
);
155164
await writeCoder(id, "echo 'token'");
156165
let exec = await execContainer(id, ["sh", "-c", instance.script]);

slackme/slackme.sh

+4-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ fi
7272

7373
START=$(date +%s%N)
7474
# Run all arguments as a command
75-
$@
75+
"$@"
76+
CODE=$?
7677
END=$(date +%s%N)
7778
DURATION_MS=$${DURATION_MS:-$(( (END - START) / 1000000 ))}
7879
PRETTY_DURATION=$(pretty_duration $DURATION_MS)
@@ -85,3 +86,5 @@ SLACK_MESSAGE=$(echo "$SLACK_MESSAGE" | sed "s|\\$DURATION|$PRETTY_DURATION|g")
8586
curl --silent -o /dev/null --header "Authorization: Bearer $BOT_TOKEN" \
8687
-G --data-urlencode "text=$${SLACK_MESSAGE}" \
8788
"$SLACK_URL/api/chat.postMessage?channel=$USER_ID&pretty=1"
89+
90+
exit $CODE

0 commit comments

Comments
 (0)