diff --git a/cmd/ldif/add.go b/cmd/ldif/add.go new file mode 100644 index 0000000..60d9fe1 --- /dev/null +++ b/cmd/ldif/add.go @@ -0,0 +1,81 @@ +package ldif + +import ( + "fmt" + "io/ioutil" + "log" + + "github.com/spf13/cobra" + "github.com/spf13/viper" + ldif "github.com/vetinari/ldif" + ldap "gopkg.in/ldap.v2" +) + +func init() { + // define ldif command flags + ldifAddCmd.Flags().StringVarP(&flagLdifFilePath, "file_path", "f", "", "LDIF file to import") +} + +// define ldif add subcommand +var ldifAddCmd = &cobra.Command{ + Use: "add", + Short: "Import an LDIF file, add resources", + Long: `Add LDAP resources using an LDIF file.`, + Run: func(cmd *cobra.Command, args []string) { + // create new LDAP connection + l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", viper.GetString("host"), viper.GetInt("port"))) + if err != nil { + log.Fatal(err) + } + defer l.Close() + + // bind as the admin user + err = l.Bind(viper.GetString("bind_dn"), viper.GetString("bind_pw")) + if err != nil { + log.Fatal(err) + } + + // read LDIF file to bytes + ldifBytes, err := ioutil.ReadFile(flagLdifFilePath) + if err != nil { + log.Fatal(err) + } + // convert bytes to string + ldifText := string(ldifBytes) + + // parse ldif text + ldifParse, err := ldif.Parse(ldifText) + if err != nil { + log.Fatal(err) + } + + // let the user know how many entries we found in the LDIF + fmt.Printf("I found %d entries in the LDIF file.\n", len(ldifParse.Entries)) + + // loop through each entry + for _, entryWrap := range ldifParse.Entries { + // grab the entry + entry := entryWrap.Entry + + // grab the entry DN + entryDn := entry.DN + + // create a new LDAP add request + addRequest := ldap.NewAddRequest(entryDn) + + // loop through and add the entry attributes + for _, entryAttr := range entry.Attributes { + addRequest.Attribute(entryAttr.Name, entryAttr.Values) + } + + // let the user know what is currently being added + fmt.Printf("I am adding %s right now.\n", entryDn) + + // perform the add operation + err = l.Add(addRequest) + if err != nil { + log.Fatal(err) + } + } + }, +} diff --git a/cmd/ldif/delete.go b/cmd/ldif/delete.go new file mode 100644 index 0000000..7b4df6a --- /dev/null +++ b/cmd/ldif/delete.go @@ -0,0 +1,76 @@ +package ldif + +import ( + "fmt" + "io/ioutil" + "log" + + "github.com/spf13/cobra" + "github.com/spf13/viper" + ldif "github.com/vetinari/ldif" + ldap "gopkg.in/ldap.v2" +) + +func init() { + // define ldif command flags + ldifDeleteCmd.Flags().StringVarP(&flagLdifFilePath, "file_path", "f", "", "LDIF file to import") +} + +// define ldif subcommand +var ldifDeleteCmd = &cobra.Command{ + Use: "delete", + Short: "Import an LDIF file, delete resources", + Long: `Delete LDAP resources using an LDIF file.`, + Run: func(cmd *cobra.Command, args []string) { + // create new LDAP connection + l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", viper.GetString("host"), viper.GetInt("port"))) + if err != nil { + log.Fatal(err) + } + defer l.Close() + + // bind as the admin user + err = l.Bind(viper.GetString("bind_dn"), viper.GetString("bind_pw")) + if err != nil { + log.Fatal(err) + } + + // read LDIF file to bytes + ldifBytes, err := ioutil.ReadFile(flagLdifFilePath) + if err != nil { + log.Fatal(err) + } + // convert bytes to string + ldifText := string(ldifBytes) + + // parse ldif text + ldifParse, err := ldif.Parse(ldifText) + if err != nil { + log.Fatal(err) + } + + // let the user know how many entries we found in the LDIF + fmt.Printf("I found %d entries in the LDIF file.\n", len(ldifParse.Entries)) + + // loop through each entry + for _, entryWrap := range ldifParse.Entries { + // grab the entry + entry := entryWrap.Entry + + // grab the entry DN + entryDn := entry.DN + + // create a new LDAP delete request + deleteRequest := ldap.NewDelRequest(entryDn, []ldap.Control{}) + + // let the user know what is currently being deleted + fmt.Printf("I am deleting %s right now.\n", entryDn) + + // perform the delete operation + err = l.Del(deleteRequest) + if err != nil { + log.Fatal(err) + } + } + }, +} diff --git a/cmd/ldif/init.go b/cmd/ldif/init.go index e042a36..6a49454 100644 --- a/cmd/ldif/init.go +++ b/cmd/ldif/init.go @@ -2,13 +2,8 @@ package ldif import ( "fmt" - "io/ioutil" - "log" "github.com/spf13/cobra" - "github.com/spf13/viper" - ldif "github.com/vetinari/ldif" - ldap "gopkg.in/ldap.v2" ) var ( @@ -17,70 +12,16 @@ var ( ) func init() { - // define ldif command flags - LdifCmd.Flags().StringVarP(&flagLdifFilePath, "file_path", "f", "", "LDIF file to import") + // register LDIF command and its subcommands + LdifCmd.AddCommand(ldifAddCmd, ldifDeleteCmd) } // define ldif subcommand var LdifCmd = &cobra.Command{ Use: "ldif", Short: "Import an LDIF file", - Long: `Import an LDIF file to your directory.`, + Long: `Manage LDAP resources using an LDIF file.`, Run: func(cmd *cobra.Command, args []string) { - // create new LDAP connection - l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", viper.GetString("host"), viper.GetInt("port"))) - if err != nil { - log.Fatal(err) - } - defer l.Close() - - // bind as the admin user - err = l.Bind(viper.GetString("bind_dn"), viper.GetString("bind_pw")) - if err != nil { - log.Fatal(err) - } - - // read LDIF file to bytes - ldifBytes, err := ioutil.ReadFile(flagLdifFilePath) - if err != nil { - log.Fatal(err) - } - // convert bytes to string - ldifText := string(ldifBytes) - - // parse ldif text - ldifParse, err := ldif.Parse(ldifText) - if err != nil { - log.Fatal(err) - } - - // let the user know how many entries we found in the LDIF - fmt.Printf("I found %d entries in the LDIF file.\n", len(ldifParse.Entries)) - - // loop through each entry - for _, entryWrap := range ldifParse.Entries { - // grab the entry - entry := entryWrap.Entry - - // grab the entry DN - entryDn := entry.DN - - // create a new LDAP add request - addRequest := ldap.NewAddRequest(entryDn) - - // loop through and add the entry attributes - for _, entryAttr := range entry.Attributes { - addRequest.Attribute(entryAttr.Name, entryAttr.Values) - } - - // let the user know what is currently being added - fmt.Printf("I am adding %s right now.\n", entryDn) - - // perform the add operation - err = l.Add(addRequest) - if err != nil { - log.Fatal(err) - } - } + fmt.Println("Available LDIF subcommands are: add, delete") }, }