update repo
This commit is contained in:
42
traefik/traefik-certs-dumper/hook/hook.go
Normal file
42
traefik/traefik-certs-dumper/hook/hook.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package hook
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Exec Execute a command on a go routine.
|
||||
func Exec(command string) {
|
||||
if command == "" {
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
errH := execute(command)
|
||||
if errH != nil {
|
||||
panic(errH)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func execute(command string) error {
|
||||
ctxCmd, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
parts := strings.Fields(os.ExpandEnv(command))
|
||||
output, err := exec.CommandContext(ctxCmd, parts[0], parts[1:]...).CombinedOutput()
|
||||
if len(output) > 0 {
|
||||
fmt.Println(string(output))
|
||||
}
|
||||
|
||||
if ctxCmd.Err() == context.DeadlineExceeded {
|
||||
return errors.New("hook timed out")
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
29
traefik/traefik-certs-dumper/hook/hook_test.go
Normal file
29
traefik/traefik-certs-dumper/hook/hook_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package hook
|
||||
|
||||
import "testing"
|
||||
|
||||
func Test_execute(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
command string
|
||||
}{
|
||||
{
|
||||
desc: "expand env vars",
|
||||
command: `echo "${GOPATH} ${GOARCH}"`,
|
||||
},
|
||||
{
|
||||
desc: "simple",
|
||||
command: `echo 'hello'`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
|
||||
err := execute(test.command)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user