gomvn/paths/repo.go

42 lines
1.0 KiB
Go
Raw Normal View History

2024-03-04 13:11:06 +00:00
package paths
import (
2024-03-15 15:13:09 +00:00
"github.com/1f349/gomvn/database"
2024-03-04 13:11:06 +00:00
"os"
"path/filepath"
"strings"
2024-03-15 15:13:09 +00:00
"time"
2024-03-04 13:11:06 +00:00
)
2024-03-16 02:00:50 +00:00
func GetRepositories(basePath string, repository []string) (map[string][]*database.Artifact, error) {
2024-03-15 15:13:09 +00:00
result := map[string][]*database.Artifact{}
2024-03-04 13:11:06 +00:00
for _, repo := range repository {
2024-03-15 15:13:09 +00:00
result[repo] = []*database.Artifact{}
2024-03-04 13:11:06 +00:00
repoPath := filepath.Join(basePath, repo)
2024-03-16 02:00:50 +00:00
err := filepath.Walk(repoPath, func(path string, info os.FileInfo, err error) error {
2024-03-04 13:11:06 +00:00
if strings.HasSuffix(path, ".pom") {
path = strings.Replace(path, "\\", "/", -1)
path = strings.Replace(path, repoPath+"/", "", 1)
2024-03-15 15:13:09 +00:00
artifact := newArtifact(path, info.ModTime())
2024-03-04 13:11:06 +00:00
result[repo] = append(result[repo], artifact)
}
return nil
})
2024-03-16 02:00:50 +00:00
if err != nil {
return nil, err
}
2024-03-04 13:11:06 +00:00
}
2024-03-16 02:00:50 +00:00
return result, nil
2024-03-04 13:11:06 +00:00
}
2024-03-15 15:13:09 +00:00
func newArtifact(p string, mod time.Time) *database.Artifact {
parts := strings.Split(p, "/")
last := len(parts) - 1
return &database.Artifact{
MvnGroup: strings.Join(parts[0:last-2], "."),
Artifact: parts[last-2],
Version: parts[last-1],
Modified: mod,
}
}