-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathbudgetsUploader.go
More file actions
49 lines (37 loc) · 1.04 KB
/
budgetsUploader.go
File metadata and controls
49 lines (37 loc) · 1.04 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
This file is responsible for handling uploading of parsed budget data to MongoDB.
*/
package uploader
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/UTDNebula/nebula-api/api/schema"
"github.com/joho/godotenv"
)
// Note that this uploader assumes that the collection names match the names of these files, which they should.
// If the names of these collections ever change, the file names should be updated accordingly.
var budgetsFile = "budgets.json"
func UploadBudgets(inDir string) {
//Load env vars
if err := godotenv.Load(); err != nil {
log.Panic("Error loading .env file")
}
//Connect to mongo
client := connectDB()
// Get 5 minute context
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
// Open data file for reading
fptr, err := os.Open(fmt.Sprintf("%s/"+budgetsFile, inDir))
if err != nil {
if os.IsNotExist(err) {
log.Panicf("File not found. Skipping %s", budgetsFile)
}
log.Panic(err)
}
defer fptr.Close()
UploadData[schema.Budget](client, ctx, fptr, false)
}