Skip to content

Commit 844f252

Browse files
committed
uid: escape paths
1 parent b0919a0 commit 844f252

File tree

8 files changed

+32
-17
lines changed

8 files changed

+32
-17
lines changed

Diff for: 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()

Diff for: 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
})

Diff for: example-nonposix/go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ go 1.15
44

55
require (
66
github.com/carapace-sh/carapace v0.50.3-0.20240311124258-a5adf91d8b8f
7-
github.com/spf13/cobra v1.8.0
8-
github.com/spf13/pflag v1.0.5
7+
github.com/spf13/cobra v1.9.1
8+
github.com/spf13/pflag v1.0.6
99
)
1010

1111
replace github.com/carapace-sh/carapace => ../

Diff for: example-nonposix/go.sum

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
github.com/carapace-sh/carapace-pflag v1.0.0 h1:uJMhl+vwEM/Eb0UdxZUuv4jo4rUAyPijkRGP5gfCuCE=
22
github.com/carapace-sh/carapace-pflag v1.0.0/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
3-
github.com/carapace-sh/carapace-shlex v1.0.0 h1:uUi+hovGpxWF6KjptrqAOHsFPT3mcMx0JKdq20hswDI=
4-
github.com/carapace-sh/carapace-shlex v1.0.0/go.mod h1:lJ4ZsdxytE0wHJ8Ta9S7Qq0XpjgjU0mdfCqiI2FHx7M=
5-
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
3+
github.com/carapace-sh/carapace-shlex v1.0.1 h1:ww0JCgWpOVuqWG7k3724pJ18Lq8gh5pHQs9j3ojUs1c=
64
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
75
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
86
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
9-
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
10-
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
7+
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
118
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
129
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1310
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

Diff for: 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

Diff for: go.work

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
go 1.19
1+
go 1.24.0
22

33
use (
44
.
55
./example-nonposix
6+
./example/cmd/_test_files
67
)

Diff for: 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+
}

Diff for: 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)