-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.go
121 lines (101 loc) · 3.48 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"github.com/samber/sync-ssh-keys/datasources"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
var (
Version string
Build string
)
var (
outputPath = kingpin.Flag("output", "Write output to <file>. Default to stdout").Short('o').String()
wError = kingpin.Flag("Werror", "Treat warning as errors. Fatal error if organization, team or user does not exist.").String()
localPath = kingpin.Flag("local-path", "Path to a local authorized_keys file. It can be useful in case of network failure ;)").String()
githubEndpoint = kingpin.Flag("github-endpoint", "Github Enterprise endpoint.").Envar("GITHUB_ENDPOINT").String()
githubToken = kingpin.Flag("github-token", "Github personal token.").Envar("GITHUB_TOKEN").String()
githubOrg = kingpin.Flag("github-org", "Github organization name.").String()
githubTeams = kingpin.Flag("github-team", "Team(s) allowed to access server.").Strings()
githubUsernames = kingpin.Flag("github-username", "Username(s) allowed to access server.").Strings()
excludeGithubUsernames = kingpin.Flag("exclude-github-username", "Username(s) to explicitly exclude.").Strings()
gitlabEndpoint = kingpin.Flag("gitlab-endpoint", "Gitlab endpoint.").Envar("GITLAB_ENDPOINT").String()
gitlabToken = kingpin.Flag("gitlab-token", "Gitlab personal token.").Envar("GITLAB_TOKEN").String()
gitlabGroups = kingpin.Flag("gitlab-group", "Group allowed to access server.").Strings()
gitlabUsernames = kingpin.Flag("gitlab-username", "Username(s) allowed to access server.").Strings()
excludeGitlabUsernames = kingpin.Flag("exclude-gitlab-username", "Username(s) to explicitly exclude.").Strings()
)
func argvToNullable() {
// main
if outputPath != nil && len(*outputPath) == 0 {
outputPath = nil
}
// local
if localPath != nil && len(*localPath) == 0 {
localPath = nil
}
// github
if githubEndpoint != nil && len(*githubEndpoint) == 0 {
githubEndpoint = nil
}
if githubToken != nil && len(*githubToken) == 0 {
githubToken = nil
}
if githubOrg != nil && len(*githubOrg) == 0 {
githubOrg = nil
}
// gitlab
if gitlabEndpoint != nil && len(*gitlabEndpoint) == 0 {
gitlabEndpoint = nil
}
}
func checkInputErrors(srcs []datasources.Source) {
if len(srcs) == 0 {
kingpin.FatalUsage("Please provide a key source.")
}
// Check data source errors
for _, src := range srcs {
if err := src.CheckInputErrors(); err != "" {
kingpin.FatalUsage(err)
}
}
}
func main() {
kingpin.Version(Version + "-" + Build)
kingpin.Parse()
argvToNullable()
srcs := []datasources.Source{}
// Init Local key source
if localPath != nil {
srcs = append(srcs, datasources.NewLocalSource(
*localPath,
))
}
// Init Github key source
if githubOrg != nil || len(*githubTeams) > 0 || len(*githubUsernames) > 0 || len(*excludeGithubUsernames) > 0 {
srcs = append(srcs, datasources.NewGithubSource(
githubEndpoint,
githubToken,
githubOrg,
*githubTeams,
*githubUsernames,
*excludeGithubUsernames,
))
}
// Init Gitlab key source
if len(*gitlabGroups) > 0 || len(*gitlabUsernames) > 0 || len(*excludeGitlabUsernames) > 0 {
srcs = append(srcs, datasources.NewGitlabSource(
gitlabEndpoint,
*gitlabToken,
*gitlabGroups,
*gitlabUsernames,
*excludeGitlabUsernames,
))
}
checkInputErrors(srcs)
// fetch keys 🚀
keys := map[string][]string{}
for _, src := range srcs {
keys[src.GetName()] = src.GetKeys()
}
// prints ssh keys
PrintKeys(outputPath, keys)
}