These functions and types are shallow wrappers around the context
store and were intended for internal use as implementation for the
CLI itself.
They were exported in 3126920af1 to be
used by plugins and Docker Desktop. However, there's currently no public
uses of this, and Docker Desktop does not use these functions.
This patch deprecates the exported functions as they were meant to be
implementation specific for the CLI. If there's a need to provide
utilities for manipulating the context-store other than through the
CLI itself, we can consider creating an SDK for that purpose.
This deprecates:
- `RunCreate` and `CreateOptions`
- `RunExport` and `ExportOptions`
- `RunImport`
- `RunRemove` and `RemoveOptions`
- `RunUpdate` and `UpdateOptions`
- `RunUse`
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
87 lines
2.2 KiB
Go
87 lines
2.2 KiB
Go
package context
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/docker/cli/cli"
|
|
"github.com/docker/cli/cli/command"
|
|
"github.com/docker/cli/cli/context/store"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// ExportOptions are the options used for exporting a context
|
|
//
|
|
// Deprecated: this type was for internal use and will be removed in the next release.
|
|
type ExportOptions struct {
|
|
ContextName string
|
|
Dest string
|
|
}
|
|
|
|
func newExportCommand(dockerCLI command.Cli) *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "export [OPTIONS] CONTEXT [FILE|-]",
|
|
Short: "Export a context to a tar archive FILE or a tar stream on STDOUT.",
|
|
Args: cli.RequiresRangeArgs(1, 2),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
contextName := args[0]
|
|
var dest string
|
|
if len(args) == 2 {
|
|
dest = args[1]
|
|
} else {
|
|
dest = contextName + ".dockercontext"
|
|
}
|
|
return runExport(dockerCLI, contextName, dest)
|
|
},
|
|
ValidArgsFunction: completeContextNames(dockerCLI, 1, true),
|
|
}
|
|
}
|
|
|
|
func writeTo(dockerCli command.Cli, reader io.Reader, dest string) error {
|
|
var writer io.Writer
|
|
var printDest bool
|
|
if dest == "-" {
|
|
if dockerCli.Out().IsTerminal() {
|
|
return errors.New("cowardly refusing to export to a terminal, specify a file path")
|
|
}
|
|
writer = dockerCli.Out()
|
|
} else {
|
|
f, err := os.OpenFile(dest, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0o600)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
writer = f
|
|
printDest = true
|
|
}
|
|
if _, err := io.Copy(writer, reader); err != nil {
|
|
return err
|
|
}
|
|
if printDest {
|
|
fmt.Fprintf(dockerCli.Err(), "Written file %q\n", dest)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// RunExport exports a Docker context
|
|
//
|
|
// Deprecated: this function was for internal use and will be removed in the next release.
|
|
func RunExport(dockerCli command.Cli, opts *ExportOptions) error {
|
|
if opts == nil {
|
|
opts = &ExportOptions{}
|
|
}
|
|
return runExport(dockerCli, opts.ContextName, opts.Dest)
|
|
}
|
|
|
|
// runExport exports a Docker context.
|
|
func runExport(dockerCLI command.Cli, contextName string, dest string) error {
|
|
if err := store.ValidateContextName(contextName); err != nil && contextName != command.DefaultContextName {
|
|
return err
|
|
}
|
|
reader := store.Export(contextName, dockerCLI.ContextStore())
|
|
defer reader.Close()
|
|
return writeTo(dockerCLI, reader, dest)
|
|
}
|