File: hook.go

package info (click to toggle)
golang-github-xenolf-lego 4.9.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,076 kB
  • sloc: xml: 533; makefile: 130; sh: 18
file content (47 lines) | stat: -rw-r--r-- 786 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package cmd

import (
	"context"
	"errors"
	"fmt"
	"os"
	"os/exec"
	"strings"
	"time"
)

func launchHook(hook string, meta map[string]string) error {
	if hook == "" {
		return nil
	}

	ctxCmd, cancel := context.WithTimeout(context.Background(), 120*time.Second)
	defer cancel()

	parts := strings.Fields(hook)

	cmdCtx := exec.CommandContext(ctxCmd, parts[0], parts[1:]...)
	cmdCtx.Env = append(os.Environ(), metaToEnv(meta)...)

	output, err := cmdCtx.CombinedOutput()

	if len(output) > 0 {
		fmt.Println(string(output))
	}

	if errors.Is(ctxCmd.Err(), context.DeadlineExceeded) {
		return errors.New("hook timed out")
	}

	return err
}

func metaToEnv(meta map[string]string) []string {
	var envs []string

	for k, v := range meta {
		envs = append(envs, k+"="+v)
	}

	return envs
}