cmd: don't alias context package, and use cliContext for cli.Context

Unfortunately, this is a rather large diff, but perhaps worth a one-time
"rip off the bandaid" for v2. This patch removes the use of "gocontext"
as alias for stdLib's "context", and uses "cliContext" for uses of
cli.context.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2024-06-20 02:15:13 +02:00
parent 741c4bde51
commit dd0542f7c1
65 changed files with 754 additions and 755 deletions

View File

@@ -17,7 +17,7 @@
package command
import (
gocontext "context"
"context"
"os"
"path/filepath"
@@ -33,7 +33,7 @@ import (
"github.com/urfave/cli/v2"
)
func outputConfig(ctx gocontext.Context, config *srvconfig.Config) error {
func outputConfig(ctx context.Context, config *srvconfig.Config) error {
plugins, err := server.LoadPlugins(ctx, config)
if err != nil {
return err
@@ -86,17 +86,17 @@ var configCommand = &cli.Command{
{
Name: "default",
Usage: "See the output of the default config",
Action: func(context *cli.Context) error {
return outputConfig(gocontext.Background(), defaultConfig())
Action: func(cliContext *cli.Context) error {
return outputConfig(context.Background(), defaultConfig())
},
},
{
Name: "dump",
Usage: "See the output of the final main config with imported in subconfig files",
Action: func(context *cli.Context) error {
Action: func(cliContext *cli.Context) error {
config := defaultConfig()
ctx := gocontext.Background()
if err := srvconfig.LoadConfig(ctx, context.String("config"), config); err != nil && !os.IsNotExist(err) {
ctx := context.Background()
if err := srvconfig.LoadConfig(ctx, cliContext.String("config"), config); err != nil && !os.IsNotExist(err) {
return err
}
@@ -106,10 +106,10 @@ var configCommand = &cli.Command{
{
Name: "migrate",
Usage: "Migrate the current configuration file to the latest version (does not migrate subconfig files)",
Action: func(context *cli.Context) error {
Action: func(cliContext *cli.Context) error {
config := defaultConfig()
ctx := gocontext.Background()
if err := srvconfig.LoadConfig(ctx, context.String("config"), config); err != nil && !os.IsNotExist(err) {
ctx := context.Background()
if err := srvconfig.LoadConfig(ctx, cliContext.String("config"), config); err != nil && !os.IsNotExist(err) {
return err
}

View File

@@ -17,7 +17,7 @@
package command
import (
gocontext "context"
"context"
"fmt"
"io"
"net"
@@ -54,8 +54,8 @@ func init() {
// Discard grpc logs so that they don't mess with our stdio
grpclog.SetLoggerV2(grpclog.NewLoggerV2(io.Discard, io.Discard, io.Discard))
cli.VersionPrinter = func(c *cli.Context) {
fmt.Println(c.App.Name, version.Package, c.App.Version, version.Revision)
cli.VersionPrinter = func(cliContext *cli.Context) {
fmt.Println(cliContext.App.Name, version.Package, cliContext.App.Version, version.Revision)
}
cli.VersionFlag = &cli.BoolFlag{
Name: "version",
@@ -118,12 +118,12 @@ can be used and modified as necessary as a custom configuration.`
publishCommand,
ociHook,
}
app.Action = func(context *cli.Context) error {
app.Action = func(cliContext *cli.Context) error {
var (
start = time.Now()
signals = make(chan os.Signal, 2048)
serverC = make(chan *server.Server, 1)
ctx, cancel = gocontext.WithCancel(gocontext.Background())
ctx, cancel = context.WithCancel(context.Background())
config = defaultConfig()
)
@@ -131,16 +131,16 @@ can be used and modified as necessary as a custom configuration.`
// Only try to load the config if it either exists, or the user explicitly
// told us to load this path.
configPath := context.String("config")
configPath := cliContext.String("config")
_, err := os.Stat(configPath)
if !os.IsNotExist(err) || context.IsSet("config") {
if !os.IsNotExist(err) || cliContext.IsSet("config") {
if err := srvconfig.LoadConfig(ctx, configPath, config); err != nil {
return err
}
}
// Apply flags to the config
if err := applyFlags(context, config); err != nil {
if err := applyFlags(cliContext, config); err != nil {
return err
}
@@ -303,7 +303,7 @@ can be used and modified as necessary as a custom configuration.`
return app
}
func serve(ctx gocontext.Context, l net.Listener, serveFunc func(net.Listener) error) {
func serve(ctx context.Context, l net.Listener, serveFunc func(net.Listener) error) {
path := l.Addr().String()
log.G(ctx).WithField("address", path).Info("serving...")
go func() {
@@ -314,10 +314,10 @@ func serve(ctx gocontext.Context, l net.Listener, serveFunc func(net.Listener) e
}()
}
func applyFlags(context *cli.Context, config *srvconfig.Config) error {
func applyFlags(cliContext *cli.Context, config *srvconfig.Config) error {
// the order for config vs flag values is that flags will always override
// the config values if they are set
if err := setLogLevel(context, config); err != nil {
if err := setLogLevel(cliContext, config); err != nil {
return err
}
if err := setLogFormat(config); err != nil {
@@ -341,7 +341,7 @@ func applyFlags(context *cli.Context, config *srvconfig.Config) error {
d: &config.GRPC.Address,
},
} {
if s := context.String(v.name); s != "" {
if s := cliContext.String(v.name); s != "" {
*v.d = s
if v.name == "root" || v.name == "state" {
absPath, err := filepath.Abs(s)
@@ -353,13 +353,13 @@ func applyFlags(context *cli.Context, config *srvconfig.Config) error {
}
}
applyPlatformFlags(context)
applyPlatformFlags(cliContext)
return nil
}
func setLogLevel(context *cli.Context, config *srvconfig.Config) error {
l := context.String("log-level")
func setLogLevel(cliContext *cli.Context, config *srvconfig.Config) error {
l := cliContext.String("log-level")
if l == "" {
l = config.Debug.Level
}

View File

@@ -33,7 +33,7 @@ import (
var ociHook = &cli.Command{
Name: "oci-hook",
Usage: "Provides a base for OCI runtime hooks to allow arguments to be injected.",
Action: func(context *cli.Context) error {
Action: func(cliContext *cli.Context) error {
state, err := loadHookState(os.Stdin)
if err != nil {
return err
@@ -45,7 +45,7 @@ var ociHook = &cli.Command{
}
var (
ctx = newTemplateContext(state, spec)
args = context.Args().Slice()
args = cliContext.Args().Slice()
env = os.Environ()
)
if err := newList(&args).render(ctx); err != nil {

View File

@@ -17,7 +17,7 @@
package command
import (
gocontext "context"
"context"
"fmt"
"io"
"net"
@@ -49,9 +49,9 @@ var publishCommand = &cli.Command{
Usage: "Topic of the event",
},
},
Action: func(context *cli.Context) error {
ctx := namespaces.WithNamespace(gocontext.Background(), context.String("namespace"))
topic := context.String("topic")
Action: func(cliContext *cli.Context) error {
ctx := namespaces.WithNamespace(context.Background(), cliContext.String("namespace"))
topic := cliContext.String("topic")
if topic == "" {
return fmt.Errorf("topic required to publish event: %w", errdefs.ErrInvalidArgument)
}
@@ -59,7 +59,7 @@ var publishCommand = &cli.Command{
if err != nil {
return err
}
client, err := connectEvents(context.String("address"))
client, err := connectEvents(cliContext.String("address"))
if err != nil {
return err
}
@@ -93,7 +93,7 @@ func connectEvents(address string) (eventsapi.EventsClient, error) {
return eventsapi.NewEventsClient(conn), nil
}
func connect(address string, d func(gocontext.Context, string) (net.Conn, error)) (*grpc.ClientConn, error) {
func connect(address string, d func(context.Context, string) (net.Conn, error)) (*grpc.ClientConn, error) {
backoffConfig := backoff.DefaultConfig
backoffConfig.MaxDelay = 3 * time.Second
connParams := grpc.ConnectParams{
@@ -106,7 +106,7 @@ func connect(address string, d func(gocontext.Context, string) (net.Conn, error)
grpc.FailOnNonTempDialError(true),
grpc.WithConnectParams(connParams),
}
ctx, cancel := gocontext.WithTimeout(gocontext.Background(), 2*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
conn, err := grpc.DialContext(ctx, dialer.DialAddress(address), gopts...)
if err != nil {

View File

@@ -30,7 +30,7 @@ func serviceFlags() []cli.Flag {
}
// applyPlatformFlags applies platform-specific flags.
func applyPlatformFlags(context *cli.Context) {
func applyPlatformFlags(cliContext *cli.Context) {
}
// registerUnregisterService is only relevant on Windows.

View File

@@ -79,8 +79,8 @@ func serviceFlags() []cli.Flag {
}
// applyPlatformFlags applies platform-specific flags.
func applyPlatformFlags(context *cli.Context) {
serviceNameFlag = context.String("service-name")
func applyPlatformFlags(cliContext *cli.Context) {
serviceNameFlag = cliContext.String("service-name")
if serviceNameFlag == "" {
serviceNameFlag = defaultServiceName
}
@@ -101,9 +101,9 @@ func applyPlatformFlags(context *cli.Context) {
d: &runServiceFlag,
},
} {
*v.d = context.Bool(v.name)
*v.d = cliContext.Bool(v.name)
}
logFileFlag = context.String("log-file")
logFileFlag = cliContext.String("log-file")
}
type handler struct {