feat(hook): expand environment variables.
This commit is contained in:
parent
a4b4be1fd4
commit
34c8608c9f
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -27,7 +28,7 @@ func execute(command string) error {
|
|||||||
ctxCmd, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
ctxCmd, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
parts := strings.Fields(command)
|
parts := strings.Fields(os.ExpandEnv(command))
|
||||||
output, err := exec.CommandContext(ctxCmd, parts[0], parts[1:]...).CombinedOutput()
|
output, err := exec.CommandContext(ctxCmd, parts[0], parts[1:]...).CombinedOutput()
|
||||||
if len(output) > 0 {
|
if len(output) > 0 {
|
||||||
fmt.Println(string(output))
|
fmt.Println(string(output))
|
||||||
|
|||||||
29
hook/hook_test.go
Normal file
29
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user