-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Thulio Ferraz Assis <thulio@aspect.dev>
- Loading branch information
Showing
24 changed files
with
451 additions
and
194 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
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
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
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
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,30 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "interceptors", | ||
srcs = [ | ||
"run.go", | ||
"workspace.go", | ||
], | ||
importpath = "aspect.build/cli/pkg/interceptors", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/pathutils", | ||
"@com_github_spf13_cobra//:cobra", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "interceptors_test", | ||
srcs = [ | ||
"run_test.go", | ||
"workspace_test.go", | ||
], | ||
embed = [":interceptors"], | ||
deps = [ | ||
"//pkg/pathutils/mock", | ||
"@com_github_golang_mock//gomock", | ||
"@com_github_onsi_gomega//:gomega", | ||
"@com_github_spf13_cobra//:cobra", | ||
], | ||
) |
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,40 @@ | ||
/* | ||
Copyright © 2021 Aspect Build Systems Inc | ||
Not licensed for re-use. | ||
*/ | ||
|
||
package interceptors | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// RunEFn matches the cobra command.RunE signature. | ||
type RunEFn func(cmd *cobra.Command, args []string) error | ||
|
||
// RunEContextFn is the signature based on RunEFn that injects the context as | ||
// an argument. | ||
type RunEContextFn func(ctx context.Context, cmd *cobra.Command, args []string) error | ||
|
||
// Interceptor represents an interceptor in the CLI command chain. It's | ||
// represented as a function signature. | ||
type Interceptor func(ctx context.Context, cmd *cobra.Command, args []string, next RunEContextFn) error | ||
|
||
// Run returns a function that matches the cobra RunE signature. It assembles | ||
// the interceptors and main command to be run in the correct sequence. | ||
func Run(interceptors []Interceptor, fn RunEContextFn) RunEFn { | ||
return func(cmd *cobra.Command, args []string) error { | ||
current := fn | ||
for i := len(interceptors) - 1; i > 0; i-- { | ||
j := i | ||
next := current | ||
current = func(ctx context.Context, cmd *cobra.Command, args []string) error { | ||
return interceptors[j](ctx, cmd, args, next) | ||
} | ||
} | ||
return interceptors[0](cmd.Context(), cmd, args, current) | ||
} | ||
} |
Oops, something went wrong.