From dcd20c43cf859b951dd8a72a1ec26846cb32de70 Mon Sep 17 00:00:00 2001 From: Alessandro Sanino Date: Thu, 27 Jul 2017 15:48:34 +0200 Subject: [PATCH] refactored parsing of args (download, install libs) --- cmd/arduino_lib.go | 46 ++++++----------------------------- common/releases/helpers.go | 4 +-- libraries/status.go | 50 ++++++++++++++++++++++++++++++++------ 3 files changed, 53 insertions(+), 47 deletions(-) diff --git a/cmd/arduino_lib.go b/cmd/arduino_lib.go index 2b82a01eb6a..29125ae9983 100644 --- a/cmd/arduino_lib.go +++ b/cmd/arduino_lib.go @@ -156,47 +156,17 @@ func executeDownloadCommand(cmd *cobra.Command, args []string) error { } } - libs, failed := purgeInvalidItems(releases.ParseArgs(args), status) + pairs := releases.ParseArgs(args) + libsToDownload, failOutputs := status.ProcessPairs(pairs) outputResults := output.LibProcessResults{ - Libraries: failed, + Libraries: failOutputs, } - releases.ParallelDownloads(libs, true, "Downloaded", GlobalFlags.Verbose, &outputResults.Libraries) + releases.ParallelDownload(libsToDownload, true, "Downloaded", GlobalFlags.Verbose, &outputResults.Libraries) formatter.Print(outputResults) return nil } -func purgeInvalidItems(items []releases.NameVersionPair, status libraries.StatusContext) ([]releases.DownloadItem, []output.ProcessResult) { - itemC := len(items) - ret := make([]releases.DownloadItem, 0, itemC) - fails := make([]output.ProcessResult, 0, itemC) - - for _, item := range items { - library, exists := status.Libraries[item.Name] - if !exists { - fails = append(fails, output.ProcessResult{ - ItemName: item.Name, - Error: "Library Not Found", - }) - } else { - release := library.GetVersion(item.Version) - if release == nil { - fails = append(fails, output.ProcessResult{ - ItemName: item.Name, - Error: "Version Not Found", - }) - } else { // replaces "latest" with latest version too - ret = append(ret, releases.DownloadItem{ - Name: library.Name, - Release: release, - }) - } - } - } - - return ret, fails -} - func executeInstallCommand(cmd *cobra.Command, args []string) error { if len(args) < 1 { return fmt.Errorf("No library specified for install command") @@ -220,14 +190,14 @@ func executeInstallCommand(cmd *cobra.Command, args []string) error { } } - parsedArgs := releases.ParseArgs(args) - libs, failOutputs := purgeInvalidItems(parsedArgs, status) + pairs := releases.ParseArgs(args) + libsToDownload, failOutputs := status.ProcessPairs(pairs) outputResults := output.LibProcessResults{ Libraries: failOutputs, } - releases.ParallelDownloads(libs, false, "Installed", GlobalFlags.Verbose, &outputResults.Libraries) + releases.ParallelDownload(libsToDownload, false, "Installed", GlobalFlags.Verbose, &outputResults.Libraries) - for i, item := range libs { + for i, item := range libsToDownload { err = libraries.InstallLib(item.Name, item.Release) if err != nil { outputResults.Libraries[i] = output.ProcessResult{ diff --git a/common/releases/helpers.go b/common/releases/helpers.go index 0b56cbb0425..a48363344e3 100644 --- a/common/releases/helpers.go +++ b/common/releases/helpers.go @@ -124,12 +124,12 @@ func downloadAndCache(item DownloadItem, progBar *pb.ProgressBar) task.Wrapper { } } -// ParallelDownloads executes multiple releases downloads in parallel and fills properly results. +// ParallelDownload executes multiple releases downloads in parallel and fills properly results. // // forced is used to force download if cached. // OkStatus is used to tell the overlying process result ("Downloaded", "Installed", etc...) // DOES NOT RETURN because modified refResults array of results using pointer provided by refResults.Results(). -func ParallelDownloads(items []DownloadItem, forced bool, OkStatus string, verbosity int, refResults *[]output.ProcessResult) { +func ParallelDownload(items []DownloadItem, forced bool, OkStatus string, verbosity int, refResults *[]output.ProcessResult) { itemC := len(items) tasks := make(map[string]task.Wrapper, itemC) progressBars := make([]*pb.ProgressBar, 0, itemC) diff --git a/libraries/status.go b/libraries/status.go index 25130a76f2b..ff41b2361c3 100644 --- a/libraries/status.go +++ b/libraries/status.go @@ -32,6 +32,8 @@ package libraries import ( "fmt" + "github.com/bcmi-labs/arduino-cli/cmd/output" + "github.com/bcmi-labs/arduino-cli/common/releases" "github.com/pmylund/sortutil" ) @@ -42,22 +44,22 @@ type StatusContext struct { } // AddLibrary adds an indexRelease to the status context -func (l *StatusContext) AddLibrary(indexLib *indexRelease) { +func (sc *StatusContext) AddLibrary(indexLib *indexRelease) { name := indexLib.Name - if l.Libraries[name] == nil { - l.Libraries[name] = indexLib.extractLibrary() + if sc.Libraries[name] == nil { + sc.Libraries[name] = indexLib.extractLibrary() } else { release := indexLib.extractRelease() - lib := l.Libraries[name] + lib := sc.Libraries[name] lib.Releases[fmt.Sprint(release.Version)] = release } } // Names returns an array with all the names of the registered libraries. -func (l StatusContext) Names() []string { - res := make([]string, len(l.Libraries)) +func (sc StatusContext) Names() []string { + res := make([]string, len(sc.Libraries)) i := 0 - for n := range l.Libraries { + for n := range sc.Libraries { res[i] = n i++ } @@ -65,6 +67,40 @@ func (l StatusContext) Names() []string { return res } +// ProcessPairs takes a set of name-version pairs and return +// a set of items to download and a set of outputs for non +// existing libraries. +func (sc StatusContext) ProcessPairs(items []releases.NameVersionPair) ([]releases.DownloadItem, []output.ProcessResult) { + itemC := len(items) + ret := make([]releases.DownloadItem, 0, itemC) + fails := make([]output.ProcessResult, 0, itemC) + + for _, item := range items { + library, exists := sc.Libraries[item.Name] + if !exists { + fails = append(fails, output.ProcessResult{ + ItemName: item.Name, + Error: "Library Not Found", + }) + } else { + release := library.GetVersion(item.Version) + if release == nil { + fails = append(fails, output.ProcessResult{ + ItemName: item.Name, + Error: "Version Not Found", + }) + } else { // replaces "latest" with latest version too + ret = append(ret, releases.DownloadItem{ + Name: library.Name, + Release: release, + }) + } + } + } + + return ret, fails +} + // CreateStatusContext creates a status context from index data. func (index Index) CreateStatusContext() (StatusContext, error) { // Start with an empty status context