Added Viper configuration tool to project

This commit is contained in:
Gregory Ballantine
2018-07-14 22:46:24 -04:00
parent 9e8bd1ae76
commit e41f40069b
7 changed files with 165 additions and 25 deletions

View File

@ -6,14 +6,11 @@ import (
"strconv"
"github.com/spf13/cobra"
"github.com/spf13/viper"
ldap "gopkg.in/ldap.v2"
)
var (
// add command global arguments
flagBindDn string
flagBindPw string
// user subcommand arguments
flagUserUsername string
flagUserEmail string
@ -24,11 +21,11 @@ var (
func init() {
// define add command flags
addCmd.PersistentFlags().StringVarP(&flagBindDn, "bind_dn", "D", "", "Privileged LDAP user to bind as")
addCmd.PersistentFlags().StringVarP(&flagBindPw, "bind_pw", "P", "", "Privileged LDAP user password")
// mark required flags
addCmd.MarkFlagRequired("bind_dn")
addCmd.MarkFlagRequired("bind_pw")
addCmd.PersistentFlags().StringP("bind_dn", "D", "", "Privileged LDAP user to bind as")
addCmd.PersistentFlags().StringP("bind_pw", "P", "", "Privileged LDAP user password")
// bind config file values to add flags
viper.BindPFlag("bind_dn", searchCmd.Flags().Lookup("bind_dn"))
viper.BindPFlag("bind_pw", searchCmd.Flags().Lookup("bind_pw"))
// define add user subcommand flags
addUserCmd.Flags().StringVarP(&flagUserUsername, "username", "u", "", "Username for a new user")
@ -59,14 +56,14 @@ var addUserCmd = &cobra.Command{
Long: `Create and add an LDAP user resource to your directory.`,
Run: func(cmd *cobra.Command, args []string) {
// create new LDAP connection
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", flagHost, flagPort))
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(flagBindDn, flagBindPw)
err = l.Bind(viper.GetString("bind_dn"), viper.GetString("bind_pw"))
if err != nil {
log.Fatal(err)
}

View File

@ -5,17 +5,16 @@ import (
"os"
"github.com/spf13/cobra"
)
var (
flagHost string
flagPort int
"github.com/spf13/viper"
)
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")
rootCmd.PersistentFlags().StringP("host", "H", "ldap.example.com", "LDAP host to perform operations on")
rootCmd.PersistentFlags().IntP("port", "p", 389, "TCP port that the LDAP host is listening on")
// bind config file values to flags
viper.BindPFlag("host", rootCmd.PersistentFlags().Lookup("host"))
viper.BindPFlag("port", rootCmd.PersistentFlags().Lookup("port"))
}
// define root command

View File

@ -6,22 +6,24 @@ import (
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
ldap "gopkg.in/ldap.v2"
)
var (
flagAttrs string
flagBase string
flagFilter string
flagIndent int
)
func init() {
// define search command flags
searchCmd.Flags().StringP("base", "b", "dc=example,dc=com", "LDAP search base DN")
searchCmd.Flags().IntP("indent", "i", 2, "Number of spaces to use while indenting search output")
searchCmd.Flags().StringVarP(&flagAttrs, "attributes", "a", "", "Comma-separated list of LDAP attributes to retrieve in the search")
searchCmd.Flags().StringVarP(&flagBase, "base", "b", "dc=example,dc=com", "LDAP search base DN")
searchCmd.Flags().StringVarP(&flagFilter, "filter", "f", "objectClass=*", "LDAP search filter")
searchCmd.Flags().IntVarP(&flagIndent, "indent", "i", 0, "Number of spaces to use while indenting search output")
// bind config file values to flags
viper.BindPFlag("search_base", searchCmd.Flags().Lookup("base"))
viper.BindPFlag("print_indent", searchCmd.Flags().Lookup("indent"))
// register search command
rootCmd.AddCommand(searchCmd)
@ -40,7 +42,7 @@ var searchCmd = &cobra.Command{
}
// create new LDAP connection
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", flagHost, flagPort))
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", viper.GetString("host"), viper.GetInt("port")))
if err != nil {
log.Fatal(err)
}
@ -48,7 +50,7 @@ var searchCmd = &cobra.Command{
// create a new LDAP search request
searchRequest := ldap.NewSearchRequest(
flagBase, // The base dn to search
viper.GetString("search_base"), // The base dn to search
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf("(%s)", flagFilter), // The filter to apply
attrs, // A list attributes to retrieve
@ -62,6 +64,6 @@ var searchCmd = &cobra.Command{
}
// print the results
sr.PrettyPrint(flagIndent)
sr.PrettyPrint(viper.GetInt("print_indent"))
},
}