diff --git a/cmd/minikube/cmd/cp.go b/cmd/minikube/cmd/cp.go index 4452e644ac23..5e19b0c91402 100644 --- a/cmd/minikube/cmd/cp.go +++ b/cmd/minikube/cmd/cp.go @@ -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. @@ -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" @@ -37,25 +38,27 @@ var ( // cpCmd represents the cp command, similar to docker cp var cpCmd = &cobra.Command{ - Use: "cp ", + Use: "cp ", Short: "Copy the specified file into minikube", - Long: "Copy the specified file into minikube, it will be saved at path \"/home/docker/\" 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 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 (example: "minikube cp a/b/c/d.txt a.txt")`) + minikube cp (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) @@ -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, ` must be an absolute Path. Relative Path is not allowed (example: "/home/docker/copied.txt")`) + } +} diff --git a/site/content/en/docs/commands/cp.md b/site/content/en/docs/commands/cp.md index 2db58d145261..b04ec3ab8ffc 100644 --- a/site/content/en/docs/commands/cp.md +++ b/site/content/en/docs/commands/cp.md @@ -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/" in your minikube. -Example Command : "minikube cp a.txt b.txt" +Copy the specified file into minikube, it will be saved at path in your minikube. +Example Command : "minikube cp a.txt /home/docker/b.txt" ```shell -minikube cp [flags] +minikube cp [flags] ``` ### Options inherited from parent commands diff --git a/test/integration/functional_test.go b/test/integration/functional_test.go index 1965a42e92a9..c21821419b43 100644 --- a/test/integration/functional_test.go +++ b/test/integration/functional_test.go @@ -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()) } @@ -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()) }