Skip to content

Commit

Permalink
Merge pull request #3 from bringg/add_config
Browse files Browse the repository at this point in the history
custom backend config
  • Loading branch information
Shareed2k authored Feb 23, 2021
2 parents 98cffed + 2a72146 commit 830879c
Show file tree
Hide file tree
Showing 17 changed files with 1,454 additions and 164 deletions.
86 changes: 86 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package cmd

import (
"context"

"github.com/pkg/errors"
"github.com/rclone/rclone/cmd"
"github.com/rclone/rclone/fs/config/flags"
"github.com/rclone/rclone/fs/rc"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/bringg/honey/pkg/config"
)

var (
configObscure bool
configNoObscure bool

configCommand = &cobra.Command{
Use: "config",
Short: `Enter an interactive configuration session.`,
Long: `Enter an interactive configuration session where you can setup new
backends and manage existing ones.
`,
Run: func(command *cobra.Command, args []string) {
cmd.CheckArgs(0, 0, command, args)
config.EditConfig(context.Background())
},
}

configCreateCommand = &cobra.Command{
Use: "create `name` `type` [`key` `value`]*",
Short: `Create a new backend with name, type and options.`,
Long: `
Create a new backend of ` + "`name`" + ` with ` + "`type`" + ` and options. The options
should be passed in pairs of ` + "`key` `value`" + `.
For example to make a k8s backend of name mybackend using auto config
you would do:
honey config create mybackend k8s context minikube
honey config create c1 aws region us-east-1
`,
RunE: func(command *cobra.Command, args []string) error {
cmd.CheckArgs(2, 256, command, args)
in, err := argsToMap(args[2:])
if err != nil {
return err
}

err = config.CreateBackend(context.Background(), args[0], args[1], in, configObscure, configNoObscure)
if err != nil {
return err
}

config.ShowBackend(args[0])

return nil
},
}
)

func init() {
for _, cmdFlags := range []*pflag.FlagSet{configCreateCommand.Flags() /* , configUpdateCommand.Flags() */} {
flags.BoolVarP(cmdFlags, &configObscure, "obscure", "", false, "Force any passwords to be obscured.")
flags.BoolVarP(cmdFlags, &configNoObscure, "no-obscure", "", false, "Force any passwords not to be obscured.")
}
}

// This takes a list of arguments in key value key value form and
// converts it into a map
func argsToMap(args []string) (out rc.Params, err error) {
if len(args)%2 != 0 {
return nil, errors.New("found key without value")
}

out = rc.Params{}
// Set the config
for i := 0; i < len(args); i += 2 {
out[args[i]] = args[i+1]
}

return out, nil
}
94 changes: 27 additions & 67 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/fatih/color"
"github.com/pkg/errors"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/flags"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -20,6 +19,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
cmdutil "k8s.io/kubectl/pkg/cmd/util"

