Skip to content

Commit

Permalink
don't use extra cmds, use a flag (#25)
Browse files Browse the repository at this point in the history
Introduced a `-D` flag to drop a path before re-adding it, preventing duplicates.

Corrected typos in comments and method names for better clarity.

Enhanced `DirList` test to cover new functionality and ensure the integrity of path manipulation.

Updated usage message to include information about the new `-D` flag feature.
  • Loading branch information
alessio authored Dec 29, 2023
1 parent fec2c44 commit 4bc6bb3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 44 deletions.
61 changes: 26 additions & 35 deletions cmd/pathctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var (
versionMode bool
listMode bool
noprefixMode bool
dropMode bool
)

var (
Expand All @@ -31,42 +32,37 @@ var cmdHandlers map[string]func(d pathlist.List)
func init() {
flag.BoolVar(&helpMode, "help", false, "display this help and exit.")
flag.BoolVar(&versionMode, "version", false, "output version information and exit.")
flag.BoolVar(&dropMode, "D", false, "drop the path before adding it again to the list.")
flag.BoolVar(&noprefixMode, "noprefix", false, "output the variable contents only.")
flag.BoolVar(&listMode, "L", false, "use a newline character as path list separator.")
flag.StringVar(&envVar, "E", "PATH", "input environment variable")
flag.StringVar(&envVar, "E", "PATH", "input environment variable.")
flag.Usage = usage
flag.CommandLine.SetOutput(os.Stderr)

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

return map[string]func(pathlist.List){
"append": hAppend,
"drop": hDrop,
"prepend": hPrepend,
"move-append": hMoveAppend,
"move-prepend": hMovePrepend,
//"appendPathctlDir": func() { appendPath(exePath()) },
//"prependPathctlDir": func() { prependPath(exePath()) },
"append": hAppend,
"drop": hDrop,
"prepend": hPrepend,

// aliases
"a": hAppend,
"d": hDrop,
"p": hPrepend,
"mv": hMoveAppend,
"mp": hMovePrepend,
"a": hAppend,
"d": hDrop,
"p": hPrepend,
}
}()
}
Expand Down Expand Up @@ -115,10 +111,6 @@ func printPathList(d pathlist.List) {
fmt.Println(sb.String())
}

func list(d pathlist.List) {
printPathList(d)
}

func handleHelpAndVersionModes() {
if !helpMode && !versionMode {
return
Expand All @@ -135,29 +127,28 @@ func handleHelpAndVersionModes() {
}

func usage() {
s := fmt.Sprintf(`Usage: %s COMMAND [PATH]
s := fmt.Sprintf(`Usage: %s [COMMAND [PATH]]
Make the management of the PATH environment variable
simple, fast, and predictable.
Commands:
append, a append a path to the end of the list
move-append, ma append a new path to the end of the list;
if the list contains the path already then
it will be moved to the end of the list
drop, d drop a path
prepend, p prepend a path to the list
move-prepend, mp prepend a new path to the top of the list;
if the list contains the path already then
it will be moved to the top of the list
append, a append a path to the end of the list.
drop, d drop a path.
prepend, p prepend a path to the list.
Options:
`, programme)
_, _ = fmt.Fprintln(os.Stderr, s)

flag.PrintDefaults()

_, _ = fmt.Fprintln(os.Stderr, `
When used with the -D flag, the commands append and prepend
drop PATH before adding it again to the list. This behaviour
guarantees that PATH is added as either the first or the last
element of the path list.
If COMMAND is not provided, it prints the contents of the PATH
environment variable.`)
}

16 changes: 8 additions & 8 deletions pathlist/list.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package pathlist implements functions to manipoulate PATH-like
// Package pathlist implements functions to manipulate PATH-like
// environment variables.
package pathlist

Expand Down Expand Up @@ -42,8 +42,8 @@ type List interface {
// Slice returns the path list as a slice of strings.
Slice() []string

// Returns the path list as a string of path list separator-separated
// directories.
// String returns the path list as a string of path list
// separator-separated directories.
String() string
}

Expand Down Expand Up @@ -168,18 +168,18 @@ func (d *dirList) clone(o *dirList) *dirList {
}

func removeDups[T comparable](col []T, applyFn func(T) (T, bool)) []T {
uniq := []T{}
var uniq = make([]T, 0)
ks := make(map[T]interface{})

for _, el := range col {
newval, ok := applyFn(el)
vv, ok := applyFn(el)
if !ok {
continue
}

if _, ok := ks[newval]; !ok {
uniq = append(uniq, newval)
ks[newval] = struct{}{}
if _, ok := ks[vv]; !ok {
uniq = append(uniq, vv)
ks[vv] = struct{}{}
}
}

Expand Down
3 changes: 2 additions & 1 deletion pathlist/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func Test_DirList_Prepend(t *testing.T) {

func Test_DirList_Drop(t *testing.T) {
d := New()
d.Load("/opt/local/bin:/usr/local/bin:/sbin:/bin:/var")
d.Load("/opt/local/bin:/usr/local/bin:/sbin:/bin:/var:/bin")
require.Equal(t, d.Slice(), []string{"/opt/local/bin", "/usr/local/bin", "/sbin", "/bin", "/var"})
d.Drop("/opt/local/bin")
d.Drop("/opt/local/bin")
d.Drop("/opt/local/bin")
Expand Down

0 comments on commit 4bc6bb3

Please sign in to comment.