/
file.go
35 lines (27 loc) · 823 Bytes
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package utils
import (
"bytes"
"encoding/csv"
"github.com/pkg/errors"
"github.com/slack-go/slack"
)
var ErrInvalidCSV = errors.New("received invalid/empty CSV file")
// DownloadAndReadCSV downloads a CSV file from urlPrivateDownload and returns
// the CSV rows. Requires the files:read scope on the user client and the
// calling user must have access to the file.
func DownloadAndReadCSV(userClient *slack.Client, urlPrivateDownload string) ([][]string, error) {
b := bytes.Buffer{}
err := userClient.GetFile(urlPrivateDownload, &b)
if err != nil {
return nil, errors.Wrapf(err, "failed to download file")
}
r := csv.NewReader(&b)
rows, err := r.ReadAll()
if err != nil {
return nil, errors.Wrapf(err, "failed to read CSV")
}
if len(rows) == 0 {
return nil, ErrInvalidCSV
}
return rows, nil
}