mirror of
https://github.com/docker/compose.git
synced 2025-07-23 21:54:40 +02:00
Refactor NewContext
This renames NewContext to NewSigContext and moves it to ./util/util.go avoiding the servers to import "github.com/docker/api/client" Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
parent
667d1fa37e
commit
b44547c71f
@ -29,28 +29,13 @@ package client
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"os"
|
|
||||||
"os/signal"
|
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
v1 "github.com/docker/api/backend/v1"
|
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/backoff"
|
"google.golang.org/grpc/backoff"
|
||||||
)
|
|
||||||
|
|
||||||
// NewContext is a context that is canceled when a signal is
|
v1 "github.com/docker/api/backend/v1"
|
||||||
// sent to the process
|
)
|
||||||
func NewContext() (context.Context, func()) {
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
s := make(chan os.Signal)
|
|
||||||
signal.Notify(s, syscall.SIGTERM, syscall.SIGINT)
|
|
||||||
go func() {
|
|
||||||
<-s
|
|
||||||
cancel()
|
|
||||||
}()
|
|
||||||
return ctx, cancel
|
|
||||||
}
|
|
||||||
|
|
||||||
// New returns a GRPC client
|
// New returns a GRPC client
|
||||||
func New(address string, timeout time.Duration) (*Client, error) {
|
func New(address string, timeout time.Duration) (*Client, error) {
|
||||||
|
@ -35,6 +35,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/docker/api/client"
|
"github.com/docker/api/client"
|
||||||
|
"github.com/docker/api/util"
|
||||||
|
|
||||||
"github.com/golang/protobuf/ptypes/empty"
|
"github.com/golang/protobuf/ptypes/empty"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
@ -45,7 +47,7 @@ var exampleCommand = cli.Command{
|
|||||||
Usage: "sample command using backend, to be removed later",
|
Usage: "sample command using backend, to be removed later",
|
||||||
Action: func(clix *cli.Context) error {
|
Action: func(clix *cli.Context) error {
|
||||||
// return information for the current context
|
// return information for the current context
|
||||||
ctx, cancel := client.NewContext()
|
ctx, cancel := util.NewSigContext()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// get our current context
|
// get our current context
|
||||||
|
@ -33,13 +33,14 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
v1 "github.com/docker/api/backend/v1"
|
|
||||||
"github.com/docker/api/client"
|
|
||||||
"github.com/docker/api/server"
|
|
||||||
"github.com/golang/protobuf/ptypes/empty"
|
"github.com/golang/protobuf/ptypes/empty"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
|
|
||||||
|
v1 "github.com/docker/api/backend/v1"
|
||||||
|
"github.com/docker/api/server"
|
||||||
|
apiUtil "github.com/docker/api/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -66,7 +67,7 @@ func main() {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
app.Action = func(clix *cli.Context) error {
|
app.Action = func(clix *cli.Context) error {
|
||||||
ctx, cancel := client.NewContext()
|
ctx, cancel := apiUtil.NewSigContext()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// create a new GRPC server with the provided server package
|
// create a new GRPC server with the provided server package
|
||||||
|
48
util/util.go
Normal file
48
util/util.go
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
Copyright (c) 2019 Docker Inc.
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person
|
||||||
|
obtaining a copy of this software and associated documentation
|
||||||
|
files (the "Software"), to deal in the Software without
|
||||||
|
restriction, including without limitation the rights to use, copy,
|
||||||
|
modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||||
|
of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be
|
||||||
|
included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED,
|
||||||
|
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
|
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||||
|
HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||||
|
DAMAGES OR OTHER LIABILITY,
|
||||||
|
WHETHER IN AN ACTION OF CONTRACT,
|
||||||
|
TORT OR OTHERWISE,
|
||||||
|
ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||||
|
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewSigContext is a context that is canceled when a signal is
|
||||||
|
// sent to the process
|
||||||
|
func NewSigContext() (context.Context, func()) {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
s := make(chan os.Signal)
|
||||||
|
signal.Notify(s, syscall.SIGTERM, syscall.SIGINT)
|
||||||
|
go func() {
|
||||||
|
<-s
|
||||||
|
cancel()
|
||||||
|
}()
|
||||||
|
return ctx, cancel
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user