refactor: updated cmd and pkg implementations

This commit is contained in:
David Allen 2025-08-30 23:30:46 -06:00
parent d88ab2c01f
commit fbed466c3d
Signed by: towk
GPG key ID: 0430CDBE22619155
10 changed files with 287 additions and 196 deletions

View file

@ -88,13 +88,10 @@ func (s *Service) GetProfile() http.HandlerFunc {
func (s *Service) CreateProfile() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
type input struct {
Path string `json:"path"`
Profile *makeshift.Profile `json:"profile"`
}
var (
body, contents []byte
in input
path string
profile *makeshift.Profile
err error
)
@ -105,23 +102,24 @@ func (s *Service) CreateProfile() http.HandlerFunc {
}
// use the request info to build profile
err = json.Unmarshal(body, &in)
err = json.Unmarshal(body, &profile)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
http.Error(w, fmt.Sprintf("failed to unmarshal profile: %v", err.Error()), http.StatusBadRequest)
return
}
// serialize just the profile part
contents, err = json.Marshal(in.Profile)
contents, err = json.Marshal(profile)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
http.Error(w, fmt.Sprintf("failed to marshal profile: %v", err.Error()), http.StatusBadRequest)
return
}
// create a new profile on disk
err = os.WriteFile(in.Path, contents, os.ModePerm)
path = s.PathForProfileWithID(profile.ID)
err = os.WriteFile(path, contents, os.ModePerm)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@ -129,6 +127,29 @@ func (s *Service) CreateProfile() http.HandlerFunc {
}
}
func (s *Service) DeleteProfile() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
profileID = chi.URLParam(r, "id")
path string
err error
)
if profileID == "default" {
http.Error(w, "cannot delete the default profile", http.StatusBadRequest)
return
}
path = s.PathForProfileWithID(profileID)
err = os.Remove(path)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
}
func (s *Service) SetProfileData() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (