This repository has been archived by the owner on May 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
111 lines (91 loc) · 2.57 KB
/
config.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"context"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/silverstripeltd/ssp-sdk-go/ssp"
"github.com/stojg/tape/internal"
)
func newApp(args []string, verbose bool) application {
c := application{}
c.Logger = internal.NewCliLogger(verbose)
var err error
if c.baseDir, err = filepath.Abs(args[0]); err != nil {
handleError(err)
}
c.tarPrefix = filepath.Base(c.baseDir)
if c.dashboard.url, err = url.Parse(args[2]); err != nil {
handleError(err)
}
// grab the stack and env from the dashboard url
pathParts := strings.Split(strings.Trim(c.dashboard.url.Path, "/"), "/")
if len(pathParts) != 5 {
handleError(fmt.Errorf("usage: %s path/to/src/directory s3://bucket/destination/file.tar.gz http://platform/ \n", os.Args[0]))
}
c.stack, c.env = pathParts[2], pathParts[4]
c.dashboard.user = os.Getenv("DASHBOARD_USER")
c.dashboard.token = os.Getenv("DASHBOARD_TOKEN")
s3URL, err := url.Parse(args[1])
if err != nil {
handleError(err)
}
if s3URL.Scheme != "s3" {
handleError(fmt.Errorf("S3Uri argument does not have valid protocol, should be 's3'"))
}
if s3URL.Host == "" {
handleError(fmt.Errorf("S3Uri is missing bucket name"))
}
c.s3.bucket = s3URL.Host
c.s3.key = s3URL.Path
c.s3.region = bucketRegion(c.s3.bucket)
return c
}
func bucketRegion(bucket string) string {
sess := session.Must(session.NewSession(&aws.Config{Region: aws.String("ap-southeast-2")}))
region, err := s3manager.GetBucketRegion(context.Background(), sess, bucket, "ap-southeast-2")
if err != nil {
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == "NotFound" {
handleError(fmt.Errorf("unable to find bucket %s's region", bucket))
}
handleError(err)
}
return region
}
type application struct {
internal.Logger
baseDir string
tarPrefix string
stack, env string
dashboard struct {
url *url.URL
user string
token string
}
s3 struct {
bucket string
key string
region string
}
}
func (conf application) dashboardClient() (*ssp.Client, error) {
return ssp.NewClient(&ssp.Config{
Email: conf.dashboard.user,
Token: conf.dashboard.token,
BaseURL: fmt.Sprintf("%s://%s", conf.dashboard.url.Scheme, conf.dashboard.url.Host),
})
}
func (conf application) S3Client() *s3.S3 {
sess := session.Must(session.NewSession(&aws.Config{
Region: aws.String(conf.s3.region),
}))
svc := s3.New(sess)
return svc
}