PKGBUILD/cli/imp/imp.go

87 lines
1.6 KiB
Go
Raw Permalink Normal View History

2020-11-06 22:09:26 +00:00
package imp
import (
2021-04-09 14:29:09 +00:00
"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"
2020-11-06 22:09:26 +00:00
"github.com/spf13/cobra"
)
func InitCmd(cmd *cobra.Command) error {
importCmd := &cobra.Command{
Use: "import",
2021-04-09 14:29:09 +00:00
Args: cobra.RangeArgs(1, 2),
2020-11-06 22:09:26 +00:00
Short: "Import data from passed URL",
2021-04-09 14:29:09 +00:00
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,
2020-11-06 22:09:26 +00:00
}
cmd.AddCommand(importCmd)
return nil
}
func importSources(cmd *cobra.Command, args []string) error {
2021-04-09 14:29:09 +00:00
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)
2020-11-06 22:09:26 +00:00
}