diff --git a/cmd/gendoc/docs.go b/cmd/gendoc/docs.go index 8e85e2a8..bc64f0a3 100644 --- a/cmd/gendoc/docs.go +++ b/cmd/gendoc/docs.go @@ -1042,8 +1042,9 @@ Contains a JSON object with the details of an error. Method: http.MethodDelete, Path: "/v1/apps/{appID}/sketch/libraries/{libRef}", Parameters: (*struct { - ID string `path:"appID" description:"application identifier."` - LibRef string `path:"libRef" description:"library reference (\"LibraryName\" or \"LibraryName@Version\")."` + ID string `path:"appID" description:"application identifier."` + LibRef string `path:"libRef" description:"library reference (\"LibraryName\" or \"LibraryName@Version\")."` + RemoveDependencies string `query:"remove_deps" description:"if set to \"true\", the library's dependencies will be removed as well if not needed anymore."` })(nil), CustomSuccessResponse: &CustomResponseDef{ ContentType: "application/json", diff --git a/go.mod b/go.mod index be40aceb..38a21b31 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ replace ( ) // Required until https://github.com/arduino/arduino-cli/pull/3019 is merged and released -replace github.com/arduino/arduino-cli => github.com/cmaglie/arduino-cli v0.0.0-20251006122726-27fe68b0a18a +replace github.com/arduino/arduino-cli => github.com/cmaglie/arduino-cli v0.0.0-20251029100020-2327b357349d require ( github.com/Andrew-M-C/go.emoji v1.1.4 diff --git a/go.sum b/go.sum index 98b1009f..5be6213d 100644 --- a/go.sum +++ b/go.sum @@ -170,8 +170,8 @@ github.com/cloudflare/cfssl v0.0.0-20180223231731-4e2dcbde5004 h1:lkAMpLVBDaj17e github.com/cloudflare/cfssl v0.0.0-20180223231731-4e2dcbde5004/go.mod h1:yMWuSON2oQp+43nFtAV/uvKQIFpSPerB57DCt9t8sSA= github.com/cloudflare/circl v1.6.1 h1:zqIqSPIndyBh1bjLVVDHMPpVKqp8Su/V+6MeDzzQBQ0= github.com/cloudflare/circl v1.6.1/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZh3pJrofs= -github.com/cmaglie/arduino-cli v0.0.0-20251006122726-27fe68b0a18a h1:xjGwmLNEUTb+QWUmUxwrb24Awbt4/rdpFG7lVUR3skI= -github.com/cmaglie/arduino-cli v0.0.0-20251006122726-27fe68b0a18a/go.mod h1:aH/Lfub80ymf3vpF0gUWx/sckVUNB0gDoPlABAx97SE= +github.com/cmaglie/arduino-cli v0.0.0-20251029100020-2327b357349d h1:Hpm/DaAji25rxW1JPr1DK7A1wnUygOw0ftAs6UiHAq0= +github.com/cmaglie/arduino-cli v0.0.0-20251029100020-2327b357349d/go.mod h1:aH/Lfub80ymf3vpF0gUWx/sckVUNB0gDoPlABAx97SE= github.com/cmaglie/pb v1.0.27 h1:ynGj8vBXR+dtj4B7Q/W/qGt31771Ux5iFfRQBnwdQiA= github.com/cmaglie/pb v1.0.27/go.mod h1:GilkKZMXYjBA4NxItWFfO+lwkp59PLHQ+IOW/b/kmZI= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= diff --git a/internal/api/docs/openapi.yaml b/internal/api/docs/openapi.yaml index 577d77d1..2e418cf8 100644 --- a/internal/api/docs/openapi.yaml +++ b/internal/api/docs/openapi.yaml @@ -302,6 +302,14 @@ paths: from the sketch project file. operationId: appSketchRemoveLibrary parameters: + - description: if set to "true", the library's dependencies will be removed + as well if not needed anymore. + in: query + name: remove_deps + schema: + description: if set to "true", the library's dependencies will be removed + as well if not needed anymore. + type: string - description: application identifier. in: path name: appID diff --git a/internal/api/handlers/app_sketch_libs.go b/internal/api/handlers/app_sketch_libs.go index 43969572..03919794 100644 --- a/internal/api/handlers/app_sketch_libs.go +++ b/internal/api/handlers/app_sketch_libs.go @@ -90,12 +90,14 @@ func HandleSketchRemoveLibrary(idProvider *app.IDProvider) http.HandlerFunc { return } - if removedLib, err := orchestrator.RemoveSketchLibrary(r.Context(), app, libRef); err != nil { + // Get query param addDeps (default false) + removeDeps, _ := strconv.ParseBool(r.URL.Query().Get("remove_deps")) + if removedLibs, err := orchestrator.RemoveSketchLibrary(r.Context(), app, libRef, removeDeps); err != nil { render.EncodeResponse(w, http.StatusInternalServerError, models.ErrorResponse{Details: "unable to remove sketch library"}) return } else { render.EncodeResponse(w, http.StatusOK, SketchRemoveLibraryResponse{ - RemovedLibraries: []orchestrator.LibraryReleaseID{removedLib}, + RemovedLibraries: removedLibs, }) return } diff --git a/internal/orchestrator/sketch_libs.go b/internal/orchestrator/sketch_libs.go index d28c9ba8..15858808 100644 --- a/internal/orchestrator/sketch_libs.go +++ b/internal/orchestrator/sketch_libs.go @@ -46,9 +46,9 @@ func AddSketchLibrary(ctx context.Context, app app.ArduinoApp, libRef LibraryRel resp, err := srv.ProfileLibAdd(ctx, &rpc.ProfileLibAddRequest{ Instance: inst, SketchPath: app.MainSketchPath.String(), - Library: &rpc.SketchProfileLibraryReference{ - Library: &rpc.SketchProfileLibraryReference_IndexLibrary_{ - IndexLibrary: &rpc.SketchProfileLibraryReference_IndexLibrary{ + Library: &rpc.ProfileLibraryReference{ + Library: &rpc.ProfileLibraryReference_IndexLibrary_{ + IndexLibrary: &rpc.ProfileLibraryReference_IndexLibrary{ Name: libRef.Name, Version: libRef.Version, }, @@ -62,11 +62,11 @@ func AddSketchLibrary(ctx context.Context, app app.ArduinoApp, libRef LibraryRel return f.Map(resp.GetAddedLibraries(), rpcProfileLibReferenceToLibReleaseID), nil } -func RemoveSketchLibrary(ctx context.Context, app app.ArduinoApp, libRef LibraryReleaseID) (LibraryReleaseID, error) { +func RemoveSketchLibrary(ctx context.Context, app app.ArduinoApp, libRef LibraryReleaseID, removeDeps bool) ([]LibraryReleaseID, error) { srv := commands.NewArduinoCoreServer() var inst *rpc.Instance if res, err := srv.Create(ctx, &rpc.CreateRequest{}); err != nil { - return LibraryReleaseID{}, err + return nil, err } else { inst = res.Instance } @@ -77,23 +77,25 @@ func RemoveSketchLibrary(ctx context.Context, app app.ArduinoApp, libRef Library // TODO: LOG progress/error? return nil })); err != nil { - return LibraryReleaseID{}, err + return nil, err } resp, err := srv.ProfileLibRemove(ctx, &rpc.ProfileLibRemoveRequest{ - Library: &rpc.SketchProfileLibraryReference{ - Library: &rpc.SketchProfileLibraryReference_IndexLibrary_{ - IndexLibrary: &rpc.SketchProfileLibraryReference_IndexLibrary{ + Instance: inst, + Library: &rpc.ProfileLibraryReference{ + Library: &rpc.ProfileLibraryReference_IndexLibrary_{ + IndexLibrary: &rpc.ProfileLibraryReference_IndexLibrary{ Name: libRef.Name, }, }, }, - SketchPath: app.MainSketchPath.String(), + SketchPath: app.MainSketchPath.String(), + RemoveDependencies: &removeDeps, }) if err != nil { - return LibraryReleaseID{}, err + return nil, err } - return rpcProfileLibReferenceToLibReleaseID(resp.GetLibrary()), nil + return f.Map(resp.GetRemovedLibraries(), rpcProfileLibReferenceToLibReleaseID), nil } func ListSketchLibraries(ctx context.Context, app app.ArduinoApp) ([]LibraryReleaseID, error) { @@ -107,19 +109,17 @@ func ListSketchLibraries(ctx context.Context, app app.ArduinoApp) ([]LibraryRele } // Keep only index libraries - libs := f.Filter(resp.Libraries, func(l *rpc.SketchProfileLibraryReference) bool { + libs := f.Filter(resp.Libraries, func(l *rpc.ProfileLibraryReference) bool { return l.GetIndexLibrary() != nil }) - res := f.Map(libs, func(l *rpc.SketchProfileLibraryReference) LibraryReleaseID { - return LibraryReleaseID{ - Name: l.GetIndexLibrary().GetName(), - Version: l.GetIndexLibrary().GetVersion(), - } - }) - return res, nil + return f.Map(libs, rpcProfileLibReferenceToLibReleaseID), nil } -func rpcProfileLibReferenceToLibReleaseID(ref *rpc.SketchProfileLibraryReference) LibraryReleaseID { +func rpcProfileLibReferenceToLibReleaseID(ref *rpc.ProfileLibraryReference) LibraryReleaseID { l := ref.GetIndexLibrary() - return NewLibraryReleaseID(l.GetName(), l.GetVersion()) + return LibraryReleaseID{ + Name: l.GetName(), + Version: l.GetVersion(), + IsDependency: l.GetIsDependency(), + } } diff --git a/internal/orchestrator/sketch_libs_release_id.go b/internal/orchestrator/sketch_libs_release_id.go index ef068f46..dec8b105 100644 --- a/internal/orchestrator/sketch_libs_release_id.go +++ b/internal/orchestrator/sketch_libs_release_id.go @@ -27,8 +27,9 @@ import ( // - name[@version] // Version is optional, if not provided, the latest version available will be used. type LibraryReleaseID struct { - Name string - Version string + Name string + Version string + IsDependency bool } func NewLibraryReleaseID(name string, version string) LibraryReleaseID {