57 lines
943 B
Go
57 lines
943 B
Go
package common
|
|
|
|
import (
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type Manifest struct {
|
|
Subdirectories []string
|
|
}
|
|
|
|
func (m *Manifest) SubdirForPackage(pkg string) string {
|
|
if m == nil {
|
|
return pkg
|
|
}
|
|
|
|
idx := -1
|
|
matchLen := 0
|
|
basePkg := path.Base(pkg)
|
|
lowercasePkg := strings.ToLower(basePkg)
|
|
|
|
for i, sub := range m.Subdirectories {
|
|
basename := strings.ToLower(path.Base(sub))
|
|
if strings.HasPrefix(lowercasePkg, basename) && matchLen < len(basename) {
|
|
idx = i
|
|
matchLen = len(basename)
|
|
}
|
|
}
|
|
|
|
if idx > -1 {
|
|
return path.Join(m.Subdirectories[idx], basePkg)
|
|
}
|
|
return pkg
|
|
}
|
|
|
|
func ReadManifestFile(filename string) (*Manifest, error) {
|
|
data, err := os.ReadFile(filename)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return ParseManifestFile(data)
|
|
}
|
|
|
|
func ParseManifestFile(data []byte) (*Manifest, error) {
|
|
ret := &Manifest{}
|
|
err := yaml.Unmarshal(data, ret)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return ret, nil
|
|
}
|