webhookd/pkg/hook/helper.go
2023-12-22 22:10:19 +00:00

25 lines
508 B
Go

package hook
import (
"errors"
"os"
"path"
"strings"
)
// ResolveScript is resolving the target script.
func ResolveScript(dir, name, defaultExt string) (string, error) {
if path.Ext(name) == "" {
name += "." + defaultExt
}
script := path.Clean(path.Join(dir, name))
if !strings.HasPrefix(script, dir) {
return "", errors.New("Invalid script path: " + name)
}
if _, err := os.Stat(script); os.IsNotExist(err) {
return "", errors.New("Script not found: " + script)
}
return script, nil
}