This repository has been archived by the owner on Aug 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
76 lines (67 loc) · 1.78 KB
/
main.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"bytes"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go/aws/defaults"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"io/ioutil"
"log"
"os"
"strings"
)
type Rowable interface {
Row() []string
}
func main() {
config := NewConfig()
sess := GetSession()
journaler, err := NewJournaler()
HandleError(err)
defer journaler.Close()
journaler.Write(Headers)
filename, err := NewFetcher(config, sess, journaler).Run()
HandleError(err)
if strings.HasPrefix(config.OutputFile, "s3://") {
buffer, err := ioutil.ReadFile(filename)
HandleError(err)
path := strings.Split(config.OutputFile, "/")
uploader := s3manager.NewUploader(sess)
// TODO: Better way to upload file from disk as opposed to memory
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(path[2]),
Key: aws.String(strings.Join(path[3:], "/")),
Body: bytes.NewBuffer(buffer),
})
HandleError(err)
} else {
// Since we've been journalling in CSV format we can just Rename
// the journal to the output filename
os.Rename(filename, config.OutputFile)
}
}
func GetSession() *session.Session {
var creds *credentials.Credentials
sess := session.Must(session.NewSession())
meta := ec2metadata.New(sess)
if meta.Available() {
creds = credentials.NewCredentials(&ec2rolecreds.EC2RoleProvider{
Client: meta,
})
} else {
creds = credentials.NewEnvCredentials()
}
if _, err := creds.Get(); err != nil {
log.Fatal(err)
}
sess.Config = defaults.Config().WithCredentials(creds)
return sess
}
func HandleError(err error) {
if err != nil {
log.Fatal(err)
}
}