forked from jadudm/cgov-util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
98 lines (90 loc) · 2.61 KB
/
util.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
package cmd
import (
"net/url"
"os"
"path"
"gov.gsa.fac.cgov-util/internal/environments"
"gov.gsa.fac.cgov-util/internal/logging"
"gov.gsa.fac.cgov-util/internal/structs"
"gov.gsa.fac.cgov-util/internal/vcap"
)
func parseS3Path(s3_path string) *structs.S3Path {
u, err := url.Parse(s3_path)
if err != nil {
logging.Logger.Printf("parseS3Path could not parse s3 path: %s", s3_path)
os.Exit(logging.S3_PATH_PARSE_ERROR)
}
if u.Scheme != "s3" {
logging.Logger.Printf("parseS3Path does not look like an S3 path (e.g. `s3://`): %s", s3_path)
os.Exit(logging.S3_PATH_PARSE_ERROR)
}
logging.Logger.Println("Host: ", u.Host)
logging.Logger.Println("Path: ", u.Path)
return &structs.S3Path{
//Bucket: filepath.Clean(u.Host),
//Key: filepath.Clean(u.Path),
Bucket: path.Clean(u.Host),
Key: path.Clean(u.Path),
}
}
func runLocalOrRemote(funs structs.Choice) {
switch os.Getenv("ENV") {
case environments.LOCAL:
fallthrough
case environments.TESTING:
funs.Local()
case environments.DEVELOPMENT:
fallthrough
case environments.PREVIEW:
fallthrough
case environments.STAGING:
fallthrough
case environments.PRODUCTION:
funs.Remote()
default:
logging.Logger.Printf("LOCALORREMOTE impossible condition")
os.Exit(-1)
}
}
func getDBCredentials(db_name string) vcap.Credentials {
// Check that we can get credentials.
db_creds, err := vcap.VCS.GetCredentials("aws-rds", db_name)
if err != nil {
logging.Logger.Printf("GETDBCREDENTIALS could not get DB credentials for %s", db_name)
os.Exit(logging.COULD_NOT_FIND_CREDENTIALS)
}
return db_creds
}
func getBucketCredentials(s3path *structs.S3Path) vcap.Credentials {
switch os.Getenv("ENV") {
case environments.LOCAL:
fallthrough
case environments.TESTING:
bucket_creds, err := vcap.VCS.GetCredentials("user-provided", s3path.Bucket)
if err != nil {
logging.Logger.Printf("GetCredentials could not get minio credentials: %s", s3path)
os.Exit(logging.COULD_NOT_FIND_CREDENTIALS)
}
return bucket_creds
case environments.DEVELOPMENT:
fallthrough
case environments.PREVIEW:
fallthrough
case environments.STAGING:
fallthrough
case environments.PRODUCTION:
bucket_creds, err := vcap.VCS.GetCredentials("s3", s3path.Bucket)
if err != nil {
logging.Logger.Printf("DBTOS3 could not get s3 credentials")
os.Exit(logging.COULD_NOT_FIND_CREDENTIALS)
}
return bucket_creds
default:
logging.Logger.Printf("DBTOS3 could not get env for bucket credentials")
os.Exit(logging.COULD_NOT_FIND_CREDENTIALS)
}
// We should never get here.
logging.Logger.Printf("DBTOS3 impossible condition")
os.Exit(-1)
return vcap.Credentials{}
}