2023-06-11 00:32:02 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2023-06-17 22:41:07 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-06-30 19:06:46 +00:00
|
|
|
"gosimplenpm/config"
|
2023-06-17 22:41:07 +00:00
|
|
|
"gosimplenpm/serviceidos"
|
|
|
|
"gosimplenpm/storage"
|
|
|
|
"io"
|
2023-06-11 00:32:02 +00:00
|
|
|
"net/http"
|
2023-06-17 22:41:07 +00:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/mod/semver"
|
2023-06-11 00:32:02 +00:00
|
|
|
)
|
|
|
|
|
2023-06-30 19:06:46 +00:00
|
|
|
func DistTagPut(lg *logrus.Logger, cfg config.Config) http.HandlerFunc {
|
2023-06-17 22:41:07 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
escapedName := mux.Vars(r)["name"]
|
|
|
|
packageName, _ := url.PathUnescape(escapedName)
|
2023-06-30 19:06:46 +00:00
|
|
|
lg.WithFields(logrus.Fields{
|
|
|
|
"function": "dist-tags-put",
|
|
|
|
}).Debugf("Package name => %s\n", packageName)
|
2023-06-17 22:41:07 +00:00
|
|
|
|
|
|
|
escapedName = mux.Vars(r)["tag"]
|
|
|
|
tag, _ := url.PathUnescape(escapedName)
|
2023-06-30 19:06:46 +00:00
|
|
|
lg.WithFields(logrus.Fields{
|
|
|
|
"function": "dist-tags-put",
|
|
|
|
}).Debugf("Tag => %s\n", tag)
|
2023-06-17 22:41:07 +00:00
|
|
|
|
|
|
|
if semver.IsValid(tag) {
|
|
|
|
http.Error(w, "Tag cannot be a semver version", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if tag == "latest" {
|
|
|
|
http.Error(w, "Cannot delete the latest tag", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body, _ := io.ReadAll(r.Body)
|
|
|
|
var version string
|
|
|
|
_ = json.Unmarshal(body, &version)
|
2023-06-30 19:06:46 +00:00
|
|
|
lg.WithFields(logrus.Fields{
|
|
|
|
"function": "dist-tags-put",
|
|
|
|
}).Debugf("Body => %s", version)
|
2023-06-17 22:41:07 +00:00
|
|
|
|
2023-06-30 19:06:46 +00:00
|
|
|
fileToServe, found, err := storage.GetIndexJsonFromStore(packageName, cfg.RepoDir, lg)
|
2023-06-17 22:41:07 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
ret := fmt.Sprintf("Package not found: %s", packageName)
|
|
|
|
http.Error(w, ret, http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsonFile serviceidos.IndexJson
|
2023-06-30 19:06:46 +00:00
|
|
|
err = storage.ReadIndexJson(fileToServe, &jsonFile, lg)
|
2023-06-17 22:41:07 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonFile.DistTags[tag] = version
|
|
|
|
|
|
|
|
// Write index.json
|
2023-06-30 19:06:46 +00:00
|
|
|
err = storage.WriteIndexJson(fileToServe, &jsonFile, lg)
|
2023-06-17 22:41:07 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
distTagsString, _ := json.Marshal(jsonFile.DistTags)
|
|
|
|
|
|
|
|
response := serviceidos.TagPutResponse{
|
|
|
|
Ok: true,
|
|
|
|
ID: escapedName,
|
|
|
|
DistTags: string(distTagsString),
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonString, _ := json.Marshal(response)
|
2023-06-11 00:32:02 +00:00
|
|
|
|
2023-06-17 22:41:07 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(jsonString)))
|
|
|
|
w.Write(jsonString)
|
|
|
|
}
|
2023-06-11 00:32:02 +00:00
|
|
|
}
|