Skip to content

Commit 89e8f9f

Browse files
mirkoCrobulucarin91
authored andcommitted
add useByApps field for brick details endpoint
1 parent f61b73f commit 89e8f9f

File tree

6 files changed

+100
-11
lines changed

6 files changed

+100
-11
lines changed

cmd/arduino-app-cli/brick/bricks.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@ package brick
1717

1818
import (
1919
"github.com/spf13/cobra"
20+
21+
"github.com/arduino/arduino-app-cli/internal/orchestrator/config"
2022
)
2123

22-
func NewBrickCmd() *cobra.Command {
24+
func NewBrickCmd(cfg config.Configuration) *cobra.Command {
2325
appCmd := &cobra.Command{
2426
Use: "brick",
2527
Short: "Manage Arduino Bricks",
2628
}
2729

2830
appCmd.AddCommand(newBricksListCmd())
29-
appCmd.AddCommand(newBricksDetailsCmd())
31+
appCmd.AddCommand(newBricksDetailsCmd(cfg))
3032

3133
return appCmd
3234
}

cmd/arduino-app-cli/brick/details.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,23 @@ import (
2525
"github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/internal/servicelocator"
2626
"github.com/arduino/arduino-app-cli/cmd/feedback"
2727
"github.com/arduino/arduino-app-cli/internal/orchestrator/bricks"
28+
"github.com/arduino/arduino-app-cli/internal/orchestrator/config"
2829
)
2930

30-
func newBricksDetailsCmd() *cobra.Command {
31+
func newBricksDetailsCmd(cfg config.Configuration) *cobra.Command {
3132
return &cobra.Command{
3233
Use: "details",
3334
Short: "Details of a specific brick",
3435
Args: cobra.ExactArgs(1),
3536
Run: func(cmd *cobra.Command, args []string) {
36-
bricksDetailsHandler(args[0])
37+
bricksDetailsHandler(args[0], cfg)
3738
},
3839
}
3940
}
4041

41-
func bricksDetailsHandler(id string) {
42-
res, err := servicelocator.GetBrickService().BricksDetails(id)
42+
func bricksDetailsHandler(id string, cfg config.Configuration) {
43+
res, err := servicelocator.GetBrickService().BricksDetails(id, servicelocator.GetAppIDProvider(),
44+
cfg)
4345
if err != nil {
4446
if errors.Is(err, bricks.ErrBrickNotFound) {
4547
feedback.Fatal(err.Error(), feedback.ErrBadArgument)

cmd/arduino-app-cli/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func run(configuration cfg.Configuration) error {
7171

7272
rootCmd.AddCommand(
7373
app.NewAppCmd(configuration),
74-
brick.NewBrickCmd(),
74+
brick.NewBrickCmd(configuration),
7575
completion.NewCompletionCommand(),
7676
daemon.NewDaemonCmd(configuration, Version),
7777
properties.NewPropertiesCmd(configuration),

internal/api/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func NewHTTPRouter(
5656
mux.Handle("GET /v1/version", handlers.HandlerVersion(version))
5757
mux.Handle("GET /v1/config", handlers.HandleConfig(cfg))
5858
mux.Handle("GET /v1/bricks", handlers.HandleBrickList(brickService))
59-
mux.Handle("GET /v1/bricks/{brickID}", handlers.HandleBrickDetails(brickService))
59+
mux.Handle("GET /v1/bricks/{brickID}", handlers.HandleBrickDetails(brickService, idProvider, cfg))
6060

6161
mux.Handle("GET /v1/properties", handlers.HandlePropertyKeys(cfg))
6262
mux.Handle("GET /v1/properties/{key}", handlers.HandlePropertyGet(cfg))

internal/api/handlers/bricks.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/arduino/arduino-app-cli/internal/api/models"
2727
"github.com/arduino/arduino-app-cli/internal/orchestrator/app"
2828
"github.com/arduino/arduino-app-cli/internal/orchestrator/bricks"
29+
"github.com/arduino/arduino-app-cli/internal/orchestrator/config"
2930
"github.com/arduino/arduino-app-cli/internal/render"
3031
)
3132

@@ -153,14 +154,15 @@ func HandleBrickCreate(
153154
}
154155
}
155156

156-
func HandleBrickDetails(brickService *bricks.Service) http.HandlerFunc {
157+
func HandleBrickDetails(brickService *bricks.Service, idProvider *app.IDProvider,
158+
cfg config.Configuration) http.HandlerFunc {
157159
return func(w http.ResponseWriter, r *http.Request) {
158160
id := r.PathValue("brickID")
159161
if id == "" {
160162
render.EncodeResponse(w, http.StatusBadRequest, models.ErrorResponse{Details: "id must be set"})
161163
return
162164
}
163-
res, err := brickService.BricksDetails(id)
165+
res, err := brickService.BricksDetails(id, idProvider, cfg)
164166
if err != nil {
165167
if errors.Is(err, bricks.ErrBrickNotFound) {
166168
details := fmt.Sprintf("brick with id %q not found", id)

internal/orchestrator/bricks/bricks.go

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package bricks
1818
import (
1919
"errors"
2020
"fmt"
21+
"log/slog"
2122
"maps"
2223
"slices"
2324

@@ -26,6 +27,7 @@ import (
2627

2728
"github.com/arduino/arduino-app-cli/internal/orchestrator/app"
2829
"github.com/arduino/arduino-app-cli/internal/orchestrator/bricksindex"
30+
"github.com/arduino/arduino-app-cli/internal/orchestrator/config"
2931
"github.com/arduino/arduino-app-cli/internal/orchestrator/modelsindex"
3032
"github.com/arduino/arduino-app-cli/internal/store"
3133
)
@@ -125,7 +127,8 @@ func (s *Service) AppBrickInstanceDetails(a *app.ArduinoApp, brickID string) (Br
125127
}, nil
126128
}
127129

128-
func (s *Service) BricksDetails(id string) (BrickDetailsResult, error) {
130+
func (s *Service) BricksDetails(id string, idProvider *app.IDProvider,
131+
cfg config.Configuration) (BrickDetailsResult, error) {
129132
brick, found := s.bricksIndex.FindBrickByID(id)
130133
if !found {
131134
return BrickDetailsResult{}, ErrBrickNotFound
@@ -160,6 +163,20 @@ func (s *Service) BricksDetails(id string) (BrickDetailsResult, error) {
160163
}
161164
})
162165

166+
/*qui mi serve una funzione per calcolare questo. devo iterare su ogni app, di esempio o no.
167+
ho già la funzione appList che mi può aiutare.
168+
e per ogni elemento se ho il bircikc id corrente
169+
mi creo un AppReference*/
170+
appList, err := getAppList(cfg)
171+
if err != nil {
172+
slog.Error("unable to get app list", slog.String("error", err.Error()))
173+
return BrickDetailsResult{}, fmt.Errorf("unable to get app list: %w", err)
174+
}
175+
usedByApps, err := getUsedByApps(appList, brick.ID, idProvider)
176+
if err != nil {
177+
slog.Error("unable to get used by apps", slog.String("error", err.Error()))
178+
return BrickDetailsResult{}, fmt.Errorf("unable to get used by apps: %w", err)
179+
}
163180
return BrickDetailsResult{
164181
ID: id,
165182
Name: brick.Name,
@@ -171,9 +188,75 @@ func (s *Service) BricksDetails(id string) (BrickDetailsResult, error) {
171188
Readme: readme,
172189
ApiDocsPath: apiDocsPath,
173190
CodeExamples: codeExamples,
191+
UsedByApps: usedByApps,
174192
}, nil
175193
}
176194

195+
func getAppList(
196+
cfg config.Configuration,
197+
) ([]app.ArduinoApp, error) {
198+
var (
199+
pathsToExplore paths.PathList
200+
appPaths paths.PathList
201+
)
202+
pathsToExplore.Add(cfg.ExamplesDir())
203+
pathsToExplore.Add(cfg.AppsDir())
204+
arduinoApps := []app.ArduinoApp{}
205+
206+
for _, p := range pathsToExplore {
207+
res, err := p.ReadDirRecursiveFiltered(func(file *paths.Path) bool {
208+
if file.Base() == ".cache" {
209+
return false
210+
}
211+
if file.Join("app.yaml").NotExist() && file.Join("app.yml").NotExist() {
212+
return true
213+
}
214+
return false
215+
}, paths.FilterDirectories(), paths.FilterOutNames("python", "sketch", ".cache"))
216+
217+
if err != nil {
218+
slog.Error("unable to list apps", slog.String("error", err.Error()))
219+
return arduinoApps, err
220+
}
221+
appPaths.AddAllMissing(res)
222+
}
223+
224+
for _, file := range appPaths {
225+
app, err := app.Load(file.String())
226+
if err != nil {
227+
/* result.BrokenApps = append(result.BrokenApps, orchestrator.BrokenAppInfo{
228+
Name: file.Base(),
229+
Error: fmt.Sprintf("unable to parse the app.yaml: %s", err.Error()),
230+
})*/
231+
continue
232+
}
233+
234+
arduinoApps = append(arduinoApps, app)
235+
}
236+
return arduinoApps, nil
237+
}
238+
239+
func getUsedByApps(apps []app.ArduinoApp, brickId string, idProvider *app.IDProvider) ([]AppReference, error) {
240+
usedByApps := []AppReference{}
241+
for _, app := range apps {
242+
for _, b := range app.Descriptor.Bricks {
243+
if b.ID == brickId {
244+
id, err := idProvider.IDFromPath(app.FullPath)
245+
if err != nil {
246+
return usedByApps, fmt.Errorf("failed to get app ID for %s: %w", app.Name, err)
247+
}
248+
usedByApps = append(usedByApps, AppReference{
249+
Name: app.Name,
250+
ID: id.String(),
251+
Icon: app.Descriptor.Icon,
252+
})
253+
break
254+
}
255+
}
256+
}
257+
return usedByApps, nil
258+
}
259+
177260
type BrickCreateUpdateRequest struct {
178261
ID string `json:"-"`
179262
Model *string `json:"model"`

0 commit comments

Comments
 (0)