Added attributes parameter to search commnad

This commit is contained in:
Gregory Ballantine 2018-07-14 19:46:42 -04:00
parent 9e59f510a1
commit 68dabe3165

View File

@ -3,12 +3,14 @@ package cmd
import (
"fmt"
"log"
"strings"
"github.com/spf13/cobra"
ldap "gopkg.in/ldap.v2"
)
var (
flagAttrs string
flagBase string
flagFilter string
flagIndent int
@ -16,6 +18,7 @@ var (
func init() {
// define search command flags
searchCmd.Flags().StringVarP(&flagAttrs, "attributes", "a", "", "Comma-separated list of LDAP attributes to retrieve in the search")
searchCmd.Flags().StringVarP(&flagBase, "base", "b", "dc=example,dc=com", "LDAP search base DN")
searchCmd.Flags().StringVarP(&flagFilter, "filter", "f", "objectClass=*", "LDAP search filter")
searchCmd.Flags().IntVarP(&flagIndent, "indent", "i", 0, "Number of spaces to use while indenting search output")
@ -30,6 +33,11 @@ var searchCmd = &cobra.Command{
Short: "Search an LDAP directory",
Long: `Perform an LDAP search operation on an LDAP directory`,
Run: func(cmd *cobra.Command, args []string) {
var attrs []string = nil
if flagAttrs != "" {
attrs = strings.Split(flagAttrs, ",")
}
// create new LDAP connection
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", flagHost, flagPort))
if err != nil {
@ -42,7 +50,7 @@ var searchCmd = &cobra.Command{
flagBase, // The base dn to search
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf("(%s)", flagFilter), // The filter to apply
nil, // A list attributes to retrieve
attrs, // A list attributes to retrieve
nil,
)