Reorganized the code for the group command

This commit is contained in:
Gregory Ballantine 2018-07-15 23:53:33 -04:00
parent 9dc381c0a3
commit 1e1154da93
4 changed files with 96 additions and 67 deletions

View File

@ -1,4 +1,4 @@
package cmd
package group
import (
"errors"
@ -15,16 +15,6 @@ import (
util "git.metaunix.net/metaunix.net/muldap/lib/util"
)
var (
// group subcommand arguments
flagGroupName string
flagGroupType string
flagGroupIdNumber int
flagGroupMembers string
validTypes []string = []string{"app", "unix"}
)
func init() {
// define group add subcommand flags
groupAddCmd.Flags().StringP("base_ou", "o", "", "LDAP OU to create the new group entry under")
@ -36,31 +26,9 @@ func init() {
// bind config file values to group add flags
viper.BindPFlag("group.base_ou", groupAddCmd.Flags().Lookup("base_ou"))
viper.BindPFlag("group.id_attr", groupAddCmd.Flags().Lookup("id_attribute"))
// define group delete subcommand flags
groupDeleteCmd.Flags().StringP("base_ou", "o", "", "LDAP OU where your group entries are stored")
groupDeleteCmd.Flags().StringP("id_attribute", "a", "cn", "LDAP DN attribute for groups")
groupDeleteCmd.Flags().StringVarP(&flagGroupName, "groupname", "n", "", "Username of group to delete")
// bind config file values to group delete flags
viper.BindPFlag("group.base_ou", groupDeleteCmd.Flags().Lookup("base_ou"))
viper.BindPFlag("group.id_attr", groupDeleteCmd.Flags().Lookup("id_attribute"))
// register add command and subcommands
groupCmd.AddCommand(groupAddCmd, groupDeleteCmd)
rootCmd.AddCommand(groupCmd)
}
// define group command
var groupCmd = &cobra.Command{
Use: "group",
Short: "Manage LDAP group resources",
Long: `Perform various LDAP operations on group resources.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Metaunix.net LDAP tool, group command. Available subcommands are: add, delete")
},
}
// define group subcommand
// define group add subcommand
var groupAddCmd = &cobra.Command{
Use: "add",
Short: "Add an LDAP group to the directory",
@ -146,36 +114,3 @@ var groupAddCmd = &cobra.Command{
}
},
}
// define group delete subcommand
var groupDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete an LDAP group from the directory",
Long: `Delete an LDAP group resource from the directory.`,
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 group
err = l.Bind(viper.GetString("bind_dn"), viper.GetString("bind_pw"))
if err != nil {
log.Fatal(err)
}
// set up group DN
groupDn := fmt.Sprintf("%s=%s,%s", viper.GetString("group.id_attr"), flagGroupName, viper.GetString("group.base_ou"))
// create a new delete request object
deleteRequest := ldap.NewDelRequest(groupDn, []ldap.Control{})
// perform the delete operation
err = l.Del(deleteRequest)
if err != nil {
log.Fatal(err)
}
},
}

53
cmd/group/delete.go Normal file
View File

@ -0,0 +1,53 @@
package group
import (
"fmt"
"log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
ldap "gopkg.in/ldap.v2"
)
func init() {
// define group delete subcommand flags
groupDeleteCmd.Flags().StringP("base_ou", "o", "", "LDAP OU where your group entries are stored")
groupDeleteCmd.Flags().StringP("id_attribute", "a", "cn", "LDAP DN attribute for groups")
groupDeleteCmd.Flags().StringVarP(&flagGroupName, "groupname", "n", "", "Username of group to delete")
// bind config file values to group delete flags
viper.BindPFlag("group.base_ou", groupDeleteCmd.Flags().Lookup("base_ou"))
viper.BindPFlag("group.id_attr", groupDeleteCmd.Flags().Lookup("id_attribute"))
}
// define group delete subcommand
var groupDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete an LDAP group from the directory",
Long: `Delete an LDAP group resource from the directory.`,
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 group
err = l.Bind(viper.GetString("bind_dn"), viper.GetString("bind_pw"))
if err != nil {
log.Fatal(err)
}
// set up group DN
groupDn := fmt.Sprintf("%s=%s,%s", viper.GetString("group.id_attr"), flagGroupName, viper.GetString("group.base_ou"))
// create a new delete request object
deleteRequest := ldap.NewDelRequest(groupDn, []ldap.Control{})
// perform the delete operation
err = l.Del(deleteRequest)
if err != nil {
log.Fatal(err)
}
},
}

36
cmd/group/group.go Normal file
View File

@ -0,0 +1,36 @@
package group
import (
"fmt"
"github.com/spf13/cobra"
)
var (
// group subcommand arguments
flagGroupName string
flagGroupType string
flagGroupIdNumber int
flagGroupMembers string
validTypes []string = []string{"app", "unix"}
)
func init() {
// register group subcommands
groupCmd.AddCommand(groupAddCmd, groupDeleteCmd)
}
func GetGroupCmd() *cobra.Command {
return groupCmd
}
// define group command
var groupCmd = &cobra.Command{
Use: "group",
Short: "Manage LDAP group resources",
Long: `Perform various LDAP operations on group resources.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Metaunix.net LDAP tool, group command. Available subcommands are: add, delete")
},
}

View File

@ -6,6 +6,8 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"git.metaunix.net/metaunix.net/muldap/cmd/group"
)
func init() {
@ -15,6 +17,9 @@ func init() {
// bind config file values to flags
viper.BindPFlag("host", rootCmd.PersistentFlags().Lookup("host"))
viper.BindPFlag("port", rootCmd.PersistentFlags().Lookup("port"))
// register commands
rootCmd.AddCommand(group.GetGroupCmd())
}
// define root command