-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (74 loc) · 2.43 KB
/
main.go
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
86
87
package main
import (
"auth0-backup-tool/pkg"
"flag"
"fmt"
"gopkg.in/auth0.v3/management"
"os"
"strings"
)
type Flags struct {
ConfigFile string
ClientId string
ClientSecret string
Domain string
UsersFile string
UserAttributes string
Connection string
Action string
}
var RequiredFlags = []string{
"client-id",
"client-secret",
"domain",
"action",
}
func parseFlags() Flags {
flags := Flags{}
flag.StringVar(&flags.ClientId, "client-id", "", "Client ID of an application with user management rights")
flag.StringVar(&flags.ClientSecret, "client-secret", "", "Client secret of an application with user management rights")
flag.StringVar(&flags.Domain, "domain", "", "Auth0 domain")
flag.StringVar(&flags.Connection, "connection", "", "Auth0 connection ID. If it's empty a backup to all connections will be performed")
flag.StringVar(&flags.UsersFile, "users-file", "users-export.json", "File path where to store the exported users or where to read the users to import. When export all connections this flag contains the folder name where to store all exported files")
flag.StringVar(&flags.UserAttributes, "user-attributes", "", "List of user attributes to export. Format: attr1,attr2,attr3")
flag.StringVar(&flags.Action, "action", "", "Action to perform. Can be 'import' or 'export'")
flag.Parse()
checkNeededFlags(flags)
return flags
}
func abortWithCommandHelp() {
fmt.Fprintf(os.Stderr, "\n\nUsage of %s:\n", os.Args[0])
flag.PrintDefaults()
os.Exit(2) // the same exit code flag.Parse uses
}
func checkNeededFlags(flags Flags) {
seen := make(map[string]bool)
var missingFlags = false
flag.Visit(func(f *flag.Flag) { seen[f.Name] = true })
for _, req := range RequiredFlags {
if !seen[req] {
fmt.Fprintf(os.Stderr, "missing required -%s argument/flag\n", req)
missingFlags = true
}
}
if missingFlags {
abortWithCommandHelp()
}
}
func main() {
flags := parseFlags()
manager, _ := management.New(flags.Domain, flags.ClientId, flags.ClientSecret)
switch flags.Action {
case "export":
if flags.Connection == "" {
pkg.ExportFromAllConnections(manager, flags.UsersFile)
} else {
pkg.ExportUsers(manager.Job, flags.Connection, strings.Split(flags.UserAttributes, ","), flags.UsersFile)
}
case "import":
pkg.ImportUsers(manager.Job, flags.Connection, flags.UsersFile, false)
default:
fmt.Fprintf(os.Stderr, "Wrong action '%s'", flags.Action)
abortWithCommandHelp()
}
}