rewrite getCredentials to be more generic
This commit is contained in:
37
main.go
37
main.go
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"backup-tool/s3helper"
|
"backup-tool/s3helper"
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
@@ -11,24 +12,32 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
accessKeyID, secretAccessKey := getCredentials()
|
|
||||||
s3helper.UploadFile(context.Background(), accessKeyID, secretAccessKey)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func getCredentials() (string, string) {
|
|
||||||
// get credentials from env vars
|
|
||||||
err := godotenv.Load()
|
err := godotenv.Load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error loading .env file: (%s)", err.Error())
|
log.Fatalf("error loading .env file: (%s)", err.Error())
|
||||||
}
|
}
|
||||||
keyName := os.Getenv("KEY_ID")
|
|
||||||
if keyName == "" {
|
if len(os.Args) > 1 {
|
||||||
log.Fatal("missing or empty KEY_ID")
|
// only do this if we have been given a file to upload (for now)
|
||||||
|
// TODO: have this tool take the backup of the DB itself
|
||||||
|
b2Creds, err := getCredentials([]string{"B2_KEY_ID", "B2_APPLICATION_KEY"})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("could not get B2 credentials: %s", err)
|
||||||
}
|
}
|
||||||
applicationKey := os.Getenv("APPLICATION_KEY")
|
s3helper.UploadFile(context.Background(), b2Creds)
|
||||||
if applicationKey == "" {
|
|
||||||
log.Fatal("missing or empty APPLICATION_KEY")
|
|
||||||
}
|
}
|
||||||
return keyName, applicationKey
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func getCredentials(credNames []string) (map[string]string, error) {
|
||||||
|
creds := map[string]string{}
|
||||||
|
for _, name := range credNames {
|
||||||
|
value := os.Getenv(name)
|
||||||
|
if value == "" {
|
||||||
|
return nil, fmt.Errorf("missing or empty ENV var: %s", name)
|
||||||
|
}
|
||||||
|
creds[name] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
return creds, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,9 +14,9 @@ const (
|
|||||||
BUCKETNAME = "ducimon-db-backups"
|
BUCKETNAME = "ducimon-db-backups"
|
||||||
)
|
)
|
||||||
|
|
||||||
func UploadFile(ctx context.Context, accessKeyID, secretAccessKey) error {
|
func UploadFile(ctx context.Context, b2Creds map[string]string) error {
|
||||||
|
|
||||||
client := createClient(accessKeyID, secretAccessKey)
|
client := createClient(b2Creds["B2_KEY_ID"], b2Creds["B2_APPLICATION_KEY"])
|
||||||
verifyBucket(client, ctx)
|
verifyBucket(client, ctx)
|
||||||
absolutePath, basename := validateUploadFile()
|
absolutePath, basename := validateUploadFile()
|
||||||
_, err := client.FPutObject(ctx, BUCKETNAME, "test", absolutePath, minio.PutObjectOptions{})
|
_, err := client.FPutObject(ctx, BUCKETNAME, "test", absolutePath, minio.PutObjectOptions{})
|
||||||
|
|||||||
Reference in New Issue
Block a user