本文整理汇总了Golang中github.com/docker/docker/pkg/jsonmessage.DisplayJSONMessagesStream函数的典型用法代码示例。如果您正苦于以下问题:Golang DisplayJSONMessagesStream函数的具体用法?Golang DisplayJSONMessagesStream怎么用?Golang DisplayJSONMessagesStream使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了DisplayJSONMessagesStream函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: pullImage
func pullImage(
ctx context.Context, l *LocalCluster, ref string, options types.ImagePullOptions,
) error {
// HACK: on CircleCI, docker pulls the image on the first access from an
// acceptance test even though that image is already present. So we first
// check to see if our image is present in order to avoid this slowness.
if hasImage(ctx, l, ref) {
log.Infof(ctx, "ImagePull %s already exists", ref)
return nil
}
log.Infof(ctx, "ImagePull %s starting", ref)
defer log.Infof(ctx, "ImagePull %s complete", ref)
rc, err := l.client.ImagePull(ctx, ref, options)
if err != nil {
return err
}
defer rc.Close()
out := os.Stderr
outFd := out.Fd()
isTerminal := isatty.IsTerminal(outFd)
return jsonmessage.DisplayJSONMessagesStream(rc, out, outFd, isTerminal, nil)
}
示例2: runLoad
func runLoad(dockerCli *client.DockerCli, opts loadOptions) error {
var input io.Reader = dockerCli.In()
if opts.input != "" {
file, err := os.Open(opts.input)
if err != nil {
return err
}
defer file.Close()
input = file
}
if !dockerCli.IsTerminalOut() {
opts.quiet = true
}
response, err := dockerCli.Client().ImageLoad(context.Background(), input, opts.quiet)
if err != nil {
return err
}
defer response.Body.Close()
if response.Body != nil && response.JSON {
return jsonmessage.DisplayJSONMessagesStream(response.Body, dockerCli.Out(), dockerCli.OutFd(), dockerCli.IsTerminalOut(), nil)
}
_, err = io.Copy(dockerCli.Out(), response.Body)
return err
}
示例3: PullImage
// PullImage pulls docker image
func (c *DockerClient) PullImage(name string) error {
var (
image = imagename.NewFromString(name)
pipeReader, pipeWriter = io.Pipe()
fdOut, isTerminalOut = term.GetFdInfo(c.log.Out)
out = c.log.Out
errch = make(chan error, 1)
)
if !isTerminalOut {
out = c.log.Writer()
}
opts := docker.PullImageOptions{
Repository: image.NameWithRegistry(),
Registry: image.Registry,
Tag: image.GetTag(),
OutputStream: pipeWriter,
RawJSONStream: true,
}
c.log.Infof("| Pull image %s", image)
c.log.Debugf("Pull image %s with options: %# v", image, opts)
go func() {
errch <- jsonmessage.DisplayJSONMessagesStream(pipeReader, out, fdOut, isTerminalOut)
}()
if err := c.client.PullImage(opts, c.auth); err != nil {
return err
}
return <-errch
}
示例4: handleStreamResponse
func handleStreamResponse(resp *http.Response, streamOptions *streamOptions) error {
var err error
if !streamOptions.useJSONDecoder && resp.Header.Get("Content-Type") != "application/json" {
if streamOptions.setRawTerminal {
_, err = io.Copy(streamOptions.stdout, resp.Body)
} else {
_, err = stdcopy.StdCopy(streamOptions.stdout, streamOptions.stderr, resp.Body)
}
return err
}
// if we want to get raw json stream, just copy it back to output
// without decoding it
if streamOptions.rawJSONStream {
_, err = io.Copy(streamOptions.stdout, resp.Body)
return err
}
if st, ok := streamOptions.stdout.(interface {
io.Writer
FD() uintptr
IsTerminal() bool
}); ok {
err = jsonmessage.DisplayJSONMessagesToStream(resp.Body, st, nil)
} else {
err = jsonmessage.DisplayJSONMessagesStream(resp.Body, streamOptions.stdout, 0, false, nil)
}
return err
}
示例5: runDeploy
func runDeploy(cmd *Command, args []string) {
r, w := io.Pipe()
if len(args) < 1 {
printFatal("You must specify an image to deploy")
}
image := args[0]
message := getMessage()
form := &PostDeployForm{Image: image}
var endpoint string
appName, _ := app()
if appName != "" {
endpoint = fmt.Sprintf("/apps/%s/deploys", appName)
} else {
endpoint = "/deploys"
}
rh := heroku.RequestHeaders{CommitMessage: message}
go func() {
must(client.PostWithHeaders(w, endpoint, form, rh.Headers()))
must(w.Close())
}()
outFd, isTerminalOut := term.GetFdInfo(os.Stdout)
must(jsonmessage.DisplayJSONMessagesStream(r, os.Stdout, outFd, isTerminalOut))
}
示例6: CmdLoad
// CmdLoad loads an image from a tar archive.
//
// The tar archive is read from STDIN by default, or from a tar archive file.
//
// Usage: docker load [OPTIONS]
func (cli *DockerCli) CmdLoad(args ...string) error {
cmd := Cli.Subcmd("load", nil, Cli.DockerCommands["load"].Description, true)
infile := cmd.String([]string{"i", "-input"}, "", "Read from a tar archive file, instead of STDIN")
quiet := cmd.Bool([]string{"q", "-quiet"}, false, "Suppress the load output")
cmd.Require(flag.Exact, 0)
cmd.ParseFlags(args, true)
var input io.Reader = cli.in
if *infile != "" {
file, err := os.Open(*infile)
if err != nil {
return err
}
defer file.Close()
input = file
}
if !cli.isTerminalOut {
*quiet = true
}
response, err := cli.client.ImageLoad(context.Background(), input, *quiet)
if err != nil {
return err
}
defer response.Body.Close()
if response.JSON {
return jsonmessage.DisplayJSONMessagesStream(response.Body, cli.out, cli.outFd, cli.isTerminalOut, nil)
}
_, err = io.Copy(cli.out, response.Body)
return err
}
示例7: CmdPush
// CmdPush pushes an image or repository to the registry.
//
// Usage: docker push NAME[:TAG]
func (cli *DockerCli) CmdPush(args ...string) error {
cmd := Cli.Subcmd("push", []string{"NAME[:TAG]"}, Cli.DockerCommands["push"].Description, true)
addTrustedFlags(cmd, false)
cmd.Require(flag.Exact, 1)
cmd.ParseFlags(args, true)
ref, err := reference.ParseNamed(cmd.Arg(0))
if err != nil {
return err
}
// Resolve the Repository name from fqn to RepositoryInfo
repoInfo, err := registry.ParseRepositoryInfo(ref)
if err != nil {
return err
}
// Resolve the Auth config relevant for this server
authConfig := cli.resolveAuthConfig(repoInfo.Index)
requestPrivilege := cli.registryAuthenticationPrivilegedFunc(repoInfo.Index, "push")
if isTrusted() {
return cli.trustedPush(repoInfo, ref, authConfig, requestPrivilege)
}
responseBody, err := cli.imagePushPrivileged(authConfig, ref.String(), requestPrivilege)
if err != nil {
return err
}
defer responseBody.Close()
return jsonmessage.DisplayJSONMessagesStream(responseBody, cli.out, cli.outFd, cli.isTerminalOut, nil)
}
示例8: pullImage
func (cli *DockerCli) pullImage(image string, out io.Writer) error {
ref, err := reference.ParseNamed(image)
if err != nil {
return err
}
// Resolve the Repository name from fqn to RepositoryInfo
repoInfo, err := registry.ParseRepositoryInfo(ref)
if err != nil {
return err
}
authConfig := cli.resolveAuthConfig(repoInfo.Index)
encodedAuth, err := encodeAuthToBase64(authConfig)
if err != nil {
return err
}
options := types.ImageCreateOptions{
RegistryAuth: encodedAuth,
}
responseBody, err := cli.client.ImageCreate(context.Background(), image, options)
if err != nil {
return err
}
defer responseBody.Close()
return jsonmessage.DisplayJSONMessagesStream(responseBody, out, cli.outFd, cli.isTerminalOut, nil)
}
示例9: runPush
func runPush(dockerCli *client.DockerCli, remote string) error {
ref, err := reference.ParseNamed(remote)
if err != nil {
return err
}
// Resolve the Repository name from fqn to RepositoryInfo
repoInfo, err := registry.ParseRepositoryInfo(ref)
if err != nil {
return err
}
ctx := context.Background()
// Resolve the Auth config relevant for this server
authConfig := dockerCli.ResolveAuthConfig(ctx, repoInfo.Index)
requestPrivilege := dockerCli.RegistryAuthenticationPrivilegedFunc(repoInfo.Index, "push")
if client.IsTrusted() {
return dockerCli.TrustedPush(ctx, repoInfo, ref, authConfig, requestPrivilege)
}
responseBody, err := dockerCli.ImagePushPrivileged(ctx, authConfig, ref.String(), requestPrivilege)
if err != nil {
return err
}
defer responseBody.Close()
return jsonmessage.DisplayJSONMessagesStream(responseBody, dockerCli.Out(), dockerCli.OutFd(), dockerCli.IsTerminalOut(), nil)
}
示例10: CmdImport
// CmdImport creates an empty filesystem image, imports the contents of the tarball into the image, and optionally tags the image.
//
// The URL argument is the address of a tarball (.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz) file or a path to local file relative to docker client. If the URL is '-', then the tar file is read from STDIN.
//
// Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
func (cli *DockerCli) CmdImport(args ...string) error {
cmd := Cli.Subcmd("import", []string{"file|URL|- [REPOSITORY[:TAG]]"}, Cli.DockerCommands["import"].Description, true)
flChanges := opts.NewListOpts(nil)
cmd.Var(&flChanges, []string{"c", "-change"}, "Apply Dockerfile instruction to the created image")
message := cmd.String([]string{"m", "-message"}, "", "Set commit message for imported image")
cmd.Require(flag.Min, 1)
cmd.ParseFlags(args, true)
var (
in io.Reader
tag string
src = cmd.Arg(0)
srcName = src
repository = cmd.Arg(1)
changes = flChanges.GetAll()
)
if cmd.NArg() == 3 {
fmt.Fprintf(cli.err, "[DEPRECATED] The format 'file|URL|- [REPOSITORY [TAG]]' has been deprecated. Please use file|URL|- [REPOSITORY[:TAG]]\n")
tag = cmd.Arg(2)
}
if repository != "" {
//Check if the given image name can be resolved
if _, err := reference.ParseNamed(repository); err != nil {
return err
}
}
if src == "-" {
in = cli.in
} else if !urlutil.IsURL(src) {
srcName = "-"
file, err := os.Open(src)
if err != nil {
return err
}
defer file.Close()
in = file
}
options := types.ImageImportOptions{
Source: in,
SourceName: srcName,
RepositoryName: repository,
Message: *message,
Tag: tag,
Changes: changes,
}
responseBody, err := cli.client.ImageImport(options)
if err != nil {
return err
}
defer responseBody.Close()
return jsonmessage.DisplayJSONMessagesStream(responseBody, cli.out, cli.outFd, cli.isTerminalOut)
}
示例11: NewDockerJsonWriter
func NewDockerJsonWriter(under io.Writer) *DockerJsonWriter {
r, w := io.Pipe()
go func() {
err := jsonmessage.DisplayJSONMessagesStream(r, under, 1, true, nil)
log.Fatal(err)
}()
return &DockerJsonWriter{under, w}
}
示例12: pullImage
func pullImage(client client.APIClient, service *Service, image string) error {
distributionRef, err := reference.ParseNamed(image)
if err != nil {
return err
}
switch distributionRef.(type) {
case reference.Canonical:
case reference.NamedTagged:
default:
distributionRef, err = reference.WithTag(distributionRef, "latest")
if err != nil {
return err
}
}
repoInfo, err := registry.ParseRepositoryInfo(distributionRef)
if err != nil {
return err
}
authConfig := types.AuthConfig{}
if service.context.ConfigFile != nil && repoInfo != nil && repoInfo.Index != nil {
authConfig = registry.ResolveAuthConfig(service.context.ConfigFile.AuthConfigs, repoInfo.Index)
}
encodedAuth, err := encodeAuthToBase64(authConfig)
if err != nil {
return err
}
options := types.ImagePullOptions{
RegistryAuth: encodedAuth,
}
responseBody, err := client.ImagePull(context.Background(), distributionRef.String(), options)
if err != nil {
logrus.Errorf("Failed to pull image %s: %v", image, err)
return err
}
defer responseBody.Close()
var writeBuff io.Writer = os.Stdout
outFd, isTerminalOut := term.GetFdInfo(os.Stdout)
err = jsonmessage.DisplayJSONMessagesStream(responseBody, writeBuff, outFd, isTerminalOut, nil)
if err != nil {
if jerr, ok := err.(*jsonmessage.JSONError); ok {
// If no error code is set, default to 1
if jerr.Code == 0 {
jerr.Code = 1
}
fmt.Fprintf(os.Stderr, "%s", writeBuff)
return fmt.Errorf("Status: %s, Code: %d", jerr.Message, jerr.Code)
}
}
return err
}
示例13: PullDockerImage
// PullDockerImage pulls an image and streams to a logger respecting terminal features
func PullDockerImage(client *docker.Client, image *imagename.ImageName, auth *docker.AuthConfigurations) (*docker.Image, error) {
if image.Storage == imagename.StorageS3 {
s3storage := s3.New(client, os.TempDir())
if err := s3storage.Pull(image.String()); err != nil {
return nil, err
}
} else {
pipeReader, pipeWriter := io.Pipe()
pullOpts := docker.PullImageOptions{
Repository: image.NameWithRegistry(),
Registry: image.Registry,
Tag: image.Tag,
OutputStream: pipeWriter,
RawJSONStream: true,
}
repoAuth, err := dockerclient.GetAuthForRegistry(auth, image)
if err != nil {
return nil, fmt.Errorf("Failed to authenticate registry %s, error: %s", image.Registry, err)
}
errch := make(chan error, 1)
go func() {
err := client.PullImage(pullOpts, repoAuth)
if err := pipeWriter.Close(); err != nil {
log.Errorf("Failed to close pull image stream for %s, error: %s", image, err)
}
errch <- err
}()
def := log.StandardLogger()
fd, isTerminal := term.GetFdInfo(def.Out)
out := def.Out
if !isTerminal {
out = def.Writer()
}
if err := jsonmessage.DisplayJSONMessagesStream(pipeReader, out, fd, isTerminal); err != nil {
return nil, fmt.Errorf("Failed to process json stream for image: %s, error: %s", image, err)
}
if err := <-errch; err != nil {
return nil, fmt.Errorf("Failed to pull image %s, error: %s", image, err)
}
}
img, err := client.InspectImage(image.String())
if err != nil {
return nil, fmt.Errorf("Failed to inspect image %s after pull, error: %s", image, err)
}
return img, nil
}
示例14: ensureImage
func ensureImage(cli DockerClient, image string) (string, error) {
ctx := context.Background()
info, _, err := cli.ImageInspectWithRaw(ctx, image, false)
if err == nil {
logrus.Debugf("Image found locally %s", image)
return info.ID, nil
}
if !client.IsErrImageNotFound(err) {
logrus.Errorf("Error inspecting image %q: %v", image, err)
return "", err
}
// Image must be tagged reference if it does not exist
ref, err := reference.Parse(image)
if err != nil {
logrus.Errorf("Image is not valid reference %q: %v", image, err)
return "", err
}
tagged, ok := ref.(reference.NamedTagged)
if !ok {
logrus.Errorf("Tagged reference required %q", image)
return "", errors.New("invalid reference, tag needed")
}
pullStart := time.Now()
pullOptions := types.ImagePullOptions{
PrivilegeFunc: registryAuthNotSupported,
}
resp, err := cli.ImagePull(ctx, tagged.String(), pullOptions)
if err != nil {
logrus.Errorf("Error pulling image %q: %v", tagged.String(), err)
return "", err
}
defer resp.Close()
outFd, isTerminalOut := term.GetFdInfo(os.Stdout)
if err = jsonmessage.DisplayJSONMessagesStream(resp, os.Stdout, outFd, isTerminalOut, nil); err != nil {
logrus.Errorf("Error copying pull output: %v", err)
return "", err
}
// TODO: Get pulled digest
logFields := logrus.Fields{
timerKey: time.Since(pullStart),
"image": tagged.String(),
}
logrus.WithFields(logFields).Info("image pulled")
info, _, err = cli.ImageInspectWithRaw(ctx, tagged.String(), false)
if err != nil {
return "", nil
}
return info.ID, nil
}
示例15: Write
// Write decodes the jsonmessage stream in the bytes, and writes the decoded
// plain text to the underlying io.Writer.
func (w *DecodedJSONMessageWriter) Write(b []byte) (int, error) {
err := jsonmessage.DisplayJSONMessagesStream(bytes.NewReader(b), w.w, w.fd, false)
if err != nil {
if err, ok := err.(*jsonmessage.JSONError); ok {
w.err = err
return len(b), nil
}
}
return len(b), err
}