-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtarget.go
More file actions
75 lines (64 loc) · 1.87 KB
/
target.go
File metadata and controls
75 lines (64 loc) · 1.87 KB
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
package manager
import (
"errors"
"fmt"
"path"
)
type Platform interface {
InitFormUnmarshaler(unmarshaler func(any) error) (Target, error)
}
var enabledPlatform = make(map[string]Platform)
func RegisterPlatform(name string, platform Platform) {
enabledPlatform[name] = platform
}
func InitTarget(platformKey string, unmarshaler func(any) error) (Target, error) {
if p, exist := enabledPlatform[platformKey]; exist {
target, err := p.InitFormUnmarshaler(unmarshaler)
if target.GetPlatform() == "" || target.GetTargetSlug() == "" {
err = fmt.Errorf("Platform Or Slug of %s config not exist", path.Ext(platformKey))
}
return target, err
}
return nil, fmt.Errorf("Platform %s not exist", platformKey)
}
type Target interface {
TargetEntry
GetTarget() Target
GetTargetSlug() string
GetPlatform() string
GetRootDepartment() (DepartmentableEntry, error)
GetAllUsers() (users []UserableEntry, err error)
}
func TargetKey(t Target) string {
return fmt.Sprintf("%s@%s", t.GetTargetSlug(), t.GetPlatform())
}
type TargetWithEnterpriseEmail interface {
GetEnterpriseEmailDomains() []string
}
func RecursionGetAllUsersIncludeChildDepartments(department DepartmentableEntry) (users []UserableEntry, err error) {
usersInThis, err := department.GetUsers()
if err != nil {
return
}
users = append(users, usersInThis...)
for _, childDepartment := range department.GetChildDepartments() {
usersUnder := make([]UserableEntry, 0)
usersUnder, err = RecursionGetAllUsersIncludeChildDepartments(childDepartment)
if err != nil {
return
}
users = append(users, usersUnder...)
}
return
}
func GetTargetByPlatformAndSlug(platform, slug string) (Target, error) {
for _, target := range Targets {
if target.GetTargetSlug() == slug && target.GetPlatform() == platform {
return target, nil
}
}
return nil, errors.New("target not found")
}
type Config struct {
Platform string
}