Skip to content

Commit

Permalink
Finish S3 code
Browse files Browse the repository at this point in the history
  • Loading branch information
drichelson committed May 21, 2024
1 parent 4ad9f7e commit ffa7d37
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
45 changes: 18 additions & 27 deletions cmd/dorkly/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,44 @@ package main

import (
"context"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/dorklyorg/dorkly/internal/dorkly"
"log"
"os"
)

const (
dorklyYamlEnvVar = "DORKLY_YAML"
newRelayArchiveDirEnvVar = "NEW_RELAY_ARCHIVE_DIR"
existingRelayArchiveEnvVar = "EXISTING_RELAY_ARCHIVE"
newRelayArchiveEnvVar = "NEW_RELAY_ARCHIVE"

defaultDorklyYamlInputPath = "project"
defaultNewRelayArchiveDirPath = "newRelayArchive"
defaultExistingRelayArchive = "flags.tar.gz"
defaultNewRelayArchive = "flags-new.tar.gz"
defaultDorklyYamlInputPath = "project"

s3BucketEnvVar = "DORKLY_S3_BUCKET"
)

func main() {
//TODO: use a config library to manage these env vars
ctx := context.Background()
dorklyYamlInputPath := os.Getenv(dorklyYamlEnvVar)
if dorklyYamlInputPath == "" {
log.Printf(dorklyYamlEnvVar+" env var not set. Using default: %s", defaultDorklyYamlInputPath)
log.Printf("Env var [%s] not set. Using default: %s", dorklyYamlEnvVar, defaultDorklyYamlInputPath)
dorklyYamlInputPath = defaultDorklyYamlInputPath
}

newRelayArchiveDirPath := os.Getenv(newRelayArchiveDirEnvVar)
if newRelayArchiveDirPath == "" {
log.Printf(newRelayArchiveDirEnvVar + " env var not set. Using default: " + defaultNewRelayArchiveDirPath)
newRelayArchiveDirPath = defaultNewRelayArchiveDirPath
}

existingRelayArchivePath := os.Getenv(existingRelayArchiveEnvVar)
if existingRelayArchivePath == "" {
log.Printf(existingRelayArchiveEnvVar + " env var not set. Using default: " + defaultExistingRelayArchive)
existingRelayArchivePath = defaultExistingRelayArchive
s3Bucket := os.Getenv(s3BucketEnvVar)
if s3Bucket == "" {
log.Fatalf("Required env var [%s] not set.", s3BucketEnvVar)
}

newRelayArchivePath := os.Getenv(newRelayArchiveEnvVar)
if newRelayArchivePath == "" {
log.Printf(newRelayArchiveEnvVar + " env var not set. Using default: " + newRelayArchivePath)
newRelayArchivePath = defaultNewRelayArchive
sdkConfig, err := config.LoadDefaultConfig(ctx)
if err != nil {
log.Fatalf("Couldn't load default aws configuration. Have you set up your AWS account? %v", err)
return
}

localFileArchiveService := dorkly.NewLocalFileRelayArchiveService("temp/flags.tar.gz")
reconciler := dorkly.NewReconciler(localFileArchiveService, dorklyYamlInputPath)
s3Client := s3.NewFromConfig(sdkConfig)
s3ArchiveService, err := dorkly.NewS3RelayArchiveService(s3Client, s3Bucket)
reconciler := dorkly.NewReconciler(s3ArchiveService, dorklyYamlInputPath)

err := reconciler.Reconcile(context.Background())
err = reconciler.Reconcile(context.Background())
if err != nil {
log.Fatal(err)
}
Expand Down
8 changes: 7 additions & 1 deletion internal/dorkly/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dorkly

import (
"context"
"errors"
"log"
"reflect"
)
Expand All @@ -21,7 +22,12 @@ func NewReconciler(archiveService RelayArchiveService, projectYamlPath string) *
func (r *Reconciler) Reconcile(ctx context.Context) error {
existingArchive, err := r.archiveService.fetchExisting(ctx)
if err != nil {
return err
if errors.Is(err, ErrExistingArchiveNotFound) {
log.Println("Existing archive not found. Creating new empty archive.")
existingArchive = &RelayArchive{}
} else {
return err
}
}

project, err := loadProjectYamlFiles(r.projectYamlPath)
Expand Down
10 changes: 10 additions & 0 deletions internal/dorkly/relay_archive_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package dorkly

import (
"context"
"errors"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"io"
"log"
"os"
Expand All @@ -16,6 +18,10 @@ const (
s3ObjectKey = "flags.tar.gz"
)

var (
ErrExistingArchiveNotFound = fmt.Errorf("existing relay archive not found")
)

type RelayArchiveService interface {
fetchExisting(ctx context.Context) (*RelayArchive, error)
saveNew(ctx context.Context, relayArchive RelayArchive) error
Expand Down Expand Up @@ -47,6 +53,10 @@ func (s S3RelayArchiveService) fetchExisting(ctx context.Context) (*RelayArchive
})

if err != nil {
var nsk *types.NoSuchKey
if errors.As(err, &nsk) {
return nil, ErrExistingArchiveNotFound
}
return nil, err
}

Expand Down

0 comments on commit ffa7d37

Please sign in to comment.