11/* 
2- Copyright 2016  The Kubernetes Authors All rights reserved. 
2+ Copyright 2021  The Kubernetes Authors All rights reserved. 
33
44Licensed under the Apache License, Version 2.0 (the "License"); 
55you may not use this file except in compliance with the License. 
@@ -17,11 +17,13 @@ limitations under the License.
1717package  cmd
1818
1919import  (
20- 	"os" 
21- 
2220	"github.com/pkg/errors" 
2321	"github.com/spf13/cobra" 
2422
23+ 	"os" 
24+ 	pt "path" 
25+ 	"path/filepath" 
26+ 
2527	"k8s.io/minikube/pkg/minikube/assets" 
2628	"k8s.io/minikube/pkg/minikube/exit" 
2729	"k8s.io/minikube/pkg/minikube/mustload" 
@@ -37,25 +39,27 @@ var (
3739
3840// cpCmd represents the cp command, similar to docker cp 
3941var  cpCmd  =  & cobra.Command {
40- 	Use :   "cp <source file path> <target file path followed by '/home/docker/' >" ,
42+ 	Use :   "cp <source file path> <target file absolute path >" ,
4143	Short : "Copy the specified file into minikube" ,
42- 	Long : "Copy the specified file into minikube, it will be saved at path \" /home/docker/ <target file path>\"  in your minikube.\n "  + 
43- 		"Example Command : \" minikube cp a.txt b.txt\" \n " ,
44+ 	Long : "Copy the specified file into minikube, it will be saved at path <target file absolute  path> in your minikube.\n "  + 
45+ 		"Example Command : \" minikube cp a.txt /home/docker/ b.txt\" \n " ,
4446	Run : func (cmd  * cobra.Command , args  []string ) {
45- 		// validate args 
4647		if  len (args ) !=  2  {
4748			exit .Message (reason .Usage , `Please specify the path to copy:  
48- 	minikube cp <source file path> <target file path>    (example: "minikube cp a/b/c/d .txt a .txt")` )
49+ 	minikube cp <source file path> <target file absolute  path> (example: "minikube cp a/b.txt /copied .txt")` )
4950		}
51+ 
5052		srcPath  =  args [0 ]
5153		dstPath  =  args [1 ]
54+ 		validateArgs (srcPath , dstPath )
5255
5356		co  :=  mustload .Running (ClusterFlagValue ())
54- 		fa , err  :=  assets .NewFileAsset (srcPath , "/home/docker/" ,  dstPath , "0644" )
57+ 		fa , err  :=  assets .NewFileAsset (srcPath , pt . Dir ( dstPath ),  pt . Base ( dstPath ) , "0644" )
5558		if  err  !=  nil  {
5659			out .ErrLn ("%v" , errors .Wrap (err , "getting file asset" ))
5760			os .Exit (1 )
5861		}
62+ 
5963		if  err  =  co .CP .Runner .Copy (fa ); err  !=  nil  {
6064			out .ErrLn ("%v" , errors .Wrap (err , "copying file" ))
6165			os .Exit (1 )
@@ -65,3 +69,25 @@ var cpCmd = &cobra.Command{
6569
6670func  init () {
6771}
72+ 
73+ func  validateArgs (srcPath  string , dstPath  string ) {
74+ 	if  srcPath  ==  ""  {
75+ 		exit .Message (reason .Usage , "Source {{.path}} can not be empty" , out.V {"path" : srcPath })
76+ 	}
77+ 
78+ 	if  dstPath  ==  ""  {
79+ 		exit .Message (reason .Usage , "Target {{.path}} can not be empty" , out.V {"path" : dstPath })
80+ 	}
81+ 
82+ 	if  _ , err  :=  os .Stat (srcPath ); err  !=  nil  {
83+ 		if  os .IsNotExist (err ) {
84+ 			exit .Message (reason .HostPathMissing , "Cannot find directory {{.path}} for copy" , out.V {"path" : srcPath })
85+ 		} else  {
86+ 			exit .Error (reason .HostPathStat , "stat failed" , err )
87+ 		}
88+ 	}
89+ 
90+ 	if  ! filepath .IsAbs (dstPath ) {
91+ 		exit .Message (reason .Usage , `<target file absolute path> must be an absolute Path. Relative Path is not allowed (example: "/home/docker/copied.txt")` )
92+ 	}
93+ }
0 commit comments