Add base cli and connection logic

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2020-04-06 11:54:01 -04:00
parent be8e73292c
commit 37591c714b
5 changed files with 232 additions and 21 deletions

View file

@ -27,21 +27,45 @@
package client
import "google.golang.org/grpc"
import (
"context"
"os"
"os/signal"
"syscall"
"time"
v1 "github.com/docker/api/backend/v1"
"google.golang.org/grpc"
)
// NewContext is a context that is canceled when a signal is
// 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
func New(address string) (*Client, error) {
conn, err := grpc.Dial(address, grpc.WithInsecure())
func New(address string, timeout time.Duration) (*Client, error) {
conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock(), grpc.WithTimeout(timeout))
if err != nil {
return nil, err
}
return &Client{
conn: conn,
conn: conn,
BackendClient: v1.NewBackendClient(conn),
}, nil
}
type Client struct {
conn *grpc.ClientConn
v1.BackendClient
}
func (c *Client) Close() error {