import.go

v1.3.4
Doc Versions Source
1
package main
2
3
import (
4
	"fmt"
5
	"log"
6
7
	"github.com/spf13/cobra"
8
9
	"go.bigb.es/curator/internal/config"
10
	"go.bigb.es/curator/internal/store"
11
)
12
13
func importCmd() *cobra.Command {
14
	var configPath string
15
16
	cmd := &cobra.Command{
17
		Use:   "import <old-config.json>",
18
		Short: "Import modules and patterns from a legacy JSON config file",
19
		Args:  cobra.ExactArgs(1),
20
		RunE: func(cmd *cobra.Command, args []string) error {
21
			return runImport(configPath, args[0])
22
		},
23
	}
24
25
	cmd.Flags().StringVarP(&configPath, "config", "c", "curator.yaml", "path to curator config file")
26
27
	return cmd
28
}
29
30
func runImport(configPath, legacyPath string) error {
31
	cfg, err := config.Load(configPath)
32
	if err != nil {
33
		return err
34
	}
35
36
	moduleStore, err := store.Open(cfg.Database.Type, cfg.Database.DSN)
37
	if err != nil {
38
		return fmt.Errorf("open store: %w", err)
39
	}
40
	defer moduleStore.Close()
41
42
	modules, patterns, err := store.ImportFromConfigFile(moduleStore, legacyPath)
43
	if err != nil {
44
		return fmt.Errorf("import: %w", err)
45
	}
46
47
	log.Printf("imported %d modules and %d patterns", modules, patterns)
48
	return nil
49
}
50

Source Files