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 ( import (
"fmt" "fmt"
"log" "log"
"strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
ldap "gopkg.in/ldap.v2" ldap "gopkg.in/ldap.v2"
) )
var ( var (
flagAttrs string
flagBase string flagBase string
flagFilter string flagFilter string
flagIndent int flagIndent int
@ -16,6 +18,7 @@ var (
func init() { func init() {
// define search command flags // 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(&flagBase, "base", "b", "dc=example,dc=com", "LDAP search base DN")
searchCmd.Flags().StringVarP(&flagFilter, "filter", "f", "objectClass=*", "LDAP search filter") 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") 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", Short: "Search an LDAP directory",
Long: `Perform an LDAP search operation on an LDAP directory`, Long: `Perform an LDAP search operation on an LDAP directory`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var attrs []string = nil
if flagAttrs != "" {
attrs = strings.Split(flagAttrs, ",")
}
// create new LDAP connection // create new LDAP connection
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", flagHost, flagPort)) l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", flagHost, flagPort))
if err != nil { if err != nil {
@ -42,7 +50,7 @@ var searchCmd = &cobra.Command{
flagBase, // The base dn to search flagBase, // The base dn to search
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf("(%s)", flagFilter), // The filter to apply fmt.Sprintf("(%s)", flagFilter), // The filter to apply
nil, // A list attributes to retrieve attrs, // A list attributes to retrieve
nil, nil,
) )