From 0e0d0696bc5d311b66e53c6ba551eabb32cc9a5b Mon Sep 17 00:00:00 2001 From: surdeus Date: Fri, 17 Feb 2023 16:07:42 +0300 Subject: [PATCH] Upgraded the paths tool. --- src/tool/paths/main.go | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/tool/paths/main.go b/src/tool/paths/main.go index 9b15e19..6c494e0 100644 --- a/src/tool/paths/main.go +++ b/src/tool/paths/main.go @@ -6,22 +6,53 @@ import ( "bufio" "os" "github.com/surdeus/goblin/src/pathx" + "path" + "log" + "errors" ) var ( + part string + handlers = map[string] func(string) string { + "base" : path.Base, + "ext" : path.Ext, + "dir" : path.Dir, + "all" : func(v string) string {return v}, + } + handler func(string) string + r bool + noPartErr = errors.New("no such part") ) + + func handlePath(p string) { - pth := pathx.From(p) - fmt.Println(pth.Real()) + if handler != nil { + p = handler(p) + } + + if r { + pth := pathx.From(p) + fmt.Println(pth.Real()) + } else { + fmt.Println(p) + } } func Run(args []string) { arg0 := args[0] args = args[1:] flags := flag.NewFlagSet(arg0, flag.ExitOnError) + flags.StringVar(&part, "p", "all", "part of path you want to print") + flags.BoolVar(&r, "r", false, "print real OS dependent paths") + flags.Parse(args) args = flags.Args() + lhandler, ok := handlers[part] + if !ok { + log.Fatal(noPartErr) + } + handler = lhandler for _, p := range args { handlePath(p)