Lot errors from GetRepositories

This commit is contained in:
Melon 2024-03-16 02:00:50 +00:00
parent 03820c48de
commit 30379b0080
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
4 changed files with 23 additions and 8 deletions

View File

@ -55,7 +55,10 @@ func main() {
if err != nil {
log.Fatal("[GoMVN] Error: invalid database: ", err)
}
mustHaveUser(db)
err = mustHaveUser(db)
if err != nil {
log.Fatal("[GoMVN] Error: failed to ensure at least one user exists: ", err)
}
repoBasePath := filepath.Join(wd, "repositories")
err = os.MkdirAll(repoBasePath, os.ModePerm)

View File

@ -8,12 +8,12 @@ import (
"time"
)
func GetRepositories(basePath string, repository []string) map[string][]*database.Artifact {
func GetRepositories(basePath string, repository []string) (map[string][]*database.Artifact, error) {
result := map[string][]*database.Artifact{}
for _, repo := range repository {
result[repo] = []*database.Artifact{}
repoPath := filepath.Join(basePath, repo)
_ = filepath.Walk(repoPath, func(path string, info os.FileInfo, err error) error {
err := filepath.Walk(repoPath, func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".pom") {
path = strings.Replace(path, "\\", "/", -1)
path = strings.Replace(path, repoPath+"/", "", 1)
@ -22,8 +22,11 @@ func GetRepositories(basePath string, repository []string) map[string][]*databas
}
return nil
})
if err != nil {
return nil, err
}
return result
}
return result, nil
}
func newArtifact(p string, mod time.Time) *database.Artifact {

View File

@ -14,7 +14,7 @@
{{range $k, $v := .Repositories}}
<ul>
{{range $v}}
<li><a href="{{ $k }}/{{ .GetPath }}" class="dir">{{ .Group }}:{{ .Artifact }}:{{ .Version }}</a>, last
<li><a href="{{ $k }}/{{ .GetPath }}" class="dir">{{ .MvnGroup }}:{{ .Artifact }}:{{ .Version }}</a>, last
modified {{ .Modified }}</li>
{{end}}
</ul>

View File

@ -9,6 +9,7 @@ import (
"github.com/thanhpk/randstr"
"html/template"
"io"
"log"
"net/http"
"os"
"path"
@ -90,11 +91,19 @@ var indexHtml string
var indexTemplate = template.Must(template.New("index").Parse(indexHtml))
func (r *routeCtx) handleIndex(rw http.ResponseWriter, req *http.Request, params httprouter.Params) {
_ = indexTemplate.Execute(rw, map[string]any{
func (r *routeCtx) handleIndex(rw http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
repositories, err := paths.GetRepositories(r.basePath, r.repository)
if err != nil {
http.Error(rw, "500 Internal Server Error", http.StatusInternalServerError)
return
}
err = indexTemplate.Execute(rw, map[string]any{
"Name": r.name,
"Repositories": paths.GetRepositories(r.basePath, r.repository),
"Repositories": repositories,
})
if err != nil {
log.Println("[GoMVN] Index template error: ", err)
}
}
func (r *routeCtx) handlePut(rw http.ResponseWriter, req *http.Request, params httprouter.Params) {