From 8c9ad7b818c0a7b1e39f8df1fabba243a0961c2d Mon Sep 17 00:00:00 2001 From: David Calavera Date: Wed, 2 Dec 2015 23:55:07 -0500 Subject: [PATCH] Implement docker commit with standalone client lib. Signed-off-by: David Calavera --- api/client/commit.go | 45 ++++++---------------- api/client/lib/container_commit.go | 62 ++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 34 deletions(-) create mode 100644 api/client/lib/container_commit.go diff --git a/api/client/commit.go b/api/client/commit.go index 36fcb3c77e..fb9cfe2820 100644 --- a/api/client/commit.go +++ b/api/client/commit.go @@ -1,18 +1,15 @@ package client import ( - "encoding/json" "errors" "fmt" - "net/url" "github.com/docker/distribution/reference" - "github.com/docker/docker/api/types" + "github.com/docker/docker/api/client/lib" Cli "github.com/docker/docker/cli" "github.com/docker/docker/opts" flag "github.com/docker/docker/pkg/mflag" "github.com/docker/docker/registry" - "github.com/docker/docker/runconfig" ) // CmdCommit creates a new image from a container's changes. @@ -59,42 +56,22 @@ func (cli *DockerCli) CmdCommit(args ...string) error { } } - v := url.Values{} - v.Set("container", name) - v.Set("repo", repositoryName) - v.Set("tag", tag) - v.Set("comment", *flComment) - v.Set("author", *flAuthor) - for _, change := range flChanges.GetAll() { - v.Add("changes", change) + options := lib.ContainerCommitOptions{ + ContainerID: name, + RepositoryName: repositoryName, + Tag: tag, + Comment: *flComment, + Author: *flAuthor, + Changes: flChanges.GetAll(), + Pause: *flPause, + JSONConfig: *flConfig, } - if *flPause != true { - v.Set("pause", "0") - } - - var ( - config *runconfig.Config - response types.ContainerCommitResponse - ) - - if *flConfig != "" { - config = &runconfig.Config{} - if err := json.Unmarshal([]byte(*flConfig), config); err != nil { - return err - } - } - serverResp, err := cli.call("POST", "/commit?"+v.Encode(), config, nil) + response, err := cli.client.ContainerCommit(options) if err != nil { return err } - defer serverResp.body.Close() - - if err := json.NewDecoder(serverResp.body).Decode(&response); err != nil { - return err - } - fmt.Fprintln(cli.out, response.ID) return nil } diff --git a/api/client/lib/container_commit.go b/api/client/lib/container_commit.go new file mode 100644 index 0000000000..9dc3440282 --- /dev/null +++ b/api/client/lib/container_commit.go @@ -0,0 +1,62 @@ +package lib + +import ( + "encoding/json" + "net/url" + + "github.com/docker/docker/api/types" + "github.com/docker/docker/runconfig" +) + +// ContainerCommitOptions hods parameters to commit changes into a container. +type ContainerCommitOptions struct { + ContainerID string + RepositoryName string + Tag string + Comment string + Author string + Changes []string + Pause bool + JSONConfig string +} + +// ContainerCommit applies changes into a container and creates a new tagged image. +func (cli *Client) ContainerCommit(options types.ContainerCommitOptions) (types.ContainerCommitResponse, error) { + query := url.Values{} + query.Set("container", options.ContainerID) + query.Set("repo", options.RepositoryName) + query.Set("tag", options.Tag) + query.Set("comment", options.Comment) + query.Set("author", options.Author) + for _, change := range options.Changes { + query.Add("changes", change) + } + if options.Pause != true { + query.Set("pause", "0") + } + + var ( + config *runconfig.Config + response types.ContainerCommitResponse + ) + + if options.JSONConfig != "" { + config = &runconfig.Config{} + if err := json.Unmarshal([]byte(options.JSONConfig), config); err != nil { + return response, err + } + } + + resp, err := cli.POST("/commit", query, config, nil) + if err != nil { + return response, err + } + + defer resp.body.Close() + + if err := json.NewDecoder(resp.body).Decode(&response); err != nil { + return response, err + } + + return response, nil +}