From 6d7ed03e47af6864a7b05ac75a38026a7a38717e Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 18 Dec 2022 21:58:56 -0500 Subject: [PATCH 1/2] Use `*mail.Address` instead of custom `*Author` type Supports #1586 --- app.go | 19 ++----------------- app_test.go | 13 +++++++------ docs_test.go | 3 ++- fish_test.go | 7 ++++--- godoc-current.txt | 12 +----------- testdata/godoc-v3.x.txt | 12 +----------- 6 files changed, 17 insertions(+), 49 deletions(-) diff --git a/app.go b/app.go index 98a629de27..37a374ccd9 100644 --- a/app.go +++ b/app.go @@ -5,6 +5,7 @@ import ( "flag" "fmt" "io" + "net/mail" "os" "path/filepath" "sort" @@ -79,7 +80,7 @@ type App struct { // Execute this function when an invalid flag is accessed from the context InvalidFlagAccessHandler InvalidFlagAccessFunc // List of all authors who contributed - Authors []*Author + Authors []*mail.Address // Copyright of the binary if any Copyright string // Reader reader to write input to (useful for tests) @@ -434,22 +435,6 @@ func runFlagActions(c *Context, fs []Flag) error { return nil } -// Author represents someone who has contributed to a cli project. -type Author struct { - Name string // The Authors name - Email string // The Authors email -} - -// String makes Author comply to the Stringer interface, to allow an easy print in the templating process -func (a *Author) String() string { - e := "" - if a.Email != "" { - e = " <" + a.Email + ">" - } - - return fmt.Sprintf("%v%v", a.Name, e) -} - func checkStringSliceIncludes(want string, sSlice []string) bool { found := false for _, s := range sSlice { diff --git a/app_test.go b/app_test.go index a720bdc59e..ff58b55003 100644 --- a/app_test.go +++ b/app_test.go @@ -6,6 +6,7 @@ import ( "flag" "fmt" "io" + "net/mail" "os" "reflect" "strconv" @@ -45,7 +46,7 @@ func ExampleApp_Run() { return nil }, UsageText: "app [first_arg] [second_arg]", - Authors: []*Author{{Name: "Oliver Allen", Email: "oliver@toyshop.example.com"}}, + Authors: []*mail.Address{{Name: "Oliver Allen", Address: "oliver@toyshop.example.com"}}, } app.Run(os.Args) @@ -100,9 +101,9 @@ func ExampleApp_Run_appHelp() { Name: "greet", Version: "0.1.0", Description: "This is how we describe greet the app", - Authors: []*Author{ - {Name: "Harrison", Email: "harrison@lolwut.com"}, - {Name: "Oliver Allen", Email: "oliver@toyshop.com"}, + Authors: []*mail.Address{ + {Name: "Harrison", Address: "harrison@lolwut.com"}, + {Name: "Oliver Allen", Address: "oliver@toyshop.com"}, }, Flags: []Flag{ &StringFlag{Name: "name", Value: "bob", Usage: "a name to say"}, @@ -135,8 +136,8 @@ func ExampleApp_Run_appHelp() { // This is how we describe greet the app // // AUTHORS: - // Harrison - // Oliver Allen + // "Harrison" + // "Oliver Allen" // // COMMANDS: // describeit, d use it to see a description diff --git a/docs_test.go b/docs_test.go index 12d5d3c8b1..f3d60d13ac 100644 --- a/docs_test.go +++ b/docs_test.go @@ -5,6 +5,7 @@ package cli import ( "errors" + "net/mail" "testing" ) @@ -49,7 +50,7 @@ func TestToMarkdownNoCommands(t *testing.T) { func TestToMarkdownNoAuthors(t *testing.T) { // Given app := testApp() - app.Authors = []*Author{} + app.Authors = []*mail.Address{} // When res, err := app.ToMarkdown() diff --git a/fish_test.go b/fish_test.go index 10a0a169c1..d42215d2f0 100644 --- a/fish_test.go +++ b/fish_test.go @@ -2,6 +2,7 @@ package cli import ( "bytes" + "net/mail" "os" "testing" ) @@ -127,9 +128,9 @@ Should be a part of the same code block app.UsageText = "app [first_arg] [second_arg]" app.Description = `Description of the application.` app.Usage = "Some app" - app.Authors = []*Author{ - {Name: "Harrison", Email: "harrison@lolwut.com"}, - {Name: "Oliver Allen", Email: "oliver@toyshop.com"}, + app.Authors = []*mail.Address{ + {Name: "Harrison", Address: "harrison@lolwut.com"}, + {Name: "Oliver Allen", Address: "oliver@toyshop.com"}, } return app } diff --git a/godoc-current.txt b/godoc-current.txt index 43add568c4..712638554a 100644 --- a/godoc-current.txt +++ b/godoc-current.txt @@ -294,7 +294,7 @@ type App struct { // Execute this function when an invalid flag is accessed from the context InvalidFlagAccessHandler InvalidFlagAccessFunc // List of all authors who contributed - Authors []*Author + Authors []*mail.Address // Copyright of the binary if any Copyright string // Reader reader to write input to (useful for tests) @@ -399,16 +399,6 @@ type Args interface { Slice() []string } -type Author struct { - Name string // The Authors name - Email string // The Authors email -} - Author represents someone who has contributed to a cli project. - -func (a *Author) String() string - String makes Author comply to the Stringer interface, to allow an easy print - in the templating process - type BeforeFunc func(*Context) error BeforeFunc is an action to execute before any subcommands are run, but after the context is ready if a non-nil error is returned, no subcommands are run diff --git a/testdata/godoc-v3.x.txt b/testdata/godoc-v3.x.txt index 43add568c4..712638554a 100644 --- a/testdata/godoc-v3.x.txt +++ b/testdata/godoc-v3.x.txt @@ -294,7 +294,7 @@ type App struct { // Execute this function when an invalid flag is accessed from the context InvalidFlagAccessHandler InvalidFlagAccessFunc // List of all authors who contributed - Authors []*Author + Authors []*mail.Address // Copyright of the binary if any Copyright string // Reader reader to write input to (useful for tests) @@ -399,16 +399,6 @@ type Args interface { Slice() []string } -type Author struct { - Name string // The Authors name - Email string // The Authors email -} - Author represents someone who has contributed to a cli project. - -func (a *Author) String() string - String makes Author comply to the Stringer interface, to allow an easy print - in the templating process - type BeforeFunc func(*Context) error BeforeFunc is an action to execute before any subcommands are run, but after the context is ready if a non-nil error is returned, no subcommands are run From cfd0e7460339b9223f719af844da8c7c0581c667 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 1 Jan 2023 17:31:44 -0500 Subject: [PATCH 2/2] Switch Authors to []any and updated tests to use both strings and *mail.Address --- app.go | 5 ++--- app_test.go | 12 ++++++------ docs_test.go | 3 +-- fish_test.go | 6 +++--- godoc-current.txt | 4 ++-- testdata/godoc-v3.x.txt | 4 ++-- 6 files changed, 16 insertions(+), 18 deletions(-) diff --git a/app.go b/app.go index 37a374ccd9..60eeb7269d 100644 --- a/app.go +++ b/app.go @@ -5,7 +5,6 @@ import ( "flag" "fmt" "io" - "net/mail" "os" "path/filepath" "sort" @@ -79,8 +78,8 @@ type App struct { OnUsageError OnUsageErrorFunc // Execute this function when an invalid flag is accessed from the context InvalidFlagAccessHandler InvalidFlagAccessFunc - // List of all authors who contributed - Authors []*mail.Address + // List of all authors who contributed (string or fmt.Stringer) + Authors []any // TODO: ~string | fmt.Stringer when interface unions are available // Copyright of the binary if any Copyright string // Reader reader to write input to (useful for tests) diff --git a/app_test.go b/app_test.go index ff58b55003..00e980d29c 100644 --- a/app_test.go +++ b/app_test.go @@ -46,7 +46,7 @@ func ExampleApp_Run() { return nil }, UsageText: "app [first_arg] [second_arg]", - Authors: []*mail.Address{{Name: "Oliver Allen", Address: "oliver@toyshop.example.com"}}, + Authors: []any{&mail.Address{Name: "Oliver Allen", Address: "oliver@toyshop.example.com"}, "gruffalo@soup-world.example.org"}, } app.Run(os.Args) @@ -101,9 +101,9 @@ func ExampleApp_Run_appHelp() { Name: "greet", Version: "0.1.0", Description: "This is how we describe greet the app", - Authors: []*mail.Address{ - {Name: "Harrison", Address: "harrison@lolwut.com"}, - {Name: "Oliver Allen", Address: "oliver@toyshop.com"}, + Authors: []any{ + &mail.Address{Name: "Harrison", Address: "harrison@lolwut.example.com"}, + "Oliver Allen ", }, Flags: []Flag{ &StringFlag{Name: "name", Value: "bob", Usage: "a name to say"}, @@ -136,8 +136,8 @@ func ExampleApp_Run_appHelp() { // This is how we describe greet the app // // AUTHORS: - // "Harrison" - // "Oliver Allen" + // "Harrison" + // Oliver Allen // // COMMANDS: // describeit, d use it to see a description diff --git a/docs_test.go b/docs_test.go index f3d60d13ac..e4ae2fe7b4 100644 --- a/docs_test.go +++ b/docs_test.go @@ -5,7 +5,6 @@ package cli import ( "errors" - "net/mail" "testing" ) @@ -50,7 +49,7 @@ func TestToMarkdownNoCommands(t *testing.T) { func TestToMarkdownNoAuthors(t *testing.T) { // Given app := testApp() - app.Authors = []*mail.Address{} + app.Authors = []any{} // When res, err := app.ToMarkdown() diff --git a/fish_test.go b/fish_test.go index d42215d2f0..de75939614 100644 --- a/fish_test.go +++ b/fish_test.go @@ -128,9 +128,9 @@ Should be a part of the same code block app.UsageText = "app [first_arg] [second_arg]" app.Description = `Description of the application.` app.Usage = "Some app" - app.Authors = []*mail.Address{ - {Name: "Harrison", Address: "harrison@lolwut.com"}, - {Name: "Oliver Allen", Address: "oliver@toyshop.com"}, + app.Authors = []any{ + "Harrison ", + &mail.Address{Name: "Oliver Allen", Address: "oliver@toyshop.com"}, } return app } diff --git a/godoc-current.txt b/godoc-current.txt index 712638554a..91a4c9473e 100644 --- a/godoc-current.txt +++ b/godoc-current.txt @@ -293,8 +293,8 @@ type App struct { OnUsageError OnUsageErrorFunc // Execute this function when an invalid flag is accessed from the context InvalidFlagAccessHandler InvalidFlagAccessFunc - // List of all authors who contributed - Authors []*mail.Address + // List of all authors who contributed (string or fmt.Stringer) + Authors []any // TODO: ~string | fmt.Stringer when interface unions are available // Copyright of the binary if any Copyright string // Reader reader to write input to (useful for tests) diff --git a/testdata/godoc-v3.x.txt b/testdata/godoc-v3.x.txt index 712638554a..91a4c9473e 100644 --- a/testdata/godoc-v3.x.txt +++ b/testdata/godoc-v3.x.txt @@ -293,8 +293,8 @@ type App struct { OnUsageError OnUsageErrorFunc // Execute this function when an invalid flag is accessed from the context InvalidFlagAccessHandler InvalidFlagAccessFunc - // List of all authors who contributed - Authors []*mail.Address + // List of all authors who contributed (string or fmt.Stringer) + Authors []any // TODO: ~string | fmt.Stringer when interface unions are available // Copyright of the binary if any Copyright string // Reader reader to write input to (useful for tests)