From 4e2e2f233c420115ada336548390694c93a4e536 Mon Sep 17 00:00:00 2001 From: Sharran Date: Fri, 14 Jul 2023 00:28:11 +0530 Subject: [PATCH 1/2] fix commit mail id --- cmd/minikube/cmd/cp.go | 57 +++++++++++++++++++++++++++++++++++-- cmd/minikube/cmd/cp_test.go | 23 +++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/cmd/minikube/cmd/cp.go b/cmd/minikube/cmd/cp.go index 012b0f5976..adec351cad 100644 --- a/cmd/minikube/cmd/cp.go +++ b/cmd/minikube/cmd/cp.go @@ -17,6 +17,9 @@ limitations under the License. package cmd import ( + "path" + "path/filepath" + "github.com/pkg/errors" "github.com/spf13/cobra" @@ -56,8 +59,10 @@ Example Command : "minikube cp a.txt /home/docker/b.txt" + minikube cp (example: "minikube cp a/b.txt /copied.txt")`) } - src := newRemotePath(args[0]) - dst := newRemotePath(args[1]) + srcPath := args[0] + dstPath := setDstFileNameFromSrc(args[1], srcPath) + src := newRemotePath(srcPath) + dst := newRemotePath(dstPath) validateArgs(src, dst) co := mustload.Running(ClusterFlagValue()) @@ -83,6 +88,54 @@ Example Command : "minikube cp a.txt /home/docker/b.txt" + func init() { } +// setDstFileNameFromSrc sets the src filename as dst filename +// when the dst file name is not provided and ends with a `/`. +// Otherwise this function is a no-op and returns the passed dst. +func setDstFileNameFromSrc(dst, src string) string { + srcPath := newRemotePath(src) + dstPath := newRemotePath(dst) + guestToHost := srcPath.node != "" && dstPath.node == "" + guestToGuest := srcPath.node != "" && dstPath.node != "" + + // Since Host can be any OS and Guest can only be linux, use + // filepath and path respectively + var dd, df, sf string + switch { + case guestToHost: + _, sf = path.Split(src) + dd, df = filepath.Split(dst) + case guestToGuest: + _, sf = path.Split(src) + dd, df = path.Split(dst) + default: + _, sf = filepath.Split(src) + dd, df = path.Split(dst) + } + + // if dst is empty, dd and df will be empty, so return dst + // validation will be happening in `validateArgs` + if dd == "" && df == "" { + return "" + } + + // if filename is already provided, return dst + if df != "" { + return dst + } + + // if src filename is empty, return dst + if sf == "" { + return dst + } + + // https://github.com/kubernetes/minikube/pull/15519/files#r1261750910 + if guestToHost { + return filepath.Join(dd, sf) + } + + return path.Join(dd, sf) +} + // split path to node name and file path func newRemotePath(path string) *remotePath { // if destination path is not a absolute path, trying to parse with : format diff --git a/cmd/minikube/cmd/cp_test.go b/cmd/minikube/cmd/cp_test.go index 632f8a89fe..1ec319f9ea 100644 --- a/cmd/minikube/cmd/cp_test.go +++ b/cmd/minikube/cmd/cp_test.go @@ -60,3 +60,26 @@ func TestParsePath(t *testing.T) { } } } + +func TestSetDstFileNameFromSrc(t *testing.T) { + cases := []struct { + src string + dst string + want string + }{ + {"./a/b", "/c/", "/c/b"}, + {"./a/b", "node:/c/", "node:/c/b"}, + {"./a", "/c/", "/c/a"}, + {"", "/c/", "/c/"}, + {"./a/b", "", ""}, + {"./a/b", "/c", "/c"}, + {"./a/", "/c/", "/c/"}, + } + + for _, c := range cases { + got := setDstFileNameFromSrc(c.dst, c.src) + if c.want != got { + t.Fatalf("wrong dst path for src=%s & dst=%s. want: %q, got: %q", c.src, c.dst, c.want, got) + } + } +} From 955d3865411b054a685803a2b56421bd619ed597 Mon Sep 17 00:00:00 2001 From: Sharran Date: Fri, 14 Jul 2023 01:14:21 +0530 Subject: [PATCH 2/2] path lint fix --- cmd/minikube/cmd/cp.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/cmd/minikube/cmd/cp.go b/cmd/minikube/cmd/cp.go index adec351cad..f60a068533 100644 --- a/cmd/minikube/cmd/cp.go +++ b/cmd/minikube/cmd/cp.go @@ -17,7 +17,6 @@ limitations under the License. package cmd import ( - "path" "path/filepath" "github.com/pkg/errors" @@ -102,14 +101,14 @@ func setDstFileNameFromSrc(dst, src string) string { var dd, df, sf string switch { case guestToHost: - _, sf = path.Split(src) + _, sf = pt.Split(src) dd, df = filepath.Split(dst) case guestToGuest: - _, sf = path.Split(src) - dd, df = path.Split(dst) + _, sf = pt.Split(src) + dd, df = pt.Split(dst) default: _, sf = filepath.Split(src) - dd, df = path.Split(dst) + dd, df = pt.Split(dst) } // if dst is empty, dd and df will be empty, so return dst @@ -133,7 +132,7 @@ func setDstFileNameFromSrc(dst, src string) string { return filepath.Join(dd, sf) } - return path.Join(dd, sf) + return pt.Join(dd, sf) } // split path to node name and file path