Skip to content

Commit 0535c11

Browse files
authored
Merge pull request kubernetes#90789 from hezhizhen/kubectl_cp
Refine extractFileSpec
2 parents 9b53b93 + 1c84efe commit 0535c11

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

pkg/kubectl/cmd/cp/cp.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -127,28 +127,33 @@ var (
127127
)
128128

129129
func extractFileSpec(arg string) (fileSpec, error) {
130-
if i := strings.Index(arg, ":"); i == -1 {
130+
i := strings.Index(arg, ":")
131+
132+
if i == -1 {
131133
return fileSpec{File: arg}, nil
132-
} else if i > 0 {
133-
file := arg[i+1:]
134-
pod := arg[:i]
135-
pieces := strings.Split(pod, "/")
136-
if len(pieces) == 1 {
137-
return fileSpec{
138-
PodName: pieces[0],
139-
File: file,
140-
}, nil
141-
}
142-
if len(pieces) == 2 {
143-
return fileSpec{
144-
PodNamespace: pieces[0],
145-
PodName: pieces[1],
146-
File: file,
147-
}, nil
148-
}
149134
}
150-
151-
return fileSpec{}, errFileSpecDoesntMatchFormat
135+
// filespec starting with a semicolon is invalid
136+
if i == 0 {
137+
return fileSpec{}, errFileSpecDoesntMatchFormat
138+
}
139+
140+
pod, file := arg[:i], arg[i+1:]
141+
pieces := strings.Split(pod, "/")
142+
switch len(pieces) {
143+
case 1:
144+
return fileSpec{
145+
PodName: pieces[0],
146+
File: file,
147+
}, nil
148+
case 2:
149+
return fileSpec{
150+
PodNamespace: pieces[0],
151+
PodName: pieces[1],
152+
File: file,
153+
}, nil
154+
default:
155+
return fileSpec{}, errFileSpecDoesntMatchFormat
156+
}
152157
}
153158

154159
// Complete completes all the required options

0 commit comments

Comments
 (0)