1
0
mirror of https://github.com/lus/pasty.git synced 2023-08-10 21:13:09 +03:00

Implement transfer tool

This commit is contained in:
Lukas SP 2020-08-26 22:12:36 +02:00
parent 5a9a5a8885
commit f97531e765
2 changed files with 86 additions and 15 deletions

67
cmd/transfer/main.go Normal file
View File

@ -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 + ".")
}
}

View File

@ -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)
}
}