// downloadAll() orchestrates downloading all known reports concurrently. // It returns an error if there is a problem logging in. func downloadAll() error { downloader, err := download.New(*site, *email, *password) if err != nil { return errors.New("Failed to initialize downloader: " + err.Error()) } var wg sync.WaitGroup // Store download functions in a slice to simplify concurrent downloading. downloadFunctions := []func(*download.Downloader){ downloadSoldItemsReport, downloadStockItemsReport, } // Call each download function concurrently. // A sync.WaitGroup is used to make sure the function does not return // until all downloads are finished. for _, df := range downloadFunctions { wg.Add(1) go func(f func(*download.Downloader)) { defer wg.Done() f(downloader) }(df) } wg.Wait() return nil }
// Download the stock items report to the given directory. // Returns the path to the report as a string. // Error is non-nil if something goes wrong. func downloadStockItemsReport(dir string) (string, error) { // downloader should be passed into the function if more // reports are necessary in the future. downloader, err := download.New(*site, *email, *password) if err != nil { return "", errors.New("Failed to initialize downloader: " + err.Error()) } stockItemsPath := path.Join(dir, "stock_items.csv") err = downloader.GetStockItemsReport(stockItemsPath) if err != nil { return "", errors.New("Failed to get Stock Items report: " + err.Error()) } return stockItemsPath, nil }