Created
June 15, 2024 17:14
-
-
Save yunginnanet/25f1a997cf8f1d2fbbdb774607694c34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"io" | |
"os" | |
"strings" | |
) | |
// pathdedupe | |
// - kayos | |
// | |
// because idk where tf a lot of these | |
// broken path values in my env are coming from | |
// | |
// (lmao) | |
// | |
// ---------------------------------------------------------- | |
// | |
// $ go build -o $HOME/.local/bin/pathdedupe pathdedupe.go | |
// | |
// # usage: | |
// # OPTIONAL: set any additional strings you want filtered out of your PATH by setting PATH_REMOVE | |
// # drop this at the bottom of your .bashrc: | |
// | |
// ---- e.g: | |
// | |
// # export PATH_REMOVE="foo,bar" | |
// export PATH="$(echo $PATH | pathdedupe)" | |
func main() { | |
var input []string | |
if len(os.Args) < 2 { | |
inBuf, _ := io.ReadAll(os.Stdin) | |
input = strings.Split(string(inBuf), ":") | |
} else { | |
input = strings.Split(os.Args[1], ":") | |
} | |
var findRemove []string | |
fre := os.Getenv("PATH_REMOVE") | |
if fre != "" { | |
if strings.Contains(fre, ",") { | |
for _, v := range strings.Split(fre, ",") { | |
findRemove = append(findRemove, v) | |
} | |
} else { | |
findRemove = append(findRemove, fre) | |
} | |
} | |
seen := make(map[string]struct{}) | |
cloop: | |
for _, p := range input { | |
p = strings.ReplaceAll(p, "\n", "") | |
if len(p) < 2 { | |
continue | |
} | |
_, ok := seen[p] | |
if ok { | |
continue | |
} | |
seen[p] = struct{}{} | |
if len(findRemove) > 0 { | |
for _, fr := range findRemove { | |
if !strings.Contains(p, fr) { | |
continue | |
} | |
continue cloop | |
} | |
} | |
_, _ = os.Stdout.WriteString(p + ":") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment