Skip to content

Commit

Permalink
Merge pull request #995 from AkihiroSuda/carry-643-cp
Browse files Browse the repository at this point in the history
[Carry 643] cp cmd
  • Loading branch information
AkihiroSuda committed Apr 21, 2022
2 parents 258a9d0 + 85ca787 commit 3ed9238
Show file tree
Hide file tree
Showing 10 changed files with 647 additions and 3 deletions.
20 changes: 18 additions & 2 deletions README.md
Expand Up @@ -249,10 +249,12 @@ It does not necessarily mean that the corresponding features are missing in cont
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->


- [Container management](#container-management)
- [:whale: :blue_square: nerdctl run](#whale-blue_square-nerdctl-run)
- [:whale: :blue_square: nerdctl exec](#whale-blue_square-nerdctl-exec)
- [:whale: :blue_square: nerdctl create](#whale-blue_square-nerdctl-create)
- [:whale: nerdctl cp](#whale-nerdctl-cp)
- [:whale: :blue_square: nerdctl ps](#whale-blue_square-nerdctl-ps)
- [:whale: :blue_square: nerdctl inspect](#whale-blue_square-nerdctl-inspect)
- [:whale: nerdctl logs](#whale-nerdctl-logs)
Expand Down Expand Up @@ -301,7 +303,6 @@ It does not necessarily mean that the corresponding features are missing in cont
- [:nerd_face: :blue_square: nerdctl namespace ls](#nerd_face-blue_square-nerdctl-namespace-ls)
- [:nerd_face: :blue_square: nerdctl namespace remove](#nerd_face-blue_square-nerdctl-namespace-remove)
- [:nerd_face: :blue_square: nerdctl namespace update](#nerd_face-blue_square-nerdctl-namespace-update)

- [AppArmor profile management](#apparmor-profile-management)
- [:nerd_face: nerdctl apparmor inspect](#nerd_face-nerdctl-apparmor-inspect)
- [:nerd_face: nerdctl apparmor load](#nerd_face-nerdctl-apparmor-load)
Expand Down Expand Up @@ -620,6 +621,7 @@ Flags:

Unimplemented `docker exec` flags: `--detach-keys`


### :whale: :blue_square: nerdctl create
Create a new container.

Expand All @@ -629,6 +631,21 @@ Usage: `nerdctl create [OPTIONS] IMAGE [COMMAND] [ARG...]`

The `nerdctl create` command similar to `nerdctl run -d` except the container is never started. You can then use the `nerdctl start <container_id>` command to start the container at any point.

### :whale: nerdctl cp
Copy files/folders between a running container and the local filesystem

Usage:
- `nerdctl cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-`
- `nerdctl cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH`

:warning: `nerdctl cp` is designed only for use with trusted, cooperating containers.
Using `nerdctl cp` with untrusted or malicious containers is unsupported and may not provide protection against unexpected behavior.

Flags:
- :whale: `-L, --follow-link` Always follow symbol link in SRC_PATH.

Unimplemented `docker cp` flags: `--archive`

### :whale: :blue_square: nerdctl ps
List containers.

Expand Down Expand Up @@ -1422,7 +1439,6 @@ See [`./docs/config.md`](./docs/config.md).
## Unimplemented Docker commands
Container management:
- `docker attach`
- `docker cp`
- `docker diff`
- `docker rename`

Expand Down
1 change: 1 addition & 0 deletions cmd/nerdctl/container.go
Expand Up @@ -48,6 +48,7 @@ func newContainerCommand() *cobra.Command {
newUnpauseCommand(),
newCommitCommand(),
)
addCpCommand(containerCommand)
return containerCommand
}

Expand Down
68 changes: 68 additions & 0 deletions cmd/nerdctl/cp.go
@@ -0,0 +1,68 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"errors"
"path/filepath"
"strings"

"github.com/spf13/cobra"
)

var errFileSpecDoesntMatchFormat = errors.New("filespec must match the canonical format: [container:]file/path")

func parseCpFileSpec(arg string) (*cpFileSpec, error) {
i := strings.Index(arg, ":")

// filespec starting with a semicolon is invalid
if i == 0 {
return nil, errFileSpecDoesntMatchFormat
}

if filepath.IsAbs(arg) {
// Explicit local absolute path, e.g., `C:\foo` or `/foo`.
return &cpFileSpec{
Container: nil,
Path: arg,
}, nil
}

parts := strings.SplitN(arg, ":", 2)

if len(parts) == 1 || strings.HasPrefix(parts[0], ".") {
// Either there's no `:` in the arg
// OR it's an explicit local relative path like `./file:name.txt`.
return &cpFileSpec{
Path: arg,
}, nil
}

return &cpFileSpec{
Container: &parts[0],
Path: parts[1],
}, nil
}

type cpFileSpec struct {
Container *string
Path string
}

func cpShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveFilterFileExt
}

0 comments on commit 3ed9238

Please sign in to comment.