mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-24 05:27:56 +02:00
Basic backend structure and listen transfer implementation
This commit is contained in:
parent
80ef3f23c0
commit
8661075975
6 changed files with 254 additions and 1 deletions
|
@ -23,8 +23,11 @@ package cmd
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"go.uploadedlobster.com/scotty/backends"
|
||||
)
|
||||
|
||||
// listensCmd represents the listens command
|
||||
|
@ -34,12 +37,32 @@ var listensCmd = &cobra.Command{
|
|||
Long: `Transfers listens between two configured backends.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("listens called")
|
||||
sourceConfig := getConfigFromFlag(cmd, "from")
|
||||
targetConfig := getConfigFromFlag(cmd, "to")
|
||||
exportBackend, err := backends.ResolveBackend[backends.ListenExport](sourceConfig)
|
||||
cobra.CheckErr(err)
|
||||
importBackend, err := backends.ResolveBackend[backends.ListenImport](targetConfig)
|
||||
cobra.CheckErr(err)
|
||||
timestamp := time.Unix(0, 0)
|
||||
listens, err := exportBackend.ExportListens(timestamp)
|
||||
cobra.CheckErr(err)
|
||||
result, err := importBackend.ImportListens(listens, timestamp)
|
||||
cobra.CheckErr(err)
|
||||
fmt.Printf("Imported %v listens (last timestamp %v)\n", result.Count, result.LastTimestamp)
|
||||
},
|
||||
}
|
||||
|
||||
func getConfigFromFlag(cmd *cobra.Command, flagName string) *viper.Viper {
|
||||
configName := cmd.Flag(flagName).Value.String()
|
||||
config := viper.Sub(configName)
|
||||
if config == nil {
|
||||
cobra.CheckErr(fmt.Sprintf("Invalid source configuration \"%s\"", configName))
|
||||
}
|
||||
return config
|
||||
}
|
||||
|
||||
func init() {
|
||||
beamCmd.AddCommand(listensCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue