Skip to content

Commit

Permalink
refactored parsing of args (download, install libs)
Browse files Browse the repository at this point in the history
  • Loading branch information
saniales committed Jul 27, 2017
1 parent 18815e5 commit dcd20c4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 47 deletions.
46 changes: 8 additions & 38 deletions cmd/arduino_lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions common/releases/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 43 additions & 7 deletions libraries/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -42,29 +44,63 @@ 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++
}
sortutil.CiAsc(res)
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
Expand Down

0 comments on commit dcd20c4

Please sign in to comment.