"github.com/bringg/honey/pkg/config/configflags"
"github.com/bringg/honey/pkg/place"
"github.com/bringg/honey/pkg/place/operations"
)
Expand All @@ -41,28 +41,22 @@ Date: %s
`

var (
version = "development"
commit = "development"
builtBy = "shareed2k"
date = time.Now().String()
banner = fmt.Sprintf(color.GreenString(bannerTmp)+"\n", builtBy, version, commit, date)

verbose int
quiet bool
cfgFile string
filter string
noCache bool
noColor bool
outFormat = "table"
backendsString string
backendFlags map[string]struct{}
version = "development"
commit = "development"
builtBy = "shareed2k"
filter = ""
date = time.Now().String()
banner = fmt.Sprintf(color.GreenString(bannerTmp)+"\n", builtBy, version, commit, date)
backendFlags map[string]struct{}
// to filter the flags with
flagsRe *regexp.Regexp

Root = &cobra.Command{
Use: "honey",
Short: "DevOps tool to help find an instance in sea of clouds",
Version: version,
Use: "honey filter",
SilenceUsage: true,
SilenceErrors: true,
Short: "DevOps tool to help find an instance in sea of clouds",
Version: version,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// Setup shell completion for the k8s-namespace flag
if err := cmd.RegisterFlagCompletionFunc("k8s-namespace", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down Expand Up @@ -124,18 +118,20 @@ var (
}

flagsRe = re
filter = args[0]
}

backends := fs.CommaSepList{}
if err := backends.Set(backendsString); err != nil {
ctx := context.TODO()
backends, err := place.GetConfig(ctx).Backends()
if err != nil {
return err
}

if len(backends) == 0 {
return errors.New("oops you must specify at least one backend")
}

return operations.Find(context.TODO(), backends, filter, noCache, outFormat, noColor)
return operations.Find(ctx, backends, filter)
},
}

Expand Down Expand Up @@ -211,52 +207,19 @@ func init() {
Root.SetUsageTemplate(banner + usageTemplate)
Root.SetHelpCommand(helpCommand)

Root.PersistentFlags().CountVarP(&verbose, "verbose", "v", "Print lots more stuff (repeat for more)")
Root.PersistentFlags().BoolVarP(&noColor, "no-color", "", noColor, "disable colorize the json for outputing to the screen")
Root.PersistentFlags().BoolVarP(&quiet, "quiet", "q", quiet, "Print as little stuff as possible")
Root.PersistentFlags().BoolVarP(&noCache, "no-cache", "", noCache, "no-cache will skip lookup in cache")
Root.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.honey.yaml)")
Root.PersistentFlags().StringVarP(&outFormat, "output", "o", outFormat, "")
Root.PersistentFlags().StringVarP(&filter, "filter", "f", filter, "")
Root.PersistentFlags().StringVarP(&backendsString, "backends", "b", backendsString, "")

Root.AddCommand(newCompletionCmd(os.Stdout))

Root.AddCommand(helpCommand)
Root.AddCommand(configCommand)
helpCommand.AddCommand(helpFlags)
helpCommand.AddCommand(helpBackends)
helpCommand.AddCommand(helpBackend)
configCommand.AddCommand(configCreateCommand)
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
setVerboseLogFlags()

// ctx := context.Background()
// ci := place.GetConfig(ctx)

/* if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Search config in home directory with name ".honey" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".honey")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
} */
// Finish parsing any command line flags
configflags.SetFlags()
}

// addBackendFlags creates flags for all the backend options
Expand Down Expand Up @@ -374,6 +337,11 @@ func quoteString(v interface{}) string {
}

func setupRootCommand() {
ci := place.GetConfig(context.Background())
configflags.AddFlags(ci, pflag.CommandLine)

flags.StringVarP(pflag.CommandLine, &filter, "filter", "f", "", "instance name filter")

cobra.AddTemplateFunc("showLocalFlags", func(cmd *cobra.Command) bool {
// Don't show local flags (which are the global ones on the root) on "honey" and
// "honey help" (which shows the global help)
Expand All @@ -400,14 +368,6 @@ func setupRootCommand() {
})
}

func setVerboseLogFlags() {
if verbose >= 2 {
log.SetLevel(log.DebugLevel)
} else if verbose >= 1 {
log.SetLevel(log.InfoLevel)
}
}

var usageTemplate = `Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ require (
github.com/smartystreets/assertions v1.0.0 // indirect
github.com/spf13/cobra v1.1.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.7.1
github.com/tidwall/gjson v1.6.8
github.com/tidwall/pretty v1.0.2
github.com/vmihailenco/msgpack/v5 v5.2.0
github.com/xujiajun/nutsdb v0.5.0
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
golang.org/x/text v0.3.4
google.golang.org/api v0.34.0
gopkg.in/yaml.v2 v2.4.0
gotest.tools/gotestsum v1.6.1
Expand Down
Loading

0 comments on commit 830879c

Please sign in to comment.