aboutsummaryrefslogtreecommitdiff
path: root/cmd/mt-auth-convert/convert.go
blob: 28e44e9a9714d0479ea7c6b9696e9e387c9be764 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
mt-auth-convert converts between authentication backends.

Usage:

	mt-auth-convert from to inconn outconn

where from is the format to convert from
and to is the format to convert to
and inconn is the postgres connection string for the source database
and outconn is the postgres connection string for the destination database.
*/
package main

import (
	"log"
	"os"

	proxy "github.com/HimbeerserverDE/mt-multiserver-proxy"
)

func main() {
	if len(os.Args) != 5 {
		log.Fatal("usage: mt-auth-convert from to inconn outconn")
	}

	var inBackend proxy.AuthBackend
	switch os.Args[1] {
	case "files":
		inBackend = proxy.AuthFiles{}
	case "mtsqlite3":
		var err error
		inBackend, err = proxy.NewAuthMTSQLite3()
		if err != nil {
			log.Fatal(err)
		}
	case "mtpostgresql":
		var err error
		inBackend, err = proxy.NewAuthMTPostgreSQL(os.Args[3])
		if err != nil {
			log.Fatal(err)
		}
	default:
		log.Fatal("invalid input auth backend")
	}

	var outBackend proxy.AuthBackend
	switch os.Args[2] {
	case "files":
		outBackend = proxy.AuthFiles{}
	case "mtsqlite3":
		var err error
		outBackend, err = proxy.NewAuthMTSQLite3()
		if err != nil {
			log.Fatal(err)
		}
	case "mtpostgresql":
		var err error
		outBackend, err = proxy.NewAuthMTPostgreSQL(os.Args[4])
		if err != nil {
			log.Fatal(err)
		}
	default:
		log.Fatal("invalid output auth backend")
	}

	if err := convert(outBackend, inBackend); err != nil {
		log.Fatal(err)
	}

	log.Print("conversion successful")
}

func convert(dst, src proxy.AuthBackend) error {
	users, err := src.Export()
	if err != nil {
		return err
	}

	if err := dst.Import(users); err != nil {
		return err
	}

	return nil
}