diff --git a/src/cmd/goblin/main.go b/src/cmd/goblin/main.go index 20f2a3c..58ea3af 100644 --- a/src/cmd/goblin/main.go +++ b/src/cmd/goblin/main.go @@ -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) diff --git a/src/tool/whoami/main.go b/src/tool/whoami/main.go new file mode 100644 index 0000000..0c12ccd --- /dev/null +++ b/src/tool/whoami/main.go @@ -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) +}