From 68dabe316533322e2623a7ff0ae7a6e9753070ce Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Sat, 14 Jul 2018 19:46:42 -0400 Subject: [PATCH] Added attributes parameter to search commnad --- cmd/search.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/cmd/search.go b/cmd/search.go index 5e594bd..e2ff759 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -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, )