Skip to content

Commit b6ed59f

Browse files
committed
uid: escape paths
1 parent b0919a0 commit b6ed59f

File tree

7 files changed

+29
-10
lines changed

7 files changed

+29
-10
lines changed

action.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,8 @@ func (a Action) Uid(scheme, host string, opts ...string) Action {
485485
for index, v := range invoked.action.rawValues {
486486
uid := url.URL{
487487
Scheme: scheme,
488-
Host: host,
489-
Path: v.Value,
488+
Host: url.PathEscape(host),
489+
Path: uid.PathEscape(v.Value),
490490
}
491491
if len(opts) > 0 {
492492
values := uid.Query()

defaultActions.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func ActionDirectories() Action {
143143
if err != nil {
144144
return nil, err
145145
}
146-
return url.Parse("file://" + abs)
146+
return url.Parse("file://" + uid.PathEscape(abs))
147147
})
148148
}).Tag("directories")
149149
}
@@ -159,7 +159,7 @@ func ActionFiles(suffix ...string) Action {
159159
if err != nil {
160160
return nil, err
161161
}
162-
return url.Parse("file://" + abs)
162+
return url.Parse("file://" + uid.PathEscape(abs))
163163
})
164164
}).Tag("files")
165165
}
@@ -458,7 +458,7 @@ func ActionExecutables(dirs ...string) Action {
458458
}
459459
return batch.ToA().
460460
UidF(func(s string, uc uid.Context) (*url.URL, error) {
461-
return &url.URL{Scheme: "cmd", Host: s}, nil
461+
return &url.URL{Scheme: "cmd", Host: uid.PathEscape(s)}, nil
462462
})
463463
}).Tag("executables")
464464
}
@@ -480,7 +480,7 @@ func actionDirectoryExecutables(dir string, prefix string, manDescriptions map[s
480480
}
481481
}
482482
return ActionStyledValuesDescribed(vals...).UidF(func(s string, uc uid.Context) (*url.URL, error) {
483-
return url.Parse(fmt.Sprintf("file://%v/%v", dir, s)) // TODO trim slash suffix from dir | backslash path possible? (windows)
483+
return url.Parse(fmt.Sprintf("file://%v/%v", uid.PathEscape(dir), uid.PathEscape(s))) // TODO trim slash suffix from dir | backslash path possible? (windows)
484484
})
485485
}
486486
return ActionValues()
@@ -555,9 +555,9 @@ func ActionCommands(cmd *cobra.Command) Action {
555555

556556
switch uid.Path {
557557
case "":
558-
uid.Path = s
558+
uid.Path = url.PathEscape(s)
559559
default:
560-
uid.Path = uid.Path + "/" + s
560+
uid.Path = uid.Path + "/" + url.PathEscape(s)
561561
}
562562
return uid, nil
563563
})

docs/book.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ enable = true
1919

2020
[output.linkcheck]
2121
follow-web-links = true
22-
exclude = [ 'gnu\.org', '\./.+/out/.+\.gif']
22+
exclude = [ 'gnu\.org', '\./.+/out/.+\.gif', 'go\.dev', 'microsoft\.com']

example/cmd/_test_files/files_linux.go

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ package testfiles
2222
//go:generate touch -- quote_double_".txt
2323
//go:generate touch -- quote_both_'".txt
2424
//go:generate touch -- hash_#.txt
25+
//go:generate touch -- percent_d_%d.txt
26+
//go:generate touch -- percent_2f_%2F.txt
2527
//go:generate touch -- pipe_|.txt
2628
//go:generate touch -- question_?.txt
2729
//go:generate touch -- round-bracket_left_(.txt

example/cmd/_test_files/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/carapace-sh/carapace/example/_test_files
22

3+
go 1.16
4+
35
// fix go mod invalid char error: https://github.com/golang/go/issues/26672

pkg/uid/path.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package uid
2+
3+
import (
4+
"net/url"
5+
"strings"
6+
)
7+
8+
// PathEscape is like url.PathEscape but keeps '/'.
9+
func PathEscape(s string) string {
10+
segments := strings.Split(s, "/")
11+
for index, segment := range segments {
12+
segments[index] = url.PathEscape(segment)
13+
}
14+
return strings.Join(segments, "/")
15+
}

pkg/uid/uid.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type Context interface {
2121
func Command(cmd *cobra.Command) *url.URL {
2222
path := []string{cmd.Name()}
2323
for parent := cmd.Parent(); parent != nil; parent = parent.Parent() {
24-
path = append(path, parent.Name())
24+
path = append(path, url.PathEscape(parent.Name()))
2525
}
2626
reverse(path) // TODO slices.Reverse
2727
return &url.URL{

0 commit comments

Comments
 (0)