Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape paths #32

Merged
merged 11 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions cmd/pathctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"os"
"strings"

"al.essio.dev/pkg/tools/dirlist"
"al.essio.dev/pkg/tools/internal/version"
"al.essio.dev/pkg/tools/pathlist"
)

const (
Expand All @@ -27,7 +27,7 @@ var (
envVar string
)

var cmdHandlers map[string]func(d pathlist.List)
var cmdHandlers map[string]func(d dirlist.List)
alessio marked this conversation as resolved.
Show resolved Hide resolved

func init() {
flag.BoolVar(&helpMode, "help", false, "display this help and exit.")
Expand All @@ -39,22 +39,22 @@ func init() {
flag.Usage = usage
flag.CommandLine.SetOutput(os.Stderr)

cmdHandlers = func() map[string]func(pathlist.List) {
hAppend := func(d pathlist.List) {
cmdHandlers = func() map[string]func(dirlist.List) {
hAppend := func(d dirlist.List) {
if dropMode {
d.Drop(flag.Arg(1))
}
d.Append(flag.Arg(1))
}
hDrop := func(d pathlist.List) { d.Drop(flag.Arg(1)) }
hPrepend := func(d pathlist.List) {
hDrop := func(d dirlist.List) { d.Drop(flag.Arg(1)) }
hPrepend := func(d dirlist.List) {
if dropMode {
d.Drop(flag.Arg(1))
}
d.Prepend(flag.Arg(1))
}

return map[string]func(pathlist.List){
return map[string]func(dirlist.List){
alessio marked this conversation as resolved.
Show resolved Hide resolved
"append": hAppend,
"drop": hDrop,
"prepend": hPrepend,
Expand All @@ -75,7 +75,7 @@ func main() {

handleHelpAndVersionModes()

dirs := pathlist.New()
dirs := dirlist.New()
dirs.LoadEnv(envVar)

if flag.NArg() < 1 {
Comment on lines 61 to 67
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 NOTE
This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [78-94]

The main function uses the dirlist package to load and manipulate environment variables but does not handle potential errors.

Implement error handling for the LoadEnv and other dirlist operations to ensure robustness.

Expand All @@ -91,7 +91,7 @@ func main() {
}
}

func printPathList(d pathlist.List) {
func printPathList(d dirlist.List) {
var sb = strings.Builder{}
sb.Reset()

Expand Down
43 changes: 43 additions & 0 deletions dirlist/dirlist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dirlist

var (
dList List
)

func init() {
dList = New()
}
alessio marked this conversation as resolved.
Show resolved Hide resolved

func Reset() { dList.Reset() }

func Contains(p string) bool {
return dList.Contains(quoteAndClean(p))
}

func Load(s string) {
dList.Load(s)
}

func LoadEnv(s string) {
dList.LoadEnv(s)
alessio marked this conversation as resolved.
Show resolved Hide resolved
}

func Prepend(p string) {
dList.Prepend(p)
}

func Append(p string) {
dList.Append(p)
}

func Drop(p string) {
dList.Drop(p)
}
alessio marked this conversation as resolved.
Show resolved Hide resolved

func Slice() []string {
return dList.Slice()
}

func String() string {
return dList.String()
alessio marked this conversation as resolved.
Show resolved Hide resolved
}
97 changes: 97 additions & 0 deletions dirlist/dirlist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package dirlist

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
)

func TestAppend(t *testing.T) {
Reset()
require.Equal(t, "", String())
for _, p := range []string{"/var", "/var", "/bin", "/bin/", "/bin///"} {
Append(p)
}

require.Equal(t, "/var:/bin", String())
Prepend("/bin///")
require.Equal(t, "/var:/bin", String())
}
alessio marked this conversation as resolved.
Show resolved Hide resolved

func TestContains(t *testing.T) {
alessio marked this conversation as resolved.
Show resolved Hide resolved
Reset()
Load("/opt/local/bin:/usr/local/bin:/sbin:/bin:/var:/bin")
require.False(t, Contains("/ur/local/sbin"))
require.False(t, Contains("/ur/local////sbin/"))
require.True(t, Contains("/sbin"))
require.True(t, Contains("///sbin//"))

}

func TestDrop(t *testing.T) {
Reset()
Load("/opt/local/bin:/usr/local/bin:/sbin:/bin:/var:/bin")
require.Equal(t, Slice(), []string{"/opt/local/bin", "/usr/local/bin", "/sbin", "/bin", "/var"})
Drop("/opt/local/bin")
Drop("/opt/local/bin")
Drop("/opt/local/bin")
Drop("/usr/local/bin")
Drop("/var")
require.NotEqual(t, "", String())
Drop("/sbin")
Drop("/bin")
require.False(t, Contains("/bin"))
require.Equal(t, "", String())
alessio marked this conversation as resolved.
Show resolved Hide resolved

Reset()
require.NotPanics(t, func() { Drop("") })
}

func TestLoadEnv(t *testing.T) {
tests := []struct {
name string
val string
want string
}{
{"empty", "", ""},
}
for i, tt := range tests {
tt2 := tt
t.Run(tt2.name, func(t *testing.T) {
envvar := fmt.Sprintf("%s_%d_VAR", t.Name(), i)
Reset()
alessio marked this conversation as resolved.
Show resolved Hide resolved
t.Setenv(envvar, tt.val)
LoadEnv(envvar)
require.Equal(t, tt2.want, String())
})
}
}

func TestPrepend(t *testing.T) {
Reset()
require.Equal(t, "", String())
for _, p := range []string{"/var", "/var", "/bin", "/bin/", "/bin///"} {
Append(p)
}

require.Equal(t, "/var:/bin", String())
alessio marked this conversation as resolved.
Show resolved Hide resolved
Prepend("/bin///")
require.Equal(t, "/var:/bin", String())
}

func TestSlice(t *testing.T) {
Reset()
require.Equal(t, 0, len(Slice()))
Prepend("/usr/bin")
Append("/bin")
require.Equal(t, []string{"/usr/bin", "/bin"}, Slice())
}

func TestString(t *testing.T) {
Reset()
require.Equal(t, "", String())
Prepend("/usr/bin")
Append("/bin")
require.Equal(t, "/usr/bin:/bin", String())
}
Loading
Loading