Skip to content

Commit ad0cc81

Browse files
authored
feat(cmd): add update command (#559)
1 parent f1b02db commit ad0cc81

File tree

1 file changed

+94
-4
lines changed

1 file changed

+94
-4
lines changed
Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,122 @@
11
package system
22

33
import (
4+
"fmt"
5+
"strings"
6+
47
"github.com/spf13/cobra"
58

69
"github.com/bcmi-labs/orchestrator/cmd/arduino-app-cli/internal/servicelocator"
10+
"github.com/bcmi-labs/orchestrator/cmd/feedback"
711
"github.com/bcmi-labs/orchestrator/internal/orchestrator"
812
"github.com/bcmi-labs/orchestrator/internal/orchestrator/config"
13+
"github.com/bcmi-labs/orchestrator/internal/update"
14+
"github.com/bcmi-labs/orchestrator/internal/update/apt"
15+
"github.com/bcmi-labs/orchestrator/internal/update/arduino"
916
)
1017

1118
func NewSystemCmd(cfg config.Configuration) *cobra.Command {
1219
cmd := &cobra.Command{
13-
Use: "system",
14-
Hidden: true,
20+
Use: "system",
1521
}
1622

1723
cmd.AddCommand(newDownloadImage(cfg))
24+
cmd.AddCommand(newUpdateCmd())
1825

1926
return cmd
2027
}
2128

2229
func newDownloadImage(cfg config.Configuration) *cobra.Command {
2330
cmd := &cobra.Command{
24-
Use: "init",
25-
Args: cobra.ExactArgs(0),
31+
Use: "init",
32+
Args: cobra.ExactArgs(0),
33+
Hidden: true,
2634
RunE: func(cmd *cobra.Command, _ []string) error {
2735
return orchestrator.SystemInit(cmd.Context(), cfg.UsedPythonImageTag, servicelocator.GetStaticStore())
2836
},
2937
}
3038

3139
return cmd
3240
}
41+
42+
func newUpdateCmd() *cobra.Command {
43+
var onlyArduino bool
44+
var forceYes bool
45+
cmd := &cobra.Command{
46+
Use: "update",
47+
Args: cobra.ExactArgs(0),
48+
RunE: func(cmd *cobra.Command, _ []string) error {
49+
filterFunc := getFilterFunc(onlyArduino)
50+
51+
updater := getUpdater()
52+
53+
pkgs, err := updater.ListUpgradablePackages(cmd.Context(), filterFunc)
54+
if err != nil {
55+
return err
56+
}
57+
if len(pkgs) == 0 {
58+
feedback.Printf("No upgradable packages found.")
59+
return nil
60+
}
61+
62+
feedback.Printf("Found %d upgradable packages:", len(pkgs))
63+
for _, pkg := range pkgs {
64+
feedback.Printf("Package: %s, From: %s, To: %s", pkg.Name, pkg.FromVersion, pkg.ToVersion)
65+
}
66+
67+
feedback.Printf("Do you want to upgrade these packages? (yes/no)")
68+
var yes bool
69+
if forceYes {
70+
yes = true
71+
} else {
72+
var yesInput string
73+
_, err := fmt.Scanf("%s\n", &yesInput)
74+
if err != nil {
75+
return err
76+
}
77+
yes = strings.ToLower(yesInput) == "yes" || strings.ToLower(yesInput) == "y"
78+
}
79+
80+
if !yes {
81+
return nil
82+
}
83+
84+
if err := updater.UpgradePackages(cmd.Context(), pkgs); err != nil {
85+
return err
86+
}
87+
88+
events := updater.Subscribe()
89+
for event := range events {
90+
feedback.Printf("[%s] %s", event.Type.String(), event.Data)
91+
92+
if event.Type == update.DoneEvent {
93+
break
94+
}
95+
}
96+
return nil
97+
},
98+
}
99+
100+
cmd.PersistentFlags().BoolVar(&onlyArduino, "only-arduino", false, "Check for all upgradable packages")
101+
cmd.PersistentFlags().BoolVar(&forceYes, "--yes", false, "Automatically confirm all prompts")
102+
103+
return cmd
104+
}
105+
106+
func getUpdater() *update.Manager {
107+
return update.NewManager(
108+
apt.New(),
109+
arduino.NewArduinoPlatformUpdater(),
110+
)
111+
}
112+
113+
func getFilterFunc(onlyArduino bool) func(p update.UpgradablePackage) bool {
114+
if onlyArduino {
115+
return func(p update.UpgradablePackage) bool {
116+
return strings.HasPrefix(p.Name, "arduino-")
117+
}
118+
}
119+
return func(p update.UpgradablePackage) bool {
120+
return true
121+
}
122+
}

0 commit comments

Comments
 (0)