Added some global flags to specify LDAP host and port to connect to

This commit is contained in:
Gregory Ballantine 2018-07-14 16:03:54 -04:00
parent 5d2a7f4304
commit 9e59f510a1
2 changed files with 14 additions and 1 deletions

View File

@ -7,6 +7,18 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var (
flagHost string
flagPort int
)
func init() {
// define global CLI flags
rootCmd.PersistentFlags().StringVarP(&flagHost, "host", "H", "ldap.example.com", "LDAP host to perform operations on")
rootCmd.PersistentFlags().IntVarP(&flagPort, "port", "p", 389, "TCP port that the LDAP host is listening on")
}
// define root command
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "muldap", Use: "muldap",
Short: "muldap is Metaunix.net's LDAP management tool", Short: "muldap is Metaunix.net's LDAP management tool",
@ -16,6 +28,7 @@ var rootCmd = &cobra.Command{
}, },
} }
// start CLI app
func Execute() { func Execute() {
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
fmt.Println(err) fmt.Println(err)

View File

@ -31,7 +31,7 @@ var searchCmd = &cobra.Command{
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) {
// create new LDAP connection // create new LDAP connection
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", "ldap.example.com", 389)) l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", flagHost, flagPort))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }