Skip to content

Commit

Permalink
Cleanup multistorage parsing slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Desrosiers committed Feb 3, 2018
1 parent 01e0a50 commit 3ba57e9
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"log"
"strings"

"github.com/google/shlex"
flags "github.com/jessevdk/go-flags"
Expand Down Expand Up @@ -48,31 +49,38 @@ type Options struct {

// createStorageFromOption takes an Option struct and based on the StorageType field constructs a storage.Storage and returns it.
func createStorageFromOption(opts *Options) (storage.NamedStorage, error) {
switch opts.StorageType {
case "Inmem":
switch strings.ToLower(opts.StorageType) {
case "inmem":
log.Printf("Setting up an Inmem Storage layer with short code length of '%d'", opts.Inmem.RandLength)

return storage.NewInmem(opts.Inmem.RandLength)
case "S3":
case "s3":
log.Println("Setting up an S3 Storage layer")

if len(opts.S3.BucketName) == 0 {
log.Fatalf("BucketName has be something (currently empty)")
}

return storage.NewS3(nil, opts.S3.BucketName)
case "Filesystem":
case "filesystem":
log.Printf("Setting up a Filesystem storage layer with root: %v", opts.Filesystem.RootPath)

return storage.NewFilesystem(opts.Filesystem.RootPath)
case "Regex":
case "regex":
log.Printf("Setting up a Regex storage with %v remaps", opts.Regex.Remaps)

return storage.NewRegexFromList(opts.Regex.Remaps)
case "Multistorage":
log.Printf("Setting up a Multilayer Storage")
case "postgres":
log.Printf("Setting up a Postgres backed storage layer")

return storage.NewPostgres(opts.Postgres.ConnectString)
case "multistorage":
storageCount := len(opts.Multistorage.StorageArgs)
if storageCount == 0 {
log.Fatal("Multistorage requires at least one child storage")
}
log.Printf("Setting up a Multilayer Storage with %d children", storageCount)

storageNames := make([]string, 0, storageCount)
storages := make([]storage.NamedStorage, 0, storageCount)
for i, rawArgs := range opts.Multistorage.StorageArgs {
Expand All @@ -92,21 +100,12 @@ func createStorageFromOption(opts *Options) (storage.NamedStorage, error) {
return nil, errors.Wrapf(err, "failed to create storage #%d from args", i)
}

nstore, ok := store.(storage.NamedStorage)
if !ok {
return nil, errors.New("MultiStorage only supports NamedStorage backends")
}

storageNames = append(storageNames, subOpt.StorageType)
storages = append(storages, nstore)
storages = append(storages, store)
}

log.Printf("Multilayer Storage created with children: %v", storageNames)
log.Printf("Multilayer Storage created with children: %v", strings.Join(storageNames, ", "))
return multistorage.Simple(storages...)
case "Postgres":
log.Printf("Setting up a Postgres backed storage layer")

return storage.NewPostgres(opts.Postgres.ConnectString)
default:
return nil, fmt.Errorf("Unsupported storage-type: '%s'", opts.StorageType)
}
Expand Down

0 comments on commit 3ba57e9

Please sign in to comment.