Reorganized the code for the group command
This commit is contained in:
parent
9dc381c0a3
commit
1e1154da93
@ -1,4 +1,4 @@
|
|||||||
package cmd
|
package group
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@ -15,16 +15,6 @@ import (
|
|||||||
util "git.metaunix.net/metaunix.net/muldap/lib/util"
|
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() {
|
func init() {
|
||||||
// define group add subcommand flags
|
// define group add subcommand flags
|
||||||
groupAddCmd.Flags().StringP("base_ou", "o", "", "LDAP OU to create the new group entry under")
|
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
|
// bind config file values to group add flags
|
||||||
viper.BindPFlag("group.base_ou", groupAddCmd.Flags().Lookup("base_ou"))
|
viper.BindPFlag("group.base_ou", groupAddCmd.Flags().Lookup("base_ou"))
|
||||||
viper.BindPFlag("group.id_attr", groupAddCmd.Flags().Lookup("id_attribute"))
|
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
|
// define group add subcommand
|
||||||
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
|
|
||||||
var groupAddCmd = &cobra.Command{
|
var groupAddCmd = &cobra.Command{
|
||||||
Use: "add",
|
Use: "add",
|
||||||
Short: "Add an LDAP group to the directory",
|
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
53
cmd/group/delete.go
Normal 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
36
cmd/group/group.go
Normal 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")
|
||||||
|
},
|
||||||
|
}
|
@ -6,6 +6,8 @@ import (
|
|||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
|
||||||
|
"git.metaunix.net/metaunix.net/muldap/cmd/group"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -15,6 +17,9 @@ func init() {
|
|||||||
// bind config file values to flags
|
// bind config file values to flags
|
||||||
viper.BindPFlag("host", rootCmd.PersistentFlags().Lookup("host"))
|
viper.BindPFlag("host", rootCmd.PersistentFlags().Lookup("host"))
|
||||||
viper.BindPFlag("port", rootCmd.PersistentFlags().Lookup("port"))
|
viper.BindPFlag("port", rootCmd.PersistentFlags().Lookup("port"))
|
||||||
|
|
||||||
|
// register commands
|
||||||
|
rootCmd.AddCommand(group.GetGroupCmd())
|
||||||
}
|
}
|
||||||
|
|
||||||
// define root command
|
// define root command
|
||||||
|
Loading…
Reference in New Issue
Block a user