|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package lib |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/arduino/arduino-cli/commands/lib" |
| 24 | + rpc "github.com/arduino/arduino-cli/rpc/commands" |
| 25 | +) |
| 26 | + |
| 27 | +// LibraryReferenceArg is a command line argument that reference a library. |
| 28 | +type LibraryReferenceArg struct { |
| 29 | + Name string |
| 30 | + Version string |
| 31 | +} |
| 32 | + |
| 33 | +func (r *LibraryReferenceArg) String() string { |
| 34 | + if r.Version != "" { |
| 35 | + return r.Name + "@" + r.Version |
| 36 | + } |
| 37 | + return r.Name |
| 38 | +} |
| 39 | + |
| 40 | +// ParseLibraryReferenceArg parse a command line argument that reference a |
| 41 | +// library in the form "LibName@Version" or just "LibName". |
| 42 | +func ParseLibraryReferenceArg(arg string) (*LibraryReferenceArg, error) { |
| 43 | + tokens := strings.SplitN(arg, "@", 2) |
| 44 | + |
| 45 | + ret := &LibraryReferenceArg{} |
| 46 | + // TODO: check library Name constraints |
| 47 | + // TODO: check library Version constraints |
| 48 | + if tokens[0] == "" { |
| 49 | + return nil, fmt.Errorf("invalid empty library name") |
| 50 | + } |
| 51 | + ret.Name = tokens[0] |
| 52 | + if len(tokens) > 1 { |
| 53 | + if tokens[1] == "" { |
| 54 | + return nil, fmt.Errorf("invalid empty library version: %s", arg) |
| 55 | + } |
| 56 | + ret.Version = tokens[1] |
| 57 | + } |
| 58 | + return ret, nil |
| 59 | +} |
| 60 | + |
| 61 | +// ParseLibraryReferenceArgs is a convenient wrapper that operates on a slice of strings and |
| 62 | +// calls ParseLibraryReferenceArg for each of them. It returns at the first invalid argument. |
| 63 | +func ParseLibraryReferenceArgs(args []string) ([]*LibraryReferenceArg, error) { |
| 64 | + ret := []*LibraryReferenceArg{} |
| 65 | + for _, arg := range args { |
| 66 | + if reference, err := ParseLibraryReferenceArg(arg); err == nil { |
| 67 | + ret = append(ret, reference) |
| 68 | + } else { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + } |
| 72 | + return ret, nil |
| 73 | +} |
| 74 | + |
| 75 | +// ParseLibraryReferenceArgAndAdjustCase parse a command line argument that reference a |
| 76 | +// library and possibly adjust the case of the name to match a library in the index |
| 77 | +func ParseLibraryReferenceArgAndAdjustCase(instance *rpc.Instance, arg string) (*LibraryReferenceArg, error) { |
| 78 | + libRef, err := ParseLibraryReferenceArg(arg) |
| 79 | + res, err := lib.LibrarySearch(context.Background(), &rpc.LibrarySearchReq{ |
| 80 | + Instance: instance, |
| 81 | + Query: libRef.Name, |
| 82 | + }) |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + |
| 87 | + candidates := []*rpc.SearchedLibrary{} |
| 88 | + for _, foundLib := range res.GetLibraries() { |
| 89 | + if strings.ToLower(foundLib.GetName()) == strings.ToLower(libRef.Name) { |
| 90 | + candidates = append(candidates, foundLib) |
| 91 | + } |
| 92 | + } |
| 93 | + if len(candidates) == 1 { |
| 94 | + libRef.Name = candidates[0].GetName() |
| 95 | + } |
| 96 | + return libRef, nil |
| 97 | +} |
| 98 | + |
| 99 | +// ParseLibraryReferenceArgsAndAdjustCase is a convenient wrapper that operates on a slice of |
| 100 | +// strings and calls ParseLibraryReferenceArgAndAdjustCase for each of them. It returns at the first invalid argument. |
| 101 | +func ParseLibraryReferenceArgsAndAdjustCase(instance *rpc.Instance, args []string) ([]*LibraryReferenceArg, error) { |
| 102 | + ret := []*LibraryReferenceArg{} |
| 103 | + for _, arg := range args { |
| 104 | + if reference, err := ParseLibraryReferenceArgAndAdjustCase(instance, arg); err == nil { |
| 105 | + ret = append(ret, reference) |
| 106 | + } else { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + } |
| 110 | + return ret, nil |
| 111 | +} |
0 commit comments