-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
81 lines (65 loc) · 1.84 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
77
78
79
80
81
package main
import (
"fmt"
"os"
_ "github.com/joho/godotenv/autoload"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/hash-gaming/statusinator/auth"
"github.com/hash-gaming/statusinator/env"
"github.com/hash-gaming/statusinator/util"
)
func handleS3Error(err error) {
s3Handler := func(arr awserr.Error) string {
switch arr.Code() {
case s3.ErrCodeNoSuchBucket:
return s3.ErrCodeNoSuchBucket + arr.Error()
default:
return arr.Error()
}
}
fmt.Println(util.HandleAWSError(err, s3Handler))
}
func main() {
env.Check()
var creds *credentials.Credentials
roleArn := env.Get(env.ServiceRoleArn)
if env.IsDevelopment() {
assumedRole, assumeRoleErr := auth.GetDevAuth(roleArn)
if assumeRoleErr != nil {
fmt.Println(assumeRoleErr)
}
creds = credentials.NewStaticCredentials(
*assumedRole.Credentials.AccessKeyId,
*assumedRole.Credentials.SecretAccessKey,
*assumedRole.Credentials.SessionToken,
)
} else if env.IsProduction() {
awsAccessKeyID, awsSecretAccessKey := env.Get(env.AWSAccessKeyID), env.Get(env.AWSSecretAccessKey)
creds = credentials.NewStaticCredentials(
awsAccessKeyID,
awsSecretAccessKey,
"",
)
} else {
fmt.Println("No other runtime environment currently configured besides development.")
os.Exit(1)
return
}
cloudAccountSesh := util.GetAWSSession(session.Options{
Config: *aws.NewConfig().WithCredentials(creds),
})
s3Client := util.GetS3Client(cloudAccountSesh, env.Get(env.Region))
input := &s3.ListObjectsV2Input{
Bucket: aws.String(env.Get(env.BucketName)),
MaxKeys: aws.Int64(2),
}
result, err := s3Client.ListObjectsV2(input)
if err != nil {
handleS3Error(err)
}
fmt.Println(util.PrettyPrint(result))
}