Skip to content

Commit 7a9c988

Browse files
committed
Update CLI commands to use new instance logic
1 parent ed70ec6 commit 7a9c988

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+270
-194
lines changed

cli/arguments/completion.go

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ import (
1616
// It returns a list of fqbn
1717
// it's taken from cli/board/listall.go
1818
func GetInstalledBoards() []string {
19-
inst := instance.CreateAndInit()
19+
instance.Init()
20+
inst := instance.Get()
2021

2122
list, _ := board.ListAll(context.Background(), &rpc.BoardListAllRequest{
22-
Instance: inst,
23+
Instance: inst.ToRPC(),
2324
SearchArgs: nil,
2425
IncludeHiddenBoards: false,
2526
})
@@ -34,8 +35,9 @@ func GetInstalledBoards() []string {
3435
// GetInstalledProtocols is an helper function useful to autocomplete.
3536
// It returns a list of protocols available based on the installed boards
3637
func GetInstalledProtocols() []string {
37-
inst := instance.CreateAndInit()
38-
pm := commands.GetPackageManager(inst.Id)
38+
instance.Init()
39+
inst := instance.Get()
40+
pm := commands.GetPackageManager(inst.ID())
3941
boards := pm.InstalledBoards()
4042

4143
installedProtocols := make(map[string]struct{})
@@ -62,12 +64,13 @@ func GetInstalledProtocols() []string {
6264
// GetInstalledProgrammers is an helper function useful to autocomplete.
6365
// It returns a list of programmers available based on the installed boards
6466
func GetInstalledProgrammers() []string {
65-
inst := instance.CreateAndInit()
66-
pm := commands.GetPackageManager(inst.Id)
67+
instance.Init()
68+
inst := instance.Get()
69+
pm := commands.GetPackageManager(inst.ID())
6770

6871
// we need the list of the available fqbn in order to get the list of the programmers
6972
list, _ := board.ListAll(context.Background(), &rpc.BoardListAllRequest{
70-
Instance: inst,
73+
Instance: inst.ToRPC(),
7174
SearchArgs: nil,
7275
IncludeHiddenBoards: false,
7376
})
@@ -93,10 +96,11 @@ func GetInstalledProgrammers() []string {
9396
// GetUninstallableCores is an helper function useful to autocomplete.
9497
// It returns a list of cores which can be uninstalled
9598
func GetUninstallableCores() []string {
96-
inst := instance.CreateAndInit()
99+
instance.Init()
100+
inst := instance.Get()
97101

98102
platforms, _ := core.GetPlatforms(&rpc.PlatformListRequest{
99-
Instance: inst,
103+
Instance: inst.ToRPC(),
100104
UpdatableOnly: false,
101105
All: false,
102106
})
@@ -111,10 +115,11 @@ func GetUninstallableCores() []string {
111115
// GetInstallableCores is an helper function useful to autocomplete.
112116
// It returns a list of cores which can be installed/downloaded
113117
func GetInstallableCores() []string {
114-
inst := instance.CreateAndInit()
118+
instance.Init()
119+
inst := instance.Get()
115120

116121
platforms, _ := core.PlatformSearch(&rpc.PlatformSearchRequest{
117-
Instance: inst,
122+
Instance: inst.ToRPC(),
118123
SearchArgs: "",
119124
AllVersions: false,
120125
})
@@ -139,9 +144,11 @@ func GetUninstallableLibraries() []string {
139144
}
140145

141146
func getLibraries(all bool) []string {
142-
inst := instance.CreateAndInit()
147+
instance.Init()
148+
inst := instance.Get()
149+
143150
libs, _ := lib.LibraryList(context.Background(), &rpc.LibraryListRequest{
144-
Instance: inst,
151+
Instance: inst.ToRPC(),
145152
All: all,
146153
Updatable: false,
147154
Name: "",
@@ -158,10 +165,11 @@ func getLibraries(all bool) []string {
158165
// GetInstallableLibs is an helper function useful to autocomplete.
159166
// It returns a list of libs which can be installed/downloaded
160167
func GetInstallableLibs() []string {
161-
inst := instance.CreateAndInit()
168+
instance.Init()
169+
inst := instance.Get()
162170

163171
libs, _ := lib.LibrarySearch(context.Background(), &rpc.LibrarySearchRequest{
164-
Instance: inst,
172+
Instance: inst.ToRPC(),
165173
Query: "", // if no query is specified all the libs are returned
166174
})
167175
var res []string
@@ -176,10 +184,11 @@ func GetInstallableLibs() []string {
176184
// It returns a list of boards which are currently connected
177185
// Obviously it does not suggests network ports because of the timeout
178186
func GetConnectedBoards() []string {
179-
inst := instance.CreateAndInit()
187+
instance.Init()
188+
inst := instance.Get()
180189

181190
list, _ := board.List(&rpc.BoardListRequest{
182-
Instance: inst,
191+
Instance: inst.ToRPC(),
183192
})
184193
var res []string
185194
// transform the data structure for the completion

cli/arguments/reference.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func ParseReference(arg string) (*Reference, error) {
9595
// try to use core.GetPlatforms to optimize what the user typed
9696
// (by replacing the PackageName and Architecture in ret with the content of core.GetPlatform())
9797
platforms, _ := core.GetPlatforms(&rpc.PlatformListRequest{
98-
Instance: instance.CreateAndInit(),
98+
Instance: instance.Get().ToRPC(),
9999
UpdatableOnly: false,
100100
All: true, // this is true because we want also the installable platforms
101101
})

cli/board/attach.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ func initAttachCommand() *cobra.Command {
5353
}
5454

5555
func runAttachCommand(cmd *cobra.Command, args []string) {
56-
instance := instance.CreateAndInit()
56+
instance.Init()
57+
instance := instance.Get()
5758

5859
logrus.Info("Executing `arduino-cli board attach`")
5960

@@ -66,14 +67,14 @@ func runAttachCommand(cmd *cobra.Command, args []string) {
6667
// ugly hack to allow user to specify fqbn and port as flags (consistency)
6768
// a more meaningful fix would be to fix board.Attach
6869
var boardURI string
69-
discoveryPort, _ := port.GetPort(instance, nil)
70+
discoveryPort, _ := port.GetPort(instance.ToRPC(), nil)
7071
if fqbn.String() != "" {
7172
boardURI = fqbn.String()
7273
} else if discoveryPort != nil {
7374
boardURI = discoveryPort.Address
7475
}
7576
if _, err := board.Attach(context.Background(), &rpc.BoardAttachRequest{
76-
Instance: instance,
77+
Instance: instance.ToRPC(),
7778
BoardUri: boardURI,
7879
SketchPath: sketchPath.String(),
7980
SearchTimeout: port.GetSearchTimeout().String(),

cli/board/details.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,13 @@ func initDetailsCommand() *cobra.Command {
5757
}
5858

5959
func runDetailsCommand(cmd *cobra.Command, args []string) {
60-
inst := instance.CreateAndInit()
60+
instance.Init()
61+
inst := instance.Get()
6162

6263
logrus.Info("Executing `arduino-cli board details`")
6364

6465
res, err := board.Details(context.Background(), &rpc.BoardDetailsRequest{
65-
Instance: inst,
66+
Instance: inst.ToRPC(),
6667
Fqbn: fqbn.String(),
6768
})
6869

cli/board/list.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@ func initListCommand() *cobra.Command {
5555

5656
// runListCommand detects and lists the connected arduino boards
5757
func runListCommand(cmd *cobra.Command, args []string) {
58-
inst := instance.CreateAndInit()
59-
6058
logrus.Info("Executing `arduino-cli board list`")
6159

60+
instance.Init()
61+
inst := instance.Get()
6262
if watch {
63-
watchList(cmd, inst)
63+
watchList(cmd, inst.ToRPC())
6464
os.Exit(0)
6565
}
6666

6767
ports, err := board.List(&rpc.BoardListRequest{
68-
Instance: inst,
68+
Instance: inst.ToRPC(),
6969
Timeout: timeout.Milliseconds(),
7070
})
7171
if err != nil {

cli/board/listall.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@ for a specific board if you specify the board name`),
5151

5252
// runListAllCommand list all installed boards
5353
func runListAllCommand(cmd *cobra.Command, args []string) {
54-
inst := instance.CreateAndInit()
54+
instance.Init()
55+
inst := instance.Get()
5556

5657
logrus.Info("Executing `arduino-cli board listall`")
5758

5859
list, err := board.ListAll(context.Background(), &rpc.BoardListAllRequest{
59-
Instance: inst,
60+
Instance: inst.ToRPC(),
6061
SearchArgs: args,
6162
IncludeHiddenBoards: showHiddenBoard,
6263
})

cli/board/search.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,13 @@ for a specific board if you specify the board name`),
4949
}
5050

5151
func runSearchCommand(cmd *cobra.Command, args []string) {
52-
inst := instance.CreateAndInit()
52+
instance.Init()
53+
inst := instance.Get()
5354

5455
logrus.Info("Executing `arduino-cli board search`")
5556

5657
res, err := board.Search(context.Background(), &rpc.BoardSearchRequest{
57-
Instance: inst,
58+
Instance: inst.ToRPC(),
5859
SearchArgs: strings.Join(args, " "),
5960
IncludeHiddenBoards: showHiddenBoard,
6061
})

cli/burnbootloader/burnbootloader.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,20 @@ func NewCommand() *cobra.Command {
6363
}
6464

6565
func runBootloaderCommand(command *cobra.Command, args []string) {
66-
instance := instance.CreateAndInit()
66+
instance.Init()
67+
instance := instance.Get()
6768

6869
logrus.Info("Executing `arduino-cli burn-bootloader`")
6970

7071
// We don't need a Sketch to upload a board's bootloader
71-
discoveryPort, err := port.GetPort(instance, nil)
72+
discoveryPort, err := port.GetPort(instance.ToRPC(), nil)
7273
if err != nil {
7374
feedback.Errorf(tr("Error during Upload: %v"), err)
7475
os.Exit(errorcodes.ErrGeneric)
7576
}
7677

7778
if _, err := upload.BurnBootloader(context.Background(), &rpc.BurnBootloaderRequest{
78-
Instance: instance,
79+
Instance: instance.ToRPC(),
7980
Fqbn: fqbn.String(),
8081
Port: discoveryPort.ToRPC(),
8182
Verbose: verbose,

cli/cache/clean.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020

2121
"github.com/arduino/arduino-cli/cli/errorcodes"
2222
"github.com/arduino/arduino-cli/cli/feedback"
23-
"github.com/arduino/arduino-cli/configuration"
23+
"github.com/arduino/arduino-cli/cli/instance"
2424
"github.com/sirupsen/logrus"
2525
"github.com/spf13/cobra"
2626
)
@@ -40,7 +40,8 @@ func initCleanCommand() *cobra.Command {
4040
func runCleanCommand(cmd *cobra.Command, args []string) {
4141
logrus.Info("Executing `arduino-cli cache clean`")
4242

43-
cachePath := configuration.Settings.GetString("directories.Downloads")
43+
instance.Init()
44+
cachePath := instance.Get().Settings.GetString("directories.Downloads")
4445
err := os.RemoveAll(cachePath)
4546
if err != nil {
4647
feedback.Errorf(tr("Error cleaning caches: %v"), err)

cli/cli.go

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/arduino/arduino-cli/cli/feedback"
3434
"github.com/arduino/arduino-cli/cli/generatedocs"
3535
"github.com/arduino/arduino-cli/cli/globals"
36+
"github.com/arduino/arduino-cli/cli/instance"
3637
"github.com/arduino/arduino-cli/cli/lib"
3738
"github.com/arduino/arduino-cli/cli/monitor"
3839
"github.com/arduino/arduino-cli/cli/outdated"
@@ -43,6 +44,7 @@ import (
4344
"github.com/arduino/arduino-cli/cli/upload"
4445
"github.com/arduino/arduino-cli/cli/version"
4546
"github.com/arduino/arduino-cli/configuration"
47+
"github.com/arduino/arduino-cli/httpclient"
4648
"github.com/arduino/arduino-cli/i18n"
4749
"github.com/arduino/arduino-cli/inventory"
4850
"github.com/arduino/arduino-cli/logging"
@@ -119,31 +121,60 @@ func createCliCommandTree(cmd *cobra.Command) {
119121
})
120122
cmd.PersistentFlags().StringVar(&configFile, "config-file", "", tr("The custom config file (if not specified the default will be used)."))
121123
cmd.PersistentFlags().StringSlice("additional-urls", []string{}, tr("Comma-separated list of additional URLs for the Boards Manager."))
122-
configuration.BindFlags(cmd, configuration.Settings)
123124
}
124125

125126
func preRun(cmd *cobra.Command, args []string) {
126127
if cmd.Name() == "daemon" {
127128
return
128129
}
129130

130-
configFile := configuration.Settings.ConfigFileUsed()
131+
configFile := configuration.FindConfigFileInArgsOrWorkingDirectory(os.Args)
132+
instance.Create(configFile)
133+
inst := instance.Get()
134+
configuration.BindFlags(cmd, inst.Settings)
131135

132-
// initialize inventory
133-
err := inventory.Init(configuration.Settings.GetString("directories.Data"))
134-
if err != nil {
135-
feedback.Errorf("Error: %v", err)
136-
os.Exit(errorcodes.ErrBadArgument)
137-
}
136+
// From this point on we can use logging
137+
138+
i18n.Init(inst.Settings.GetString("locale"))
138139

139140
outputFormat, err := cmd.Flags().GetString("format")
140141
if err != nil {
141142
feedback.Errorf(tr("Error getting flag value: %s", err))
142143
os.Exit(errorcodes.ErrBadCall)
143144
}
144-
noColor := configuration.Settings.GetBool("output.no_color") || os.Getenv("NO_COLOR") != ""
145+
146+
noColor := inst.Settings.GetBool("output.no_color") || os.Getenv("NO_COLOR") != ""
145147
output.Setup(outputFormat, noColor)
146148

149+
// Setups logging if necessary
150+
verbose, err := cmd.Flags().GetBool("verbose")
151+
if err != nil {
152+
feedback.Errorf(tr("Error getting flag value: %s", err))
153+
os.Exit(errorcodes.ErrBadCall)
154+
}
155+
logging.Setup(
156+
verbose,
157+
noColor,
158+
inst.Settings.GetString("logging.level"),
159+
inst.Settings.GetString("logging.file"),
160+
inst.Settings.GetString("logging.format"),
161+
)
162+
163+
httpConfig, err := httpclient.ConfigFromSettings(inst.Settings)
164+
if err != nil {
165+
feedback.Errorf("Error configuring http client: %v", err)
166+
os.Exit(errorcodes.ErrCoreConfig)
167+
}
168+
// Initializes http client used by this command line process
169+
httpclient.Init(httpConfig)
170+
171+
// initialize inventory
172+
err = inventory.Init(inst.Settings.GetString("directories.Data"))
173+
if err != nil {
174+
feedback.Errorf("Error: %v", err)
175+
os.Exit(errorcodes.ErrBadArgument)
176+
}
177+
147178
updaterMessageChan = make(chan *semver.Version)
148179
go func() {
149180
if cmd.Name() == "version" {
@@ -158,20 +189,6 @@ func preRun(cmd *cobra.Command, args []string) {
158189
updaterMessageChan <- updater.CheckForUpdate(currentVersion)
159190
}()
160191

161-
// Setups logging if necessary
162-
verbose, err := cmd.Flags().GetBool("verbose")
163-
if err != nil {
164-
feedback.Errorf(tr("Error getting flag value: %s", err))
165-
os.Exit(errorcodes.ErrBadCall)
166-
}
167-
logging.Setup(
168-
verbose,
169-
noColor,
170-
configuration.Settings.GetString("logging.level"),
171-
configuration.Settings.GetString("logging.file"),
172-
configuration.Settings.GetString("logging.format"),
173-
)
174-
175192
//
176193
// Print some status info and check command is consistent
177194
//

0 commit comments

Comments
 (0)