refactor stdin check

This commit is contained in:
filifa 2024-09-04 21:35:38 -05:00
parent 9d16b2970c
commit 82e2b05925
1 changed files with 16 additions and 4 deletions

View File

@ -18,7 +18,6 @@ package cmd
import (
"errors"
"log"
"os"
"github.com/spf13/cobra"
@ -28,6 +27,21 @@ import (
var presets []string
var transition bool
/*
* checkStdin checks if any data has been passed in through stdin and returns
* an error if we don't believe so.
*/
func checkStdin() error {
stat, err := os.Stdin.Stat()
if err != nil {
return err
} else if (stat.Mode() & os.ModeCharDevice) != 0 {
return errors.New("nothing to read from stdin")
}
return nil
}
/*
* validatePresets performs some basic checks on the presets passed in and
* returns an error if it finds a problem.
@ -108,11 +122,9 @@ func update(m *milkDropWindow) (bool, error) {
* milkbucket sets up the program and starts a rendering loop.
*/
func milkbucket(cmd *cobra.Command, args []string) {
stat, err := os.Stdin.Stat()
err := checkStdin()
if err != nil {
panic(err)
} else if (stat.Mode() & os.ModeCharDevice) != 0 {
log.Fatal("nothing to read from stdin")
}
err = sdl.Init(sdl.INIT_VIDEO)