Added whoami util

This commit is contained in:
Andrey Parhomenko 2023-02-26 13:12:40 +03:00
parent 8ec37e9279
commit 0b9e7aa8c2
2 changed files with 37 additions and 0 deletions

View file

@ -29,6 +29,7 @@ import(
"github.com/surdeus/goblin/src/tool/mk"
"github.com/surdeus/goblin/src/tool/awk"
"github.com/surdeus/goblin/src/tool/paths"
"github.com/surdeus/goblin/src/tool/whoami"
)
func main() {
@ -63,6 +64,10 @@ func main() {
paths.Run,
"convert UNIX slash separated paths into the OS compatible ones",
},
"whoami": mtool.Tool{
whoami.Run,
"print current user name",
},
}
mtool.Main("goblin", tools)

32
src/tool/whoami/main.go Normal file
View file

@ -0,0 +1,32 @@
package whoami
import(
"os"
"os/user"
"flag"
"fmt"
"log"
)
func Run(args []string) {
arg0 := args[0]
args = args[1:]
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s: %s\n", arg0, arg0)
flagSet.PrintDefaults()
}
flagSet.Parse(args)
if len(flagSet.Args())>0 {
flagSet.Usage()
os.Exit(1)
}
u, err := user.Current()
if err != nil {
log.Fatal(err)
}
fmt.Print(u.Username)
}