fix(ci): fix ci lint fmt err

This commit is contained in:
sky5454 2026-03-31 05:33:34 +08:00
parent 9f74143103
commit 29ae8b5de8

View file

@ -1,52 +1,52 @@
package api package api
import ( import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"github.com/sipeed/picoclaw/pkg/updater" "github.com/sipeed/picoclaw/pkg/updater"
) )
// registerUpdateRoutes registers the self-update endpoint. // registerUpdateRoutes registers the self-update endpoint.
func (h *Handler) registerUpdateRoutes(mux *http.ServeMux) { func (h *Handler) registerUpdateRoutes(mux *http.ServeMux) {
mux.HandleFunc("/api/update", h.handleUpdate) mux.HandleFunc("/api/update", h.handleUpdate)
} }
type updateRequest struct { type updateRequest struct {
URL string `json:"url,omitempty"` URL string `json:"url,omitempty"`
Binary string `json:"binary,omitempty"` Binary string `json:"binary,omitempty"`
} }
type updateResponse struct { type updateResponse struct {
Status string `json:"status"` Status string `json:"status"`
Message string `json:"message,omitempty"` Message string `json:"message,omitempty"`
} }
func (h *Handler) handleUpdate(w http.ResponseWriter, r *http.Request) { func (h *Handler) handleUpdate(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed) w.WriteHeader(http.StatusMethodNotAllowed)
_ = json.NewEncoder(w).Encode(updateResponse{Status: "error", Message: "method not allowed"}) _ = json.NewEncoder(w).Encode(updateResponse{Status: "error", Message: "method not allowed"})
return return
} }
dec := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)) dec := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20))
var req updateRequest var req updateRequest
if err := dec.Decode(&req); err != nil { if err := dec.Decode(&req); err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
_ = json.NewEncoder(w).Encode(updateResponse{Status: "error", Message: "invalid request body"}) _ = json.NewEncoder(w).Encode(updateResponse{Status: "error", Message: "invalid request body"})
return return
} }
binary := req.Binary binary := req.Binary
if binary == "" { if binary == "" {
binary = "picoclaw-launcher" binary = "picoclaw-launcher"
} }
if err := updater.UpdateSelfFromRelease(req.URL, "", "", binary); err != nil { if err := updater.UpdateSelfFromRelease(req.URL, "", "", binary); err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
_ = json.NewEncoder(w).Encode(updateResponse{Status: "error", Message: err.Error()}) _ = json.NewEncoder(w).Encode(updateResponse{Status: "error", Message: err.Error()})
return return
} }
_ = json.NewEncoder(w).Encode(updateResponse{Status: "ok", Message: "update applied; restart to use new version"}) _ = json.NewEncoder(w).Encode(updateResponse{Status: "ok", Message: "update applied; restart to use new version"})
} }