mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-16 01:59:29 +02:00
131 lines
4.5 KiB
Go
131 lines
4.5 KiB
Go
/*
|
|
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
"go.uploadedlobster.com/scotty/backends"
|
|
"go.uploadedlobster.com/scotty/models"
|
|
"go.uploadedlobster.com/scotty/storage"
|
|
)
|
|
|
|
// lovesCmd represents the loves command
|
|
var lovesCmd = &cobra.Command{
|
|
Use: "loves",
|
|
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"))
|
|
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
|
|
token, err := db.GetOAuth2Token(sourceName)
|
|
cobra.CheckErr(err)
|
|
auth, err := backends.Authenticate(exportBackend, token, viper.GetViper())
|
|
cobra.CheckErr(err)
|
|
if auth {
|
|
db.SetOAuth2Token(sourceName, token)
|
|
}
|
|
|
|
token, err = db.GetOAuth2Token(targetName)
|
|
cobra.CheckErr(err)
|
|
auth, err = backends.Authenticate(importBackend, token, viper.GetViper())
|
|
cobra.CheckErr(err)
|
|
if auth {
|
|
defer db.SetOAuth2Token(targetName, token)
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
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
|
|
lovesChan := make(chan models.LovesResult, 1000)
|
|
go exportBackend.ExportLoves(timestamp, lovesChan, exportProgress)
|
|
|
|
// Import into target
|
|
resultChan := make(chan models.ImportResult)
|
|
go backends.ProcessLovesImports(importBackend, lovesChan, resultChan, importProgress)
|
|
result := <-resultChan
|
|
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
|
|
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)
|
|
}
|
|
}
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
beamCmd.AddCommand(lovesCmd)
|
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
// Cobra supports Persistent Flags which will work for this command
|
|
// and all subcommands, e.g.:
|
|
// lovesCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
|
|
// Cobra supports local flags which will only run when this command
|
|
// is called directly, e.g.:
|
|
// lovesCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
}
|