/
main.go
65 lines (52 loc) · 1.53 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
package main
import (
"flag"
"fmt"
"os"
"github.com/razzkumar/PR-Automation/aws-client"
"github.com/razzkumar/PR-Automation/cloudfront"
"github.com/razzkumar/PR-Automation/logger"
"github.com/razzkumar/PR-Automation/s3"
"github.com/razzkumar/PR-Automation/utils"
)
func main() {
// Setting env variable
awsRegion := os.Getenv("AWS_REGION")
if awsRegion == "" {
err := os.Setenv("AWS_REGION", "us-east-2")
if err != nil {
logger.FailOnError(err, "Fail to set AWS_REGION")
}
}
var action string
// Getting action wether delete or create
var repo utils.ProjectInfo
flag.StringVar(&action, "action", "", "It's create or delete s3 bucket")
flag.Parse()
if action == "" {
logger.FailOnNoFlag("Please provide action what to do [deploy,delete,create]")
}
if os.Getenv("GITHUB_EVENT_NAME") == "pull_request" && (action == "create" || action == "delete") {
repo = utils.GetPRInfo(repo)
} else {
repo = utils.GetInfo(repo, action)
}
// Getting session of aws
sess := awsclient.GetSession()
switch action {
case "deploy":
err := s3.Deploy(repo, sess)
invErr := cloudfront.Invalidation(repo.CloudfrontId, sess)
logger.FailOnError(invErr, "Fail to invalitate")
logger.FailOnError(err, "Error on Deployment")
case "create":
err := s3.DeployAndComment(repo, sess)
logger.FailOnError(err, "Error on Deployment and commit")
case "delete":
err := s3.Delete(repo.Bucket, sess)
logger.FailOnError(err, "Error while Delete")
default:
err := fmt.Errorf("Nothing to do")
logger.FailOnError(err, "Default case")
}
}