Move Notarius path and executable resolution into its adapter
This commit is contained in:
37
internal/adapters/notarius/paths.go
Normal file
37
internal/adapters/notarius/paths.go
Normal file
@@ -0,0 +1,37 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user