From aaf03c75fccc6a66840651c283cdebb2f375a490 Mon Sep 17 00:00:00 2001 From: peusebiu Date: Mon, 10 Jul 2023 19:40:14 +0300 Subject: [PATCH] build: fix mgmt and userprefs when building them separately (#1601) move queryHasParams in common package fixes building mgmt and userprefs separately Signed-off-by: Petu Eusebiu --- pkg/common/http_server.go | 11 +++++++++++ pkg/extensions/extension_mgmt.go | 8 ++++---- pkg/extensions/extension_userprefs.go | 17 +++-------------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/pkg/common/http_server.go b/pkg/common/http_server.go index fd075039..df40b7f3 100644 --- a/pkg/common/http_server.go +++ b/pkg/common/http_server.go @@ -2,6 +2,7 @@ package common import ( "net/http" + "net/url" "strconv" "strings" "time" @@ -82,6 +83,16 @@ func WriteData(w http.ResponseWriter, status int, mediaType string, data []byte) _, _ = w.Write(data) } +func QueryHasParams(values url.Values, params []string) bool { + for _, param := range params { + if !values.Has(param) { + return false + } + } + + return true +} + /* GetAuthUserFromRequestSession returns identity and auth status if on the request's cookie session is a logged in user. diff --git a/pkg/extensions/extension_mgmt.go b/pkg/extensions/extension_mgmt.go index f06e5e7b..ea6a5d20 100644 --- a/pkg/extensions/extension_mgmt.go +++ b/pkg/extensions/extension_mgmt.go @@ -99,7 +99,7 @@ func (mgmt *mgmt) handler() http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var resource string - if queryHasParams(r.URL.Query(), []string{"resource"}) { + if zcommon.QueryHasParams(r.URL.Query(), []string{"resource"}) { resource = r.URL.Query().Get("resource") } else { resource = ConfigResource // default value of "resource" query param @@ -181,7 +181,7 @@ func (mgmt *mgmt) HandleGetConfig(w http.ResponseWriter, r *http.Request) { // @Failure 400 {string} string "bad request". // @Failure 500 {string} string "internal server error". func HandleCertificatesAndPublicKeysUploads(response http.ResponseWriter, request *http.Request) { - if !queryHasParams(request.URL.Query(), []string{"tool"}) { + if !zcommon.QueryHasParams(request.URL.Query(), []string{"tool"}) { response.WriteHeader(http.StatusBadRequest) return @@ -207,13 +207,13 @@ func HandleCertificatesAndPublicKeysUploads(response http.ResponseWriter, reques case signatures.NotationSignature: var truststoreType string - if !queryHasParams(request.URL.Query(), []string{"truststoreName"}) { + if !zcommon.QueryHasParams(request.URL.Query(), []string{"truststoreName"}) { response.WriteHeader(http.StatusBadRequest) return } - if queryHasParams(request.URL.Query(), []string{"truststoreType"}) { + if zcommon.QueryHasParams(request.URL.Query(), []string{"truststoreType"}) { truststoreType = request.URL.Query().Get("truststoreType") } else { truststoreType = "ca" // default value of "truststoreType" query param diff --git a/pkg/extensions/extension_userprefs.go b/pkg/extensions/extension_userprefs.go index 5bee652c..e4f23160 100644 --- a/pkg/extensions/extension_userprefs.go +++ b/pkg/extensions/extension_userprefs.go @@ -6,7 +6,6 @@ package extensions import ( "errors" "net/http" - "net/url" "github.com/gorilla/mux" @@ -59,7 +58,7 @@ func SetupUserPreferencesRoutes(config *config.Config, router *mux.Router, store // @Failure 400 {string} string "bad request". func HandleUserPrefs(repoDB repodb.RepoDB, log log.Logger) func(w http.ResponseWriter, r *http.Request) { return func(rsp http.ResponseWriter, req *http.Request) { - if !queryHasParams(req.URL.Query(), []string{"action"}) { + if !zcommon.QueryHasParams(req.URL.Query(), []string{"action"}) { rsp.WriteHeader(http.StatusBadRequest) return @@ -85,7 +84,7 @@ func HandleUserPrefs(repoDB repodb.RepoDB, log log.Logger) func(w http.ResponseW } func PutStar(rsp http.ResponseWriter, req *http.Request, repoDB repodb.RepoDB, log log.Logger) { - if !queryHasParams(req.URL.Query(), []string{"repo"}) { + if !zcommon.QueryHasParams(req.URL.Query(), []string{"repo"}) { rsp.WriteHeader(http.StatusBadRequest) return @@ -120,7 +119,7 @@ func PutStar(rsp http.ResponseWriter, req *http.Request, repoDB repodb.RepoDB, l } func PutBookmark(rsp http.ResponseWriter, req *http.Request, repoDB repodb.RepoDB, log log.Logger) { - if !queryHasParams(req.URL.Query(), []string{"repo"}) { + if !zcommon.QueryHasParams(req.URL.Query(), []string{"repo"}) { rsp.WriteHeader(http.StatusBadRequest) return @@ -153,13 +152,3 @@ func PutBookmark(rsp http.ResponseWriter, req *http.Request, repoDB repodb.RepoD rsp.WriteHeader(http.StatusOK) } - -func queryHasParams(values url.Values, params []string) bool { - for _, param := range params { - if !values.Has(param) { - return false - } - } - - return true -}