From f97531e765c05d41746241249e6e1e24a7ffb5df Mon Sep 17 00:00:00 2001 From: Lukas SP Date: Wed, 26 Aug 2020 22:12:36 +0200 Subject: [PATCH] Implement transfer tool --- cmd/transfer/main.go | 67 ++++++++++++++++++++++++++++++++++++++ internal/storage/driver.go | 34 ++++++++++--------- 2 files changed, 86 insertions(+), 15 deletions(-) create mode 100644 cmd/transfer/main.go diff --git a/cmd/transfer/main.go b/cmd/transfer/main.go new file mode 100644 index 0000000..9ee6521 --- /dev/null +++ b/cmd/transfer/main.go @@ -0,0 +1,67 @@ +package main + +import ( + "github.com/Lukaesebrot/pasty/internal/env" + "github.com/Lukaesebrot/pasty/internal/storage" + "log" + "os" +) + +func main() { + // Validate the command line arguments + if len(os.Args) != 3 { + panic("Invalid command line arguments") + return + } + + // Load the optional .env file + log.Println("Loading the optional .env file...") + env.Load() + + // Create and initialize the first (from) driver + from, err := storage.GetDriver(os.Args[1]) + if err != nil { + panic(err) + } + err = from.Initialize() + if err != nil { + panic(err) + } + + // Create and initialize the second (to) driver + to, err := storage.GetDriver(os.Args[2]) + if err != nil { + panic(err) + } + err = to.Initialize() + if err != nil { + panic(err) + } + + // Retrieve a list of IDs from the first (from) driver + ids, err := from.ListIDs() + if err != nil { + panic(err) + } + + // Transfer every paste to the second (to) driver + for _, id := range ids { + log.Println("Transferring ID " + id + "...") + + // Retrieve the paste + paste, err := from.Get(id) + if err != nil { + log.Println("[ERR]", err.Error()) + continue + } + + // Save the paste + err = to.Save(paste) + if err != nil { + log.Println("[ERR]", err.Error()) + continue + } + + log.Println("Transferred ID " + id + ".") + } +} diff --git a/internal/storage/driver.go b/internal/storage/driver.go index c2b59a1..595de61 100644 --- a/internal/storage/driver.go +++ b/internal/storage/driver.go @@ -23,27 +23,31 @@ type Driver interface { // Load loads the current storage driver func Load() error { // Define the driver to use - var driver Driver - storageType := strings.ToLower(env.Get("STORAGE_TYPE", "file")) - switch storageType { - case "file": - driver = new(FileDriver) - break - case "s3": - driver = new(S3Driver) - break - case "mongodb": - driver = new(MongoDBDriver) - break - default: - return fmt.Errorf("invalid storage type '%s'", storageType) + storageType := env.Get("STORAGE_TYPE", "file") + driver, err := GetDriver(storageType) + if err != nil { + return err } // Initialize the driver - err := driver.Initialize() + err = driver.Initialize() if err != nil { return err } Current = driver return nil } + +// GetDriver returns the driver with the given type string if it exists +func GetDriver(storageType string) (Driver, error) { + switch strings.ToLower(storageType) { + case "file": + return new(FileDriver), nil + case "s3": + return new(S3Driver), nil + case "mongodb": + return new(MongoDBDriver), nil + default: + return nil, fmt.Errorf("invalid storage type '%s'", storageType) + } +}