From 745eca4f7bea387079e45e1a2feb6950f86fce93 Mon Sep 17 00:00:00 2001 From: Drew Stinnett Date: Wed, 14 Feb 2024 18:04:03 -0500 Subject: [PATCH] Removing cobra --- README.md | 6 ++- cobra.go | 146 -------------------------------------------------- cobra_test.go | 94 -------------------------------- go.mod | 3 -- go.sum | 9 ---- 5 files changed, 5 insertions(+), 253 deletions(-) delete mode 100644 cobra.go delete mode 100644 cobra_test.go diff --git a/README.md b/README.md index 2685ae8..05a5684 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,11 @@ data out in a more arbitrary format. This uses the `text/template` package to parse each item in the return slice. See [the example here](_examples/gotemplate/main.go) for full details. -## Coming Soon/TODO +## Related Projects + +* [Gout-Cobra](https://github.com/drewstinnett/gout-cobra) - Configure a cobra.Command to output using Gout + +## Coming Soon? ### TSV (Tab Separated Values) diff --git a/cobra.go b/cobra.go deleted file mode 100644 index bc00fa1..0000000 --- a/cobra.go +++ /dev/null @@ -1,146 +0,0 @@ -package gout - -import ( - "fmt" - "strings" - - "github.com/drewstinnett/gout/v2/config" - "github.com/drewstinnett/gout/v2/formats/gotemplate" - "github.com/spf13/cobra" -) - -// CobraCmdConfig defines what fields the formatting values are stored in -type CobraConfig struct { - FormatField string - FormatDefault string - FormatHelp string - TemplateDefault string - TemplateHelp string -} - -type CobraOption func(*CobraConfig) - -func WithCobraFormatField(s string) CobraOption { - return func(c *CobraConfig) { - c.FormatField = s - } -} - -func WithCobraFormatDefault(s string) CobraOption { - return func(c *CobraConfig) { - c.FormatDefault = s - } -} - -func WithCobraTemplateDefault(s string) CobraOption { - return func(c *CobraConfig) { - c.TemplateDefault = s - } -} - -// WithCobraFormatHelp defines wht the help for the format itself should be -func WithCobraFormatHelp(s string) CobraOption { - return func(c *CobraConfig) { - c.FormatHelp = s - } -} - -// WithCobraTemplateHelp defines what the help for the format template should be -func WithCobraTemplateHelp(s string) CobraOption { - return func(c *CobraConfig) { - c.TemplateHelp = s - } -} - -// NewcobraCmdConfig generates a new CobraCmdConfig with some sane defaults -func NewCobraConfig(opts ...CobraOption) *CobraConfig { - // Set up a default config - cc := &CobraConfig{ - FormatField: "format", - FormatDefault: "yaml", - FormatHelp: "Format to use for output", - TemplateDefault: "{{ . }}", - TemplateHelp: "Template to use when using the gotemplate format", - } - - // Override that stuff - for _, opt := range opts { - opt(cc) - } - - return cc -} - -// BindCobraCmd creates a new set of flags for Cobra that can be used to -// configure Gout -func BindCobra(cmd *cobra.Command, config *CobraConfig) error { - if config == nil { - config = NewCobraConfig() - } - keys := make([]string, 0, len(BuiltInFormatters)) - for k := range BuiltInFormatters { - keys = append(keys, k) - } - help := config.FormatHelp + " (" + strings.Join(keys, "|") + ")" - cmd.PersistentFlags().String(config.FormatField, config.FormatDefault, help) - cmd.PersistentFlags().String(config.FormatField+"-template", config.TemplateDefault, config.TemplateHelp) - return nil -} - -// WithCobra sets up the the built-in Gout client using options from the cobra.Cmd -func WithCobra(cmd *cobra.Command, conf *CobraConfig) error { - g := GetGout() - err := ApplyCobra(g, cmd, conf) - if err != nil { - return err - } - return nil -} - -// ApplyCobra uses settings from cobra.Command to set up a given Gout instance -func ApplyCobra(g *Gout, cmd *cobra.Command, conf *CobraConfig) error { - if conf == nil { - conf = NewCobraConfig() - } - - var format string - var err error - format, err = cmd.Flags().GetString(conf.FormatField) - if err != nil { - return err - } - if format == "gotemplate" { - t, err := cmd.PersistentFlags().GetString(conf.FormatField + "-template") - if err != nil { - return err - } - g.SetFormatter(gotemplate.Formatter{ - Opts: config.FormatterOpts{ - "template": t, - }, - }) - } else { - if fr, ok := BuiltInFormatters[format]; ok { - g.SetFormatter(fr) - } else { - return fmt.Errorf("Could not find the format %v", format) - } - } - return nil -} - -// NewWithCobraCmd creates a pointer to a new writer with options from a cobra.Command -func NewWithCobraCmd(cmd *cobra.Command, conf *CobraConfig) (*Gout, error) { - // Default this writer to stdout - c, err := New() - if err != nil { - return nil, err - } - - err = ApplyCobra(c, cmd, conf) - if err != nil { - return nil, err - } - - return c, nil -} diff --git a/cobra_test.go b/cobra_test.go deleted file mode 100644 index d40d7be..0000000 --- a/cobra_test.go +++ /dev/null @@ -1,94 +0,0 @@ -package gout - -import ( - "testing" - - "github.com/spf13/cobra" - "github.com/stretchr/testify/require" -) - -/* -func TestNewWithCobraCmdFlag(t *testing.T) { - cmd := cobra.Command{} - cmd.Flags().String("format", "yaml", "The format") - c, err := NewWithCobraCmd(&cmd, nil) - require.NoError(t, err) - require.NotNil(t, c) -} -*/ - -func TestNewWithCobraCmdPersistentFlag(t *testing.T) { - cmd := cobra.Command{} - cmd.PersistentFlags().String("format", "yaml", "The format") - err := cmd.Execute() - require.NoError(t, err) - c, err := NewWithCobraCmd(&cmd, nil) - require.NoError(t, err) - require.NotNil(t, c) -} - -func TestNewWithCobraCmdPersistentFlagTemplate(t *testing.T) { - cmd := cobra.Command{} - cmd.PersistentFlags().String("format", "gotemplate", "The format") - cmd.PersistentFlags().String("format-template", "{{ . }}", "The format template") - err := cmd.Execute() - require.NoError(t, err) - c, err := NewWithCobraCmd(&cmd, nil) - require.NoError(t, err) - require.NotNil(t, c) -} - -func TestNewWithCobraCmdBadFormat(t *testing.T) { - cmd := cobra.Command{} - cmd.PersistentFlags().String("format", "not-exist", "The format") - err := cmd.Execute() - require.NoError(t, err) - c, err := NewWithCobraCmd(&cmd, nil) - require.EqualError(t, err, "Could not find the format not-exist") - require.Nil(t, c) -} - -func TestNewWithCobraCmdMissingFormat(t *testing.T) { - cmd := cobra.Command{} - c, err := NewWithCobraCmd(&cmd, nil) - require.EqualError(t, err, "flag accessed but not defined: format") - require.Nil(t, c) -} - -func TestBindCobraCmd(t *testing.T) { - cmd := cobra.Command{} - - err := BindCobra(&cmd, nil) - require.NoError(t, err) - err = cmd.Execute() - require.NoError(t, err) - - got, err := cmd.Flags().GetString("format") - require.NoError(t, err) - require.Equal(t, "yaml", got) -} - -func TestWithCobra(t *testing.T) { - cmd := cobra.Command{} - cmd.PersistentFlags().String("format", "yaml", "The format") - err := cmd.Execute() - require.NoError(t, err) - - err = WithCobra(&cmd, nil) - require.NoError(t, err) -} - -func TestCobraConfig(t *testing.T) { - cc := NewCobraConfig( - WithCobraFormatField("funky"), - WithCobraFormatDefault("toml"), - WithCobraFormatHelp("format-helper-stuff"), - WithCobraTemplateDefault("foo-template-default"), - WithCobraTemplateHelp("foo-template-help"), - ) - require.Equal(t, "funky", cc.FormatField) - require.Equal(t, "toml", cc.FormatDefault) - require.Equal(t, "format-helper-stuff", cc.FormatHelp) - require.Equal(t, "foo-template-default", cc.TemplateDefault) - require.Equal(t, "foo-template-help", cc.TemplateHelp) -} diff --git a/go.mod b/go.mod index a8f9a81..40a0989 100644 --- a/go.mod +++ b/go.mod @@ -5,14 +5,11 @@ go 1.19 require ( github.com/jszwec/csvutil v1.7.1 github.com/pelletier/go-toml/v2 v2.0.5 - github.com/spf13/cobra v1.5.0 github.com/stretchr/testify v1.8.0 gopkg.in/yaml.v3 v3.0.1 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect - github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/spf13/pflag v1.0.5 // indirect ) diff --git a/go.sum b/go.sum index 0ec8df2..d27f9c3 100644 --- a/go.sum +++ b/go.sum @@ -1,20 +1,12 @@ -github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= -github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/jszwec/csvutil v1.7.1 h1:btxPxFwms8lHMgl0OIgOQ4Tayfqo0xid0hGkq1kM510= github.com/jszwec/csvutil v1.7.1/go.mod h1:Rpu7Uu9giO9subDyMCIQfHVDuLrcaC36UA4YcJjGBkg= github.com/pelletier/go-toml/v2 v2.0.5 h1:ipoSadvV8oGUjnUbMub59IDPPwfxF694nG/jwbMiyQg= github.com/pelletier/go-toml/v2 v2.0.5/go.mod h1:OMHamSCAODeSsVrwwvcJOaoN0LIUIaFVNZzmWyNfXas= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU= -github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM= -github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= -github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= @@ -22,7 +14,6 @@ github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PK github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=