func (cli *DockerCli) ExampleCommand(args ...string) error { if len(args) < 1 { return cli.Errorf("No arguments provided") } cmd := exec.Command(args[0], args[1:]...) if err := cmd.Run(); err != nil { return cli.Err(err) } return nil }
func (cli *DockerCli) BuildCommand(ctx context.Context, stdout, stderr io.Writer, opts types.ImageBuildOptions) error { client, err := client.NewEnvClient() if err != nil { return cli.Err(err) } buildContext, err := archive.TarWithOptions(".", &archive.TarOptions{}) if err != nil { return cli.Err(err) } response, err := client.ImageBuild(ctx, buildContext, opts) if err != nil { return cli.Err(err) } defer response.Body.Close() if err := jsonmessage.DisplayJSONMessagesStream(response.Body, stdout, stdout.Fd(), false, nil); err != nil { return cli.Err(err) } return nil }This code example shows the BuildCommand method, which takes a context, standard output and error writers, and image build options as arguments. It uses the Docker client library to build a Docker image from the current directory. If any errors occur during the process, they are returned using the Err method. The method also outputs JSON messages using the jsonmessage library.