feat: updated cmd/pkg implementations and cleanup

This commit is contained in:
David Allen 2025-08-31 11:48:49 -06:00
parent cb3d4ce8db
commit 8e1fa3d2ab
Signed by: towk
GPG key ID: 0430CDBE22619155
2 changed files with 88 additions and 68 deletions

View file

@ -88,33 +88,33 @@ func (s *Service) GetPluginRaw() http.HandlerFunc {
func (s *Service) CreatePlugin() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
plugin makeshift.Plugin
path string
err error
pluginName = chi.URLParam(r, "name")
body []byte
path string
err error
)
plugin, err = getPluginFromRequestBody(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// helper to check for valid plugin name
var hasValidName = func(name string) bool {
return name != "" && len(name) < 64
}
// check for a valid plugin name
if !hasValidName(plugin.Name()) {
if !hasValidName(pluginName) {
http.Error(w, "invalid name for plugin", http.StatusBadRequest)
return
}
body, err = io.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// save plugin at path using it's name
path = s.PathForPluginWithName(plugin.Name())
err = SavePluginToFile(path, &plugin)
path = s.PathForPluginWithName(pluginName)
err = os.WriteFile(path, body, 0o777)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
@ -139,22 +139,3 @@ func (s *Service) DeletePlugin() http.HandlerFunc {
w.WriteHeader(http.StatusOK)
}
}
func getPluginFromRequestBody(r *http.Request) (makeshift.Plugin, error) {
var (
plugin makeshift.Plugin
body []byte
err error
)
body, err = io.ReadAll(r.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &plugin)
if err != nil {
return nil, err
}
return plugin, nil
}