libct/configs: add HookList.SetDefaultEnv

1. Make CommandHook.Command a pointer, which reduces the amount of data
   being copied when using hooks, and allows to modify command hooks.

2. Add SetDefaultEnv, which is to be used by the next commit.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin
2024-12-22 17:32:02 -08:00
committed by lfbzhm
parent c49b891681
commit 6171da6005
6 changed files with 29 additions and 15 deletions
+13 -3
View File
@@ -434,6 +434,16 @@ func (hooks Hooks) Run(name HookName, state *specs.State) error {
return nil
}
// SetDefaultEnv sets the environment for those CommandHook entries
// that do not have one set.
func (hooks HookList) SetDefaultEnv(env []string) {
for _, h := range hooks {
if ch, ok := h.(CommandHook); ok && len(ch.Env) == 0 {
ch.Env = env
}
}
}
type Hook interface {
// Run executes the hook with the provided state.
Run(*specs.State) error
@@ -463,17 +473,17 @@ type Command struct {
}
// NewCommandHook will execute the provided command when the hook is run.
func NewCommandHook(cmd Command) CommandHook {
func NewCommandHook(cmd *Command) CommandHook {
return CommandHook{
Command: cmd,
}
}
type CommandHook struct {
Command
*Command
}
func (c Command) Run(s *specs.State) error {
func (c *Command) Run(s *specs.State) error {
b, err := json.Marshal(s)
if err != nil {
return err