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

118
Gopkg.lock generated
View File

@ -1,6 +1,33 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
digest = "1:abeb38ade3f32a92943e5be54f55ed6d6e3b6602761d74b4aab4c9dd45c18abd"
name = "github.com/fsnotify/fsnotify"
packages = ["."]
pruneopts = "UT"
revision = "c2828203cd70a50dcccfb2761f8b1f8ceef9a8e9"
version = "v1.4.7"
[[projects]]
branch = "master"
digest = "1:ac64f01acc5eeea9dde40e326de6b6471e501392ec06524c3b51033aa50789bc"
name = "github.com/hashicorp/hcl"
packages = [
".",
"hcl/ast",
"hcl/parser",
"hcl/printer",
"hcl/scanner",
"hcl/strconv",
"hcl/token",
"json/parser",
"json/scanner",
"json/token",
]
pruneopts = "UT"
revision = "ef8a98b0bbce4a65b5aa4c368430a80ddc533168"
[[projects]]
digest = "1:870d441fe217b8e689d7949fef6e43efbc787e50f200cb1e70dbca9204a1d6be"
name = "github.com/inconshreveable/mousetrap"
@ -9,6 +36,49 @@
revision = "76626ae9c91c4f2a10f34cad8ce83ea42c93bb75"
version = "v1.0"
[[projects]]
digest = "1:c568d7727aa262c32bdf8a3f7db83614f7af0ed661474b24588de635c20024c7"
name = "github.com/magiconair/properties"
packages = ["."]
pruneopts = "UT"
revision = "c2353362d570a7bfa228149c62842019201cfb71"
version = "v1.8.0"
[[projects]]
branch = "master"
digest = "1:e730597b38a4d56e2361e0b6236cb800e52c73cace2ff91396f4ff35792ddfa7"
name = "github.com/mitchellh/mapstructure"
packages = ["."]
pruneopts = "UT"
revision = "bb74f1db0675b241733089d5a1faa5dd8b0ef57b"
[[projects]]
digest = "1:95741de3af260a92cc5c7f3f3061e85273f5a81b5db20d4bd68da74bd521675e"
name = "github.com/pelletier/go-toml"
packages = ["."]
pruneopts = "UT"
revision = "c01d1270ff3e442a8a57cddc1c92dc1138598194"
version = "v1.2.0"
[[projects]]
digest = "1:37ace7f35375adec11634126944bdc45a673415e2fcc07382d03b75ec76ea94c"
name = "github.com/spf13/afero"
packages = [
".",
"mem",
]
pruneopts = "UT"
revision = "787d034dfe70e44075ccc060d346146ef53270ad"
version = "v1.1.1"
[[projects]]
digest = "1:516e71bed754268937f57d4ecb190e01958452336fa73dbac880894164e91c1f"
name = "github.com/spf13/cast"
packages = ["."]
pruneopts = "UT"
revision = "8965335b8c7107321228e3e3702cab9832751bac"
version = "v1.2.0"
[[projects]]
digest = "1:872fa275c31e1f9db31d66fa9b1d4a7bb9a080ff184e6977da01f36bfbe07f11"
name = "github.com/spf13/cobra"
@ -17,6 +87,14 @@
revision = "ef82de70bb3f60c65fb8eebacbb2d122ef517385"
version = "v0.0.3"
[[projects]]
branch = "master"
digest = "1:080e5f630945ad754f4b920e60b4d3095ba0237ebf88dc462eb28002932e3805"
name = "github.com/spf13/jwalterweatherman"
packages = ["."]
pruneopts = "UT"
revision = "7c0cea34c8ece3fbeb2b27ab9b59511d360fb394"
[[projects]]
digest = "1:9424f440bba8f7508b69414634aef3b2b3a877e522d8a4624692412805407bb7"
name = "github.com/spf13/pflag"
@ -25,6 +103,37 @@
revision = "583c0c0531f06d5278b7d917446061adc344b5cd"
version = "v1.0.1"
[[projects]]
digest = "1:59e7dceb53b4a1e57eb1eb0bf9951ff0c25912df7660004a789b62b4e8cdca3b"
name = "github.com/spf13/viper"
packages = ["."]
pruneopts = "UT"
revision = "b5e8006cbee93ec955a89ab31e0e3ce3204f3736"
version = "v1.0.2"
[[projects]]
branch = "master"
digest = "1:d773e525476aefa22ea944a5425a9bfb99819b2e67eeb9b1966454fd57522bbf"
name = "golang.org/x/sys"
packages = ["unix"]
pruneopts = "UT"
revision = "1b2967e3c290b7c545b3db0deeda16e9be4f98a2"
[[projects]]
digest = "1:038003d098ffc345c4c5c6d47bcc6920b2649ee0c0d557162e54e75c76cadf8b"
name = "golang.org/x/text"
packages = [
"internal/gen",
"internal/triegen",
"internal/ucd",
"transform",
"unicode/cldr",
"unicode/norm",
]
pruneopts = "UT"
revision = "f21a4dfb5e38f5895301dc265a8def02365cc3d0"
version = "v0.3.0"
[[projects]]
digest = "1:81e1c5cee195fca5de06e2540cb63eea727a850b7e5c213548e7f81521c97a57"
name = "gopkg.in/asn1-ber.v1"
@ -41,11 +150,20 @@
revision = "bb7a9ca6e4fbc2129e3db588a34bc970ffe811a9"
version = "v2.5.1"
[[projects]]
digest = "1:342378ac4dcb378a5448dd723f0784ae519383532f5e70ade24132c4c8693202"
name = "gopkg.in/yaml.v2"
packages = ["."]
pruneopts = "UT"
revision = "5420a8b6744d3b0345ab293f6fcba19c978f1183"
version = "v2.2.1"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
input-imports = [
"github.com/spf13/cobra",
"github.com/spf13/viper",
"gopkg.in/ldap.v2",
]
solver-name = "gps-cdcl"

View File

@ -36,3 +36,7 @@
[[constraint]]
name = "github.com/spf13/cobra"
version = "0.0.3"
[[constraint]]
name = "github.com/spf13/viper"
version = "1.0.2"

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

6
config.toml Normal file
View File

@ -0,0 +1,6 @@
host = "ldap.example.com"
port = 389
search_base = "dc=example,dc=com"
print_indent = 2
bind_dn = ""
bind_pw = ""

View File

@ -1,9 +1,23 @@
package main
import (
"fmt"
"github.com/spf13/viper"
"git.metaunix.net/metaunix.net/muldap/cmd"
)
func main() {
// load configuration
viper.SetConfigName("config")
viper.AddConfigPath("$HOME/.muldap")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
// run CLI app w/ Cobra
cmd.Execute()
}