Write usage/errors to stderr

main v0.0.1
noah metz 2023-12-03 22:15:18 -07:00
parent d1eefd6be1
commit bbd6dd940e
2 changed files with 17 additions and 3 deletions

1
.gitignore vendored

@ -8,6 +8,7 @@
*.dll
*.so
*.dylib
main
# Test binary, built with `go test -c`
*.test

@ -6,7 +6,19 @@ import (
"os"
)
func usage(code int, err error) {
if err != nil {
os.Stderr.WriteString(fmt.Sprintf("error %s\n", err))
}
os.Stderr.WriteString(fmt.Sprintf("usage: %s [uri] [bind_dn] [bind_pw] [base_dn] [uid]\n", os.Args[0]))
os.Exit(code)
}
func main() {
if len(os.Args) != 6 {
usage(1, nil)
}
var ldap_uri string = os.Args[1]
var ldap_bind_dn string = os.Args[2]
var ldap_bind_pw string = os.Args[3]
@ -15,19 +27,20 @@ func main() {
l, err := ldap.DialURL(ldap_uri)
if err != nil {
panic(err)
usage(2, err)
}
defer l.Close()
err = l.Bind(ldap_bind_dn, ldap_bind_pw)
if err != nil {
panic(err)
usage(3, err)
}
search_request := ldap.NewSearchRequest(ldap_base_dn, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, fmt.Sprintf("(&(objectClass=posixAccount)(uid=%s))", uid), []string{"dn", "sshPublicKey"}, nil)
sr, err := l.Search(search_request)
if err != nil {
panic(err)
usage(4, err)
}
for _, entry := range(sr.Entries) {