generated from maragudk/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a new `middleware` package as well as `middleware.Flags`, which allows you to supply a callback function receiving a `flag.FlagSet` to define flags on. In order to make this work, middleware is now applied before route matching. Otherwise, middleware can't change the route matching, which is necessary, because flags are part of the route before parsing. I had to disable `Router.Scope` because I can't currently make it work with the middleware changes, and I'm prioritizing the flags feature. See #8. Fixes #4
- Loading branch information
1 parent
bd6db43
commit 39523fc
Showing
6 changed files
with
206 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Package middleware provides useful middleware for a [clir.Router]. | ||
package middleware | ||
|
||
import ( | ||
"flag" | ||
|
||
"maragu.dev/clir" | ||
) | ||
|
||
// Flags middleware allows you to set flags on a route. | ||
func Flags(cb func(fs *flag.FlagSet)) clir.Middleware { | ||
fs := flag.NewFlagSet("", flag.ContinueOnError) | ||
cb(fs) | ||
|
||
return func(next clir.Runner) clir.Runner { | ||
return clir.RunnerFunc(func(ctx clir.Context) error { | ||
if err := fs.Parse(ctx.Args); err != nil { | ||
return err | ||
} | ||
ctx.Args = fs.Args() | ||
return next.Run(ctx) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package middleware_test | ||
|
||
import ( | ||
"flag" | ||
"os" | ||
"testing" | ||
|
||
"maragu.dev/is" | ||
|
||
"maragu.dev/clir" | ||
"maragu.dev/clir/middleware" | ||
) | ||
|
||
func TestFlags(t *testing.T) { | ||
t.Run("can set flags on a root route", func(t *testing.T) { | ||
r := clir.NewRouter() | ||
|
||
var v *bool | ||
r.Use(middleware.Flags(func(fs *flag.FlagSet) { | ||
v = fs.Bool("v", false, "") | ||
})) | ||
|
||
var called bool | ||
r.RouteFunc("", func(ctx clir.Context) error { | ||
called = true | ||
return nil | ||
}) | ||
|
||
err := r.Run(clir.Context{ | ||
Args: []string{"-v"}, | ||
}) | ||
is.NotError(t, err) | ||
is.True(t, called) | ||
is.NotNil(t, v) | ||
is.True(t, *v) | ||
}) | ||
|
||
t.Run("can set flags on the root and subroutes", func(t *testing.T) { | ||
r := clir.NewRouter() | ||
|
||
var v *bool | ||
r.Use(middleware.Flags(func(fs *flag.FlagSet) { | ||
v = fs.Bool("v", false, "") | ||
})) | ||
|
||
var called bool | ||
var fancy *bool | ||
|
||
r.Branch("dance", func(r *clir.Router) { | ||
r.Use(middleware.Flags(func(fs *flag.FlagSet) { | ||
fancy = fs.Bool("fancypants", false, "") | ||
})) | ||
|
||
r.RouteFunc("", func(ctx clir.Context) error { | ||
called = true | ||
return nil | ||
}) | ||
}) | ||
|
||
err := r.Run(clir.Context{ | ||
Args: []string{"-v", "dance", "-fancypants"}, | ||
}) | ||
is.NotError(t, err) | ||
is.True(t, called) | ||
is.NotNil(t, v) | ||
is.True(t, *v) | ||
is.NotNil(t, fancy) | ||
is.True(t, *fancy) | ||
}) | ||
} | ||
|
||
func ExampleFlags() { | ||
r := clir.NewRouter() | ||
|
||
var v *bool | ||
r.Use(middleware.Flags(func(fs *flag.FlagSet) { | ||
v = fs.Bool("v", false, "verbose output") | ||
})) | ||
|
||
r.RouteFunc("", func(ctx clir.Context) error { | ||
if *v { | ||
ctx.Println("Hello!") | ||
} | ||
return nil | ||
}) | ||
|
||
_ = r.Run(clir.Context{ | ||
Args: []string{"-v"}, | ||
Out: os.Stdout, | ||
}) | ||
// Output: Hello! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters