Skip to content

Commit

Permalink
make cp commmand allow target path as absolutepath
Browse files Browse the repository at this point in the history
- only absolute path is allowed

Signed-off-by: anencore94 <anencore94@kaist.ac.kr>
  • Loading branch information
anencore94 committed Mar 4, 2021
1 parent 2e1635b commit 3b444db
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
43 changes: 34 additions & 9 deletions cmd/minikube/cmd/cp.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Copyright 2021 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -17,10 +17,11 @@ limitations under the License.
package cmd

import (
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"os"
pt "path"
"path/filepath"

"k8s.io/minikube/pkg/minikube/assets"
"k8s.io/minikube/pkg/minikube/exit"
Expand All @@ -37,25 +38,27 @@ var (

// cpCmd represents the cp command, similar to docker cp
var cpCmd = &cobra.Command{
Use: "cp <source file path> <target file path followed by '/home/docker/'>",
Use: "cp <source file path> <target file absolute path>",
Short: "Copy the specified file into minikube",
Long: "Copy the specified file into minikube, it will be saved at path \"/home/docker/<target file path>\" in your minikube.\n" +
"Example Command : \"minikube cp a.txt b.txt\"\n",
Long: "Copy the specified file into minikube, it will be saved at path <target file absolute path> in your minikube.\n" +
"Example Command : \"minikube cp a.txt /home/docker/b.txt\"\n",
Run: func(cmd *cobra.Command, args []string) {
// validate args
if len(args) != 2 {
exit.Message(reason.Usage, `Please specify the path to copy:
minikube cp <source file path> <target file path> (example: "minikube cp a/b/c/d.txt a.txt")`)
minikube cp <source file path> <target file absolute path> (example: "minikube cp a/b.txt /copied.txt")`)
}

srcPath = args[0]
dstPath = args[1]
validateArgs(srcPath, dstPath)

co := mustload.Running(ClusterFlagValue())
fa, err := assets.NewFileAsset(srcPath, "/home/docker/", dstPath, "0644")
fa, err := assets.NewFileAsset(srcPath, pt.Dir(dstPath), pt.Base(dstPath), "0644")
if err != nil {
out.ErrLn("%v", errors.Wrap(err, "getting file asset"))
os.Exit(1)
}

if err = co.CP.Runner.Copy(fa); err != nil {
out.ErrLn("%v", errors.Wrap(err, "copying file"))
os.Exit(1)
Expand All @@ -65,3 +68,25 @@ var cpCmd = &cobra.Command{

func init() {
}

func validateArgs(srcPath string, dstPath string) {
if srcPath == "" {
exit.Message(reason.Usage, "Source {{.path}} can not be empty", out.V{"path": srcPath})
}

if dstPath == "" {
exit.Message(reason.Usage, "Target {{.path}} can not be empty", out.V{"path": dstPath})
}

if _, err := os.Stat(srcPath); err != nil {
if os.IsNotExist(err) {
exit.Message(reason.HostPathMissing, "Cannot find directory {{.path}} for copy", out.V{"path": srcPath})
} else {
exit.Error(reason.HostPathStat, "stat failed", err)
}
}

if !filepath.IsAbs(dstPath) {
exit.Message(reason.Usage, `<target file absolute path> must be an absolute Path. Relative Path is not allowed (example: "/home/docker/copied.txt")`)
}
}
6 changes: 3 additions & 3 deletions site/content/en/docs/commands/cp.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ Copy the specified file into minikube

### Synopsis

Copy the specified file into minikube, it will be saved at path "/home/docker/<target file path>" in your minikube.
Example Command : "minikube cp a.txt b.txt"
Copy the specified file into minikube, it will be saved at path <target file absolute path> in your minikube.
Example Command : "minikube cp a.txt /home/docker/b.txt"


```shell
minikube cp <source file path> <target file path followed by '/home/docker/'> [flags]
minikube cp <source file path> <target file absolute path> [flags]
```

### Options inherited from parent commands
Expand Down
4 changes: 2 additions & 2 deletions test/integration/functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ func validateCpCmd(ctx context.Context, t *testing.T, profile string) {
}

cpPath := filepath.Join(*testdataDir, "cp-test.txt")
rr, err := Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "cp", cpPath, "hello_cp.txt"))
rr, err := Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "cp", cpPath, "/home/docker/hello_cp.txt"))
if ctx.Err() == context.DeadlineExceeded {
t.Errorf("failed to run command by deadline. exceeded timeout : %s", rr.Command())
}
Expand All @@ -1160,7 +1160,7 @@ func validateCpCmd(ctx context.Context, t *testing.T, profile string) {
}

expected := "Test file for checking file cp process"
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "ssh", "cat hello_cp.txt"))
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "ssh", "cat /home/docker/hello_cp.txt"))
if ctx.Err() == context.DeadlineExceeded {
t.Errorf("failed to run command by deadline. exceeded timeout : %s", rr.Command())
}
Expand Down

0 comments on commit 3b444db

Please sign in to comment.