giredore/domains/server/v1/goimports.go

51 lines
1.6 KiB
Go
Raw Normal View History

package serverv1
import (
"net/http"
2019-10-07 20:39:11 +05:00
"strings"
"github.com/labstack/echo"
"go.dev.pztrn.name/giredore/internal/configuration"
"go.dev.pztrn.name/giredore/internal/structs"
)
func throwGoImports(ectx echo.Context) error {
2019-10-07 20:39:11 +05:00
// Getting real path. This might be the package itself, or namespace
// to list available packages.
// For now only package itself is supported, all other features in ToDo.
packageNameRaw := ectx.Request().URL.Path
2019-10-16 23:32:21 +05:00
2019-10-07 20:39:11 +05:00
pkgs, errs := configuration.Cfg.GetPackagesInfo([]string{packageNameRaw})
2019-10-16 23:32:21 +05:00
2019-10-07 20:39:11 +05:00
if errs != nil {
log.Error().Str("package", packageNameRaw).Msgf("Failed to get package information: %+v", errs)
// nolint:exhaustruct,wrapcheck
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errs})
2019-10-07 20:39:11 +05:00
}
if len(pkgs) == 0 {
// nolint:exhaustruct,wrapcheck
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrNoPackagesFound}})
2019-10-07 20:39:11 +05:00
}
pkg, found := pkgs[packageNameRaw]
if !found {
// nolint:exhaustruct,wrapcheck
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrNoPackagesFound}})
2019-10-07 20:39:11 +05:00
}
// We should compose package name using our domain under which giredore
// is working.
domain := ectx.Request().Host
2019-10-07 20:39:11 +05:00
packageName := domain + packageNameRaw
tmpl := singlePackageTemplate
tmpl = strings.Replace(tmpl, "{PKGNAME}", packageName, -1)
tmpl = strings.Replace(tmpl, "{VCS}", pkg.VCS, 1)
tmpl = strings.Replace(tmpl, "{REPOPATH}", pkg.RealPath, 1)
// nolint:wrapcheck
return ectx.HTML(http.StatusOK, tmpl)
}