Implement docker commit with standalone client lib.

Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
David Calavera 2015-12-02 23:55:07 -05:00
parent 589df17a1a
commit 8c9ad7b818
2 changed files with 73 additions and 34 deletions

View File

@ -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
}

View File

@ -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
}