Skip to content

Commit c1b3ae8

Browse files
authored
Add "exec" trigger. (#367)
I'm not 100% sure this is a good idea, as it could easily lead to performance issues, but in some situations I think it could be quite useful.
1 parent 33034b6 commit c1b3ae8

File tree

5 files changed

+21
-0
lines changed

5 files changed

+21
-0
lines changed

app/exec_cmd.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package app
22

33
import (
4+
"fmt"
45
"net/http"
56
"os"
67
"path/filepath"
@@ -65,6 +66,16 @@ func (e *execCmd) Run(l *ui.UI, cache *cache.Cache, sta *state.State, env *hermi
6566
if err := pkg.EnsureSupported(); err != nil {
6667
return errors.Wrapf(err, "execution failed")
6768
}
69+
70+
// Run any pre-execution triggers.
71+
messages, err := env.TriggerForPackage(l, manifest.EventExec, pkg)
72+
if err != nil {
73+
return errors.WithStack(err)
74+
}
75+
w := l.WriterAt(ui.LevelInfo)
76+
for _, message := range messages {
77+
fmt.Fprintln(w, message)
78+
}
6879
installed, err := env.ListInstalledReferences()
6980
if err != nil {
7081
return errors.WithStack(err)

docs/docs/packaging/reference.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ be applied when certain events occur in the package lifecycle. Supported events
133133
|---------------|-------------|
134134
| `unpack` | Triggered when a package is unpacked into the Hermit cache. |
135135
| `install` | Triggered when a package is installed into an environment. |
136+
| `uninstall` | Triggered when a package is uninstalled from an environment. |
136137
| `activate` | Triggered when the environment the package is installed in is activated. |
138+
| `exec` | Triggered whenever a binary in the package is executed. <br>**NOTE:** This trigger will run for _every_ execution and can negatively impact performance. |
137139

138140
More triggers may be added in the future.

integration/integration_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ func TestIntegration(t *testing.T) {
252252
hermit install testbin1
253253
testbin1
254254
testbin2
255+
assert test -d exec-hook-triggered
255256
`,
256257
expectations: exp{outputContains("testbin1 1.0.1")},
257258
},

integration/testdata/testenv3/packages/testbin1.hcl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ on unpack {
88
symlink { from = "${root}/testbin1" to = "${root}/dir/testbin2" }
99
}
1010

11+
on exec {
12+
// Messages can't be sent to the parent process, so we create a directory to indicate the hook worked instead.
13+
mkdir { dir = "${env}/exec-hook-triggered" }
14+
}
15+
1116
version "1.0.0" "1.0.1" {}

manifest/triggers.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const (
2525
EventUnpack Event = "unpack"
2626
EventInstall Event = "install"
2727
EventUninstall Event = "uninstall"
28+
EventExec Event = "exec" // Triggered when a binary in a package is executed.
2829
// Environment specific events
2930
EventEnvActivate Event = "activate"
3031
)
@@ -35,6 +36,7 @@ var eventMap = map[Event]bool{
3536
EventInstall: true,
3637
EventUninstall: true,
3738
EventEnvActivate: true,
39+
EventExec: true,
3840
}
3941

4042
// A Trigger applied when a lifecycle event occurs.

0 commit comments

Comments
 (0)