diff --git a/internal/cli/register.go b/internal/cli/register.go index 2774280..a507956 100644 --- a/internal/cli/register.go +++ b/internal/cli/register.go @@ -4,16 +4,50 @@ import ( "context" "fmt" "io/ioutil" + "os" nodepair "github.com/mudler/go-nodepair" qr "github.com/mudler/go-nodepair/qrcode" ) +// isDirectory determines if a file represented +// by `path` is a directory or not. +func isDirectory(path string) (bool, error) { + fileInfo, err := os.Stat(path) + if err != nil { + return false, err + } + + return fileInfo.IsDir(), err +} + +func isReadable(fileName string) bool { + file, err := os.Open(fileName) + if err != nil { + if os.IsPermission(err) { + return false + } + } + file.Close() + return true +} + func register(loglevel, arg, configFile, device string, reboot, poweroff bool) error { b, _ := ioutil.ReadFile(configFile) ctx, cancel := context.WithCancel(context.Background()) defer cancel() + if arg != "" { + isDir, err := isDirectory(arg) + if err == nil && isDir { + return fmt.Errorf("Cannot register with a directory, please pass a file.") //nolint:revive // This is a message printed to the user. + } else if err != nil { + return err + } + if !isReadable(arg) { + return fmt.Errorf("Cannot register with a file that is not readable.") //nolint:revive // This is a message printed to the user. + } + } // dmesg -D to suppress tty ev fmt.Println("Sending registration payload, please wait")