Skip to content

cli code refactoring: argument parsing/handling should not access the arduino package directly #1948

@cmaglie

Description

@cmaglie

Describe the problem

This is an architectural issue in the golang implementation of the CLI.
The cli package should not use directly the arduino package but it should use only the functions available through gRPC, implemented in the commands package to avoid code duplication and re-implementation of the same functionality over and over.

To reproduce

The current instances of the above issue are here:

internal/cli/arguments/completion.go access the package arduino/cores:

internal/cli/arguments/fqbn.go access the packages arduino/cores and arduino/sketch

internal/cli/arguments/port.go access arduino/discovery and arduino/sketch

  • imports:
    "github.com/arduino/arduino-cli/arduino"
    "github.com/arduino/arduino-cli/arduino/discovery"
    "github.com/arduino/arduino-cli/arduino/sketch"
  • use of sketch:
    func (p *Port) GetPortAddressAndProtocol(instance *rpc.Instance, sk *sketch.Sketch) (string, string, error) {
  • use of sketch:
    func (p *Port) GetPort(instance *rpc.Instance, sk *sketch.Sketch) (*discovery.Port, error) {
  • it completely duplicates a discovery-manager run loop to discover the ports:
    // FIXME: We must not access PackageManager directly here but use one of the commands.* functions
    pme, release := commands.GetPackageManagerExplorer(&rpc.BoardListAllRequest{Instance: instance})
    if pme == nil {
    return nil, &arduino.InvalidInstanceError{}
    }
    defer release()
    dm := pme.DiscoveryManager()
    watcher, err := dm.Watch()
    if err != nil {
    return nil, err
    }
    defer watcher.Close()
    deadline := time.After(p.timeout.Get())
    for {
    select {
    case portEvent := <-watcher.Feed():
    if portEvent.Type != "add" {
    continue
    }
    port := portEvent.Port
    if (protocol == "" || protocol == port.Protocol) && address == port.Address {
    return port, nil
    }
    case <-deadline:
    // No matching port found
    if protocol == "" {
    return &discovery.Port{
    Address: address,
    Protocol: "serial",
    }, nil
    }
    return nil, fmt.Errorf(tr("port not found: %[1]s %[2]s"), address, protocol)
    }
    }
    }

cli/arguments/reference.go uses arduino to get an error object:

cli/arguments/sketch.go uses arduino/sketch:

Expected behavior

Remove all direct access to the arduino package using the gRPC commads implementation in commands.

Arduino CLI version

nightly build

Operating system

N/A

Operating system version

Additional context

No response

Issue checklist

  • I searched for previous reports in the issue tracker
  • I verified the problem still occurs when using the nightly build
  • My report contains all necessary details

Metadata

Metadata

Assignees

Labels

criticality: lowOf low impactpriority: highResolution is a high prioritytopic: codeRelated to content of the project itselftype: imperfectionPerceived defect in any part of project

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions