user.Current may return err in some cases on Windows
ceclin opened this issue · comments
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x40 pc=0xae5ea2]
goroutine 1 [running]:
github.com/ferama/rospo/cmd.init.1()
github.com/ferama/rospo/cmd/grabpubkey.go:15 +0x62
I think it is better to use os.Getwd or empty string as a fallback.
You are right, thanks for reporting. The grabpubkey subcommand is not the only one that suffer from this issue. rospo needs better error handling for this scenario
Haha, in fact, this error will be triggered in sshc subcommand first. I fixed that and found not only sshc, so I opened this issue.
As a temporary fix, I adopted the following fallback strategy for home dir:
- user.Current()
- os.LookupEnv("HOME")
- os.Getwd()
- default to "/"
And the following fallback strategy for username:
- user.Current()
- os.LookupEnv("USER")
- default to "root"
This may serve as a reference for you.
By the way, it is interesting that I encountered a similar situation on linux, but I didn't get into why. (That's why I use root as username fallback).
Would you like to contribute with a pull request? We could define a couple of functions into the utils/helpers.go file that return homeDir and username. Then we should use those function everywhere the user.Current() is actually used
I was too busy the past few days.
A pull request is created just now. Review please.