38 lines
1001 B
Go
38 lines
1001 B
Go
package notarius
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
var unsafeLaneCharacters = regexp.MustCompile(`[^a-zA-Z0-9._-]`)
|
|
|
|
// LaneRelativePath maps a native lane identifier to its bundle-relative JSON path.
|
|
func LaneRelativePath(laneID string) (string, error) {
|
|
name := unsafeLaneCharacters.ReplaceAllString(strings.TrimSpace(laneID), "_")
|
|
for strings.Contains(name, "..") {
|
|
name = strings.ReplaceAll(name, "..", "__")
|
|
}
|
|
name = strings.Trim(name, "._")
|
|
if name == "" {
|
|
return "", fmt.Errorf("notarius lane ID %q cannot produce a filename", laneID)
|
|
}
|
|
return "lanes/" + name + ".json", nil
|
|
}
|
|
|
|
// ResolveExecutable resolves an explicit binary or PATH entry to an absolute path.
|
|
func ResolveExecutable(value string) (string, error) {
|
|
resolved, err := exec.LookPath(strings.TrimSpace(value))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
resolved, err = filepath.Abs(strings.TrimSpace(resolved))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Clean(resolved), nil
|
|
}
|