package imp import ( "context" "fmt" "net/url" "git.cryptic.systems/volker.raschek/flucky/pkg/config" "git.cryptic.systems/volker.raschek/flucky/pkg/repository" "git.cryptic.systems/volker.raschek/go-logger" "github.com/spf13/cobra" ) func InitCmd(cmd *cobra.Command) error { importCmd := &cobra.Command{ Use: "import", Args: cobra.RangeArgs(1, 2), Short: "Import data from passed URL", Example: `import sqlite3:///var/cache/flucky/sqlite3.db import sqlite3:///var/cache/flucky/sqlite3.db postgres://user:password@host:port/database?sslmode=disable`, RunE: importSources, } cmd.AddCommand(importCmd) return nil } func importSources(cmd *cobra.Command, args []string) error { configFile, err := cmd.Flags().GetString("config") if err != nil { return fmt.Errorf("No config file defined") } cnf, err := config.Read(configFile) if err != nil { return err } logLevelString, err := cmd.Flags().GetString("loglevel") if err != nil { return err } logLevel, err := logger.ParseLogLevel(logLevelString) if err != nil { return err } flogger := logger.NewLogger(logLevel) var ( srcURL *url.URL destURL *url.URL ) srcURL, err = url.Parse(args[0]) if err != nil { return err } switch len(args) { case 1: destURL, err = url.Parse(cnf.DSN) if err != nil { return err } case 2: destURL, err = url.Parse(args[1]) if err != nil { return err } } srcRepository, err := repository.New(srcURL, flogger) if err != nil { return err } destRepository, err := repository.New(destURL, flogger) if err != nil { return err } return destRepository.Import(context.Background(), srcRepository) }