refactor: generic common cmd processing

This commit is contained in:
Philipp Wolfer 2023-12-04 08:03:32 +01:00
parent a87686af57
commit 7c85ba05ab
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
5 changed files with 182 additions and 149 deletions

View file

@ -17,15 +17,9 @@ Scotty. If not, see <https://www.gnu.org/licenses/>.
package cmd
import (
"fmt"
"sync"
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uploadedlobster.com/scotty/internal/backends"
"go.uploadedlobster.com/scotty/internal/models"
"go.uploadedlobster.com/scotty/internal/storage"
)
// lovesCmd represents the loves command
@ -34,77 +28,15 @@ var lovesCmd = &cobra.Command{
Short: "Transfer loves between two services",
Long: `Transfers loves between two configured services.`,
Run: func(cmd *cobra.Command, args []string) {
sourceName, sourceConfig := getConfigFromFlag(cmd, "from")
targetName, targetConfig := getConfigFromFlag(cmd, "to")
fmt.Printf("Transferring loves from %s to %s...\n", sourceName, targetName)
// Setup database
db, err := storage.New(viper.GetString("database"))
exp, imp, err := resolveBackends[models.LovesExport, models.LovesImport, models.LovesResult](cmd)
cobra.CheckErr(err)
// Initialize backends
exportBackend, err := backends.ResolveBackend[models.LovesExport](sourceConfig)
cobra.CheckErr(err)
importBackend, err := backends.ResolveBackend[models.LovesImport](targetConfig)
cobra.CheckErr(err)
// Authenticate backends, if needed
_, err = backends.Authenticate(sourceName, exportBackend, db, viper.GetViper())
cobra.CheckErr(err)
_, err = backends.Authenticate(targetName, importBackend, db, viper.GetViper())
cobra.CheckErr(err)
// Read timestamp
timestamp := time.Unix(getInt64FromFlag(cmd, "timestamp"), 0)
if timestamp == time.Unix(0, 0) {
timestamp, err = db.GetImportTimestamp(sourceName, targetName, "loves")
cobra.CheckErr(err)
exp.processor = backends.LovesExportProcessor{
Backend: exp.backend,
}
fmt.Printf("From timestamp: %v (%v)\n", timestamp, timestamp.Unix())
// Prepare progress bars
exportProgress := make(chan models.Progress)
importProgress := make(chan models.Progress)
var wg sync.WaitGroup
progress := progressBar(&wg, exportProgress, importProgress)
// Export from source
exportChan := make(chan models.LovesResult, 1000)
go exportBackend.ExportLoves(timestamp, exportChan, exportProgress)
// Import into target
resultChan := make(chan models.ImportResult)
var processor = backends.LovesImportProcessor{
Backend: importBackend,
}
go processor.Process(exportChan, resultChan, importProgress)
result := <-resultChan
close(exportProgress)
wg.Wait()
progress.Wait()
if result.Error != nil {
fmt.Printf("Import failed, last reported timestamp was %v (%v)\n", result.LastTimestamp, result.LastTimestamp.Unix())
cobra.CheckErr(result.Error)
}
fmt.Printf("Imported %v of %v loves into %v.\n",
result.ImportCount, result.TotalCount, targetName)
// Update timestamp
if result.LastTimestamp.Unix() < timestamp.Unix() {
result.LastTimestamp = timestamp
}
fmt.Printf("Latest timestamp: %v (%v)\n", result.LastTimestamp, result.LastTimestamp.Unix())
err = db.SetImportTimestamp(sourceName, targetName, "loves", result.LastTimestamp)
cobra.CheckErr(err)
// Print errors
if len(result.ImportErrors) > 0 {
fmt.Printf("\nDuring the import the following errors occurred:\n")
for _, err := range result.ImportErrors {
fmt.Printf("Error: %v\n", err)
}
imp.processor = backends.LovesImportProcessor{
Backend: imp.backend,
}
cmdExportImport(cmd, "loves", exp, imp)
},
}