Updated LDIF command to have add and delete subcommands

This commit is contained in:
Gregory Ballantine 2018-08-27 16:43:45 -04:00
parent b98d681bb0
commit 5acdc024ae
3 changed files with 161 additions and 63 deletions

81
cmd/ldif/add.go Normal file
View File

@ -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)
}
}
},
}

76
cmd/ldif/delete.go Normal file
View File

@ -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)
}
}
},
}

View File

@ -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")
},
}