From 59e712c450a18928ed0663de650a286c85e4e973 Mon Sep 17 00:00:00 2001 From: Jonathan Pentecost Date: Sun, 8 Aug 2021 11:14:39 -0400 Subject: [PATCH] Standardise command output --- cmd/httpserver.go | 6 +++-- cmd/load-app.go | 22 ++++++++----------- cmd/load.go | 22 ++++++++----------- cmd/ls.go | 13 +++++------ cmd/mute.go | 5 ++--- cmd/next.go | 6 ++--- cmd/pause.go | 5 ++--- cmd/playlist.go | 40 ++++++++++++++------------------- cmd/previous.go | 5 ++--- cmd/restart.go | 7 ++---- cmd/rewind.go | 16 +++++--------- cmd/root.go | 3 +-- cmd/seek-to.go | 16 +++++--------- cmd/seek.go | 16 +++++--------- cmd/slideshow.go | 19 +++++----------- cmd/status.go | 12 +++++----- cmd/stop.go | 6 ++--- cmd/transcode.go | 18 +++++++-------- cmd/tts.go | 31 ++++++++++---------------- cmd/ui.go | 7 +++--- cmd/unmute.go | 6 ++--- cmd/unpause.go | 5 ++--- cmd/utils.go | 56 +++++++++++++++++++++++++++++++++++++---------- cmd/volume.go | 15 +++++-------- cmd/watch.go | 17 +++++++------- 25 files changed, 167 insertions(+), 207 deletions(-) diff --git a/cmd/httpserver.go b/cmd/httpserver.go index c4cec7d6..17fb6c18 100644 --- a/cmd/httpserver.go +++ b/cmd/httpserver.go @@ -25,7 +25,7 @@ var httpserverCmd = &cobra.Command{ Short: "Start the HTTP server", Long: `Start the HTTP server which provides an HTTP api to control chromecast devices on a network.`, - RunE: func(cmd *cobra.Command, args []string) error { + Run: func(cmd *cobra.Command, args []string) { addr, _ := cmd.Flags().GetString("http-addr") port, _ := cmd.Flags().GetString("http-port") @@ -35,7 +35,9 @@ api to control chromecast devices on a network.`, verbose, _ := cmd.Flags().GetBool("verbose") debug, _ := cmd.Flags().GetBool("debug") - return http.NewHandler(verbose || debug).Serve(addr + ":" + port) + if err := http.NewHandler(verbose || debug).Serve(addr + ":" + port); err != nil { + exit("unable to run http server: %v\n", err) + } }, } diff --git a/cmd/load-app.go b/cmd/load-app.go index 3735beb1..d6698e58 100644 --- a/cmd/load-app.go +++ b/cmd/load-app.go @@ -15,11 +15,8 @@ package cmd import ( - "fmt" - "github.com/vishen/go-chromecast/ui" - log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -31,14 +28,13 @@ var loadAppCmd = &cobra.Command{ the chromecast receiver app to be specified. An older list can be found here https://gist.github.com/jloutsenhizer/8855258. `, - RunE: func(cmd *cobra.Command, args []string) error { + Run: func(cmd *cobra.Command, args []string) { if len(args) != 2 { - return fmt.Errorf("requires exactly two arguments") + exit("requires exactly two arguments\n") } app, err := castApplication(cmd, args) if err != nil { - log.WithError(err).Info("unable to get cast application") - return nil + exit("unable to get cast application: %v\n", err) } // Optionally run a UI when playing this media: @@ -46,23 +42,23 @@ here https://gist.github.com/jloutsenhizer/8855258. if runWithUI { go func() { if err := app.LoadApp(args[0], args[1]); err != nil { - log.WithError(err).Fatal("unable to load media") + exit("unable to load media: %v\n", err) } }() ccui, err := ui.NewUserInterface(app) if err != nil { - log.WithError(err).Fatal("unable to prepare a new user-interface") + exit("unable to prepare a new user-interface: %v\n", err) + } + if err := ccui.Run(); err != nil { + exit("unable to run ui: %v\n", err) } - return ccui.Run() } // Otherwise just run in CLI mode: if err := app.LoadApp(args[0], args[1]); err != nil { - log.WithError(err).Info("unable to load media") - return nil + exit("unable to load media: %v\n", err) } - return nil }, } diff --git a/cmd/load.go b/cmd/load.go index da2de992..7139687d 100644 --- a/cmd/load.go +++ b/cmd/load.go @@ -15,11 +15,8 @@ package cmd import ( - "fmt" - "github.com/vishen/go-chromecast/ui" - log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -34,14 +31,13 @@ chromecast if it is a local file, otherwise it will load the url. If the media file is an unplayable media type by the chromecast, this will attempt to transcode the media file to mp4 using ffmpeg. This requires that ffmpeg is installed.`, - RunE: func(cmd *cobra.Command, args []string) error { + Run: func(cmd *cobra.Command, args []string) { if len(args) != 1 { - return fmt.Errorf("requires exactly one argument, should be the media file to load") + exit("requires exactly one argument, should be the media file to load\n") } app, err := castApplication(cmd, args) if err != nil { - log.WithError(err).Println("unable to get cast application") - return nil + exit("unable to get cast application: %v\n", err) } contentType, _ := cmd.Flags().GetString("content-type") @@ -53,23 +49,23 @@ that ffmpeg is installed.`, if runWithUI { go func() { if err := app.Load(args[0], contentType, transcode, detach, false); err != nil { - log.WithError(err).Fatal("unable to load media") + exit("unable to load media: %v\n", err) } }() ccui, err := ui.NewUserInterface(app) if err != nil { - log.WithError(err).Fatal("unable to prepare a new user-interface") + exit("unable to prepare a new user-interface: %v\n", err) + } + if err := ccui.Run(); err != nil { + exit("unable to run ui: %v\n", err) } - return ccui.Run() } // Otherwise just run in CLI mode: if err := app.Load(args[0], contentType, transcode, detach, false); err != nil { - log.WithError(err).Println("unable to load media") - return nil + exit("unable to load media: %v\n", err) } - return nil }, } diff --git a/cmd/ls.go b/cmd/ls.go index 87b3c073..5624cd27 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -19,7 +19,6 @@ import ( "net" "time" - log "github.com/sirupsen/logrus" "github.com/spf13/cobra" castdns "github.com/vishen/go-chromecast/dns" ) @@ -28,32 +27,30 @@ import ( var lsCmd = &cobra.Command{ Use: "ls", Short: "List devices", - RunE: func(cmd *cobra.Command, args []string) error { + Run: func(cmd *cobra.Command, args []string) { ifaceName, _ := cmd.Flags().GetString("iface") dnsTimeoutSeconds, _ := cmd.Flags().GetInt("dns-timeout") var iface *net.Interface var err error if ifaceName != "" { if iface, err = net.InterfaceByName(ifaceName); err != nil { - log.Fatalf("unable to find interface %q: %v", ifaceName, err) + exit("unable to find interface %q: %v\n", ifaceName, err) } } ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(dnsTimeoutSeconds)) defer cancel() castEntryChan, err := castdns.DiscoverCastDNSEntries(ctx, iface) if err != nil { - log.WithError(err).Error("unable to discover chromecast devices") - return nil + exit("unable to discover chromecast devices: %v\n", err) } i := 1 for d := range castEntryChan { - log.Infof("%d) device=%q device_name=%q address=\"%s:%d\" uuid=%q\n", i, d.Device, d.DeviceName, d.AddrV4, d.Port, d.UUID) + outputInfo("%d) device=%q device_name=%q address=\"%s:%d\" uuid=%q\n", i, d.Device, d.DeviceName, d.AddrV4, d.Port, d.UUID) i++ } if i == 1 { - log.Error("no cast devices found on network\n") + outputError("no cast devices found on network\n") } - return nil }, } diff --git a/cmd/mute.go b/cmd/mute.go index e4a89b19..0a870b76 100644 --- a/cmd/mute.go +++ b/cmd/mute.go @@ -15,7 +15,6 @@ package cmd import ( - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -26,11 +25,11 @@ var muteCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { app, err := castApplication(cmd, args) if err != nil { - logrus.Printf("unable to get cast application: %v\n", err) + exit("unable to get cast application: %v\n", err) return } if err := app.SetMuted(true); err != nil { - logrus.Printf("unable to mute cast application: %v\n", err) + exit("unable to mute cast application: %v\n", err) return } }, diff --git a/cmd/next.go b/cmd/next.go index b5bc9cad..3c0475e8 100644 --- a/cmd/next.go +++ b/cmd/next.go @@ -15,7 +15,6 @@ package cmd import ( - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -26,11 +25,10 @@ var nextCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { app, err := castApplication(cmd, args) if err != nil { - logrus.Printf("unable to get cast application: %v\n", err) - return + exit("unable to get cast application: %v\n", err) } if err := app.Next(); err != nil { - logrus.Printf("unable to play next media: %v\n", err) + exit("unable to play next media: %v\n", err) } }, } diff --git a/cmd/pause.go b/cmd/pause.go index 7ecd9cb5..15d6b31b 100644 --- a/cmd/pause.go +++ b/cmd/pause.go @@ -15,7 +15,6 @@ package cmd import ( - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -26,11 +25,11 @@ var pauseCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { app, err := castApplication(cmd, args) if err != nil { - logrus.Printf("unable to get cast application: %v\n", err) + exit("unable to get cast application: %v\n", err) return } if err := app.Pause(); err != nil { - logrus.Printf("unable to pause cast application: %v\n", err) + exit("unable to pause cast application: %v\n", err) return } }, diff --git a/cmd/playlist.go b/cmd/playlist.go index 58b51756..f048bde4 100644 --- a/cmd/playlist.go +++ b/cmd/playlist.go @@ -16,7 +16,6 @@ package cmd import ( "bufio" - "fmt" "io/ioutil" "os" "path/filepath" @@ -25,7 +24,6 @@ import ( "strings" "time" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/vishen/go-chromecast/ui" ) @@ -46,21 +44,18 @@ chromecast. If the media file is an unplayable media type by the chromecast, this will attempt to transcode the media file to mp4 using ffmpeg. This requires that ffmpeg is installed.`, - RunE: func(cmd *cobra.Command, args []string) error { + Run: func(cmd *cobra.Command, args []string) { if len(args) != 1 { - return fmt.Errorf("requires exactly one argument, should be the folder to play media from") + exit("requires exactly one argument, should be the folder to play media from\n") } if fileInfo, err := os.Stat(args[0]); err != nil { - logrus.Printf("unable to find %q: %v\n", args[0], err) - return nil + exit("unable to find %q: %v\n", args[0], err) } else if !fileInfo.Mode().IsDir() { - logrus.Printf("%q is not a directory\n", args[0]) - return nil + exit("%q is not a directory\n", args[0]) } app, err := castApplication(cmd, args) if err != nil { - logrus.Printf("unable to get cast application: %v\n", err) - return nil + exit("unable to get cast application: %v\n", err) } contentType, _ := cmd.Flags().GetString("content-type") @@ -70,8 +65,7 @@ that ffmpeg is installed.`, selection, _ := cmd.Flags().GetBool("select") files, err := ioutil.ReadDir(args[0]) if err != nil { - logrus.Printf("unable to list files from %q: %v", args[0], err) - return nil + exit("unable to list files from %q: %v\n", args[0], err) } filesToPlay := make([]mediaFile, 0, len(files)) for _, f := range files { @@ -144,21 +138,21 @@ that ffmpeg is installed.`, indexToPlayFrom := 0 if selection { - logrus.Println("Will play the following items, select where to start from:") + outputInfo("Will play the following items, select where to start from:") for i, f := range filenames { lastPlayed := "never" if lp, ok := app.PlayedItems()[f]; ok { t := time.Unix(lp.Started, 0) lastPlayed = t.String() } - logrus.Printf("%d) %s: last played %q\n", i+1, f, lastPlayed) + outputInfo("%d) %s: last played %q\n", i+1, f, lastPlayed) } reader := bufio.NewReader(os.Stdin) for { - logrus.Printf("Enter selection: ") + outputInfo("Enter selection: ") text, err := reader.ReadString('\n') if err != nil { - logrus.Printf("error reading console: %v\n", err) + outputError("reading console: %v\n", err) continue } i, err := strconv.Atoi(strings.TrimSpace(text)) @@ -199,29 +193,29 @@ that ffmpeg is installed.`, for _, f := range filenames[indexToPlayFrom:] { s += "- " + f + "\n" } - logrus.Print(s) + outputInfo(s) // Optionally run a UI when playing this media: runWithUI, _ := cmd.Flags().GetBool("with-ui") if runWithUI { go func() { if err := app.QueueLoad(filenames[indexToPlayFrom:], contentType, transcode); err != nil { - logrus.WithError(err).Fatal("unable to play playlist on cast application") + exit("unable to play playlist on cast application: %v\n", err) } }() ccui, err := ui.NewUserInterface(app) if err != nil { - logrus.WithError(err).Fatal("unable to prepare a new user-interface") + exit("unable to prepare a new user-interface: %v\n", err) + } + if err := ccui.Run(); err != nil { + exit("unable to run ui: %v\n", err) } - return ccui.Run() } if err := app.QueueLoad(filenames[indexToPlayFrom:], contentType, transcode); err != nil { - logrus.Printf("unable to play playlist on cast application: %v\n", err) - return nil + exit("unable to play playlist on cast application: %v\n", err) } - return nil }, } diff --git a/cmd/previous.go b/cmd/previous.go index 7c768919..25dadcdc 100644 --- a/cmd/previous.go +++ b/cmd/previous.go @@ -15,7 +15,6 @@ package cmd import ( - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -26,11 +25,11 @@ var previousCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { app, err := castApplication(cmd, args) if err != nil { - logrus.Printf("unable to get cast application: %v\n", err) + exit("unable to get cast application: %v\n", err) return } if err := app.Previous(); err != nil { - logrus.Printf("unable to play previous media: %v\n", err) + exit("unable to play previous media: %v\n", err) } }, } diff --git a/cmd/restart.go b/cmd/restart.go index 17c40674..dcb1837b 100644 --- a/cmd/restart.go +++ b/cmd/restart.go @@ -15,7 +15,6 @@ package cmd import ( - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -26,12 +25,10 @@ var restartCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { app, err := castApplication(cmd, args) if err != nil { - logrus.Printf("unable to get cast application: %v\n", err) - return + exit("unable to get cast application: %v\n", err) } - // TODO(): THIS DOES NOT WORK if err := app.SeekFromStart(0); err != nil { - logrus.Printf("unable to restart media: %v\n", err) + exit("unable to restart media: %v\n", err) } }, } diff --git a/cmd/rewind.go b/cmd/rewind.go index 39ff4644..257e9ec3 100644 --- a/cmd/rewind.go +++ b/cmd/rewind.go @@ -17,8 +17,6 @@ package cmd import ( "strconv" - "github.com/pkg/errors" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -26,25 +24,21 @@ import ( var rewindCmd = &cobra.Command{ Use: "rewind ", Short: "Rewind by seconds the currently playing media", - RunE: func(cmd *cobra.Command, args []string) error { + Run: func(cmd *cobra.Command, args []string) { if len(args) != 1 { - return errors.New("one argument required") + exit("one argument required\n") } value, err := strconv.Atoi(args[0]) if err != nil { - logrus.Printf("unable to parse %q to an integer\n", args[0]) - return nil + exit("unable to parse %q to an integer\n", args[0]) } app, err := castApplication(cmd, args) if err != nil { - logrus.Printf("unable to get cast application: %v\n", err) - return nil + exit("unable to get cast application: %v\n", err) } if err := app.Seek(-value); err != nil { - logrus.Printf("unable to rewind current media: %v\n", err) - return nil + exit("unable to rewind current media: %v\n", err) } - return nil }, } diff --git a/cmd/root.go b/cmd/root.go index db451d1b..c688c1a2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,7 +17,6 @@ package cmd import ( "time" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -39,7 +38,7 @@ command line.`, if len(Version) > 0 && Version[0] != 'v' && Version != "dev" { Version = "v" + Version } - logrus.Printf("go-chromecast %s (%s) %s\n", Version, Commit, Date) + outputInfo("go-chromecast %s (%s) %s\n", Version, Commit, Date) return nil } return cmd.Help() diff --git a/cmd/seek-to.go b/cmd/seek-to.go index 1a26cb3b..5692ee76 100644 --- a/cmd/seek-to.go +++ b/cmd/seek-to.go @@ -17,8 +17,6 @@ package cmd import ( "strconv" - "github.com/pkg/errors" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -26,25 +24,21 @@ import ( var seekToCmd = &cobra.Command{ Use: "seek-to ", Short: "Seek to the in the currently playing media", - RunE: func(cmd *cobra.Command, args []string) error { + Run: func(cmd *cobra.Command, args []string) { if len(args) != 1 { - return errors.New("one argument required") + exit("one argument required\n") } value, err := strconv.ParseFloat(args[0], 32) if err != nil { - logrus.Printf("unable to parse %q to an integer\n", args[0]) - return nil + exit("unable to parse %q to an integer\n", args[0]) } app, err := castApplication(cmd, args) if err != nil { - logrus.Printf("unable to get cast application: %v\n", err) - return nil + exit("unable to get cast application: %v\n", err) } if err := app.SeekToTime(float32(value)); err != nil { - logrus.Printf("unable to seek to current media: %v\n", err) - return nil + exit("unable to seek to current media: %v\n", err) } - return nil }, } diff --git a/cmd/seek.go b/cmd/seek.go index 250f61a2..e1d7a678 100644 --- a/cmd/seek.go +++ b/cmd/seek.go @@ -17,8 +17,6 @@ package cmd import ( "strconv" - "github.com/pkg/errors" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -26,25 +24,21 @@ import ( var seekCmd = &cobra.Command{ Use: "seek ", Short: "Seek by seconds into the currently playing media", - RunE: func(cmd *cobra.Command, args []string) error { + Run: func(cmd *cobra.Command, args []string) { if len(args) != 1 { - return errors.New("one argument required") + exit("one argument required\n") } value, err := strconv.Atoi(args[0]) if err != nil { - logrus.Printf("unable to parse %q to an integer\n", args[0]) - return nil + exit("unable to parse %q to an integer\n", args[0]) } app, err := castApplication(cmd, args) if err != nil { - logrus.Printf("unable to get cast application: %v\n", err) - return nil + exit("unable to get cast application: %v\n", err) } if err := app.Seek(value); err != nil { - logrus.Printf("unable to seek current media: %v\n", err) - return nil + exit("unable to seek current media: %v\n", err) } - return nil }, } diff --git a/cmd/slideshow.go b/cmd/slideshow.go index d92c64a9..c6de0976 100644 --- a/cmd/slideshow.go +++ b/cmd/slideshow.go @@ -15,10 +15,8 @@ package cmd import ( - "fmt" "os" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -26,32 +24,27 @@ import ( var slideshowCmd = &cobra.Command{ Use: "slideshow file1 file2 ...", Short: "Play a slideshow of photos", - RunE: func(cmd *cobra.Command, args []string) error { + Run: func(cmd *cobra.Command, args []string) { if len(args) == 0 { - return fmt.Errorf("requires files to play in slideshow") + exit("requires files to play in slideshow\n") } for _, arg := range args { if fileInfo, err := os.Stat(arg); err != nil { - logrus.Printf("unable to find %q: %v\n", arg, err) - return nil + exit("unable to find %q: %v\n", arg, err) } else if fileInfo.Mode().IsDir() { - logrus.Printf("%q is a directory\n", arg) - return nil + exit("%q is a directory\n", arg) } } app, err := castApplication(cmd, args) if err != nil { - logrus.Printf("unable to get cast application: %v\n", err) - return nil + exit("unable to get cast application: %v\n", err) } duration, _ := cmd.Flags().GetInt("duration") repeat, _ := cmd.Flags().GetBool("repeat") if err := app.Slideshow(args, duration, repeat); err != nil { - logrus.Printf("unable to play slideshow on cast application: %v\n", err) - return nil + exit("unable to play slideshow on cast application: %v\n", err) } - return nil }, } diff --git a/cmd/status.go b/cmd/status.go index 817d890d..9654524d 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -17,7 +17,6 @@ package cmd import ( "fmt" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -28,16 +27,15 @@ var statusCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { app, err := castApplication(cmd, args) if err != nil { - logrus.Printf("unable to get cast application: %v\n", err) - return + exit("unable to get cast application: %v\n", err) } castApplication, castMedia, castVolume := app.Status() if castApplication == nil { - logrus.Printf("Idle, volume=%0.2f muted=%t\n", castVolume.Level, castVolume.Muted) + outputInfo("Idle, volume=%0.2f muted=%t\n", castVolume.Level, castVolume.Muted) } else if castApplication.IsIdleScreen { - logrus.Printf("Idle (%s), volume=%0.2f muted=%t\n", castApplication.DisplayName, castVolume.Level, castVolume.Muted) + outputInfo("Idle (%s), volume=%0.2f muted=%t\n", castApplication.DisplayName, castVolume.Level, castVolume.Muted) } else if castMedia == nil { - logrus.Printf("Idle (%s), volume=%0.2f muted=%t\n", castApplication.DisplayName, castVolume.Level, castVolume.Muted) + outputInfo("Idle (%s), volume=%0.2f muted=%t\n", castApplication.DisplayName, castVolume.Level, castVolume.Muted) } else { metadata := "unknown" var usefulID string @@ -49,7 +47,7 @@ var statusCmd = &cobra.Command{ md := castMedia.Media.Metadata metadata = fmt.Sprintf("title=%q, artist=%q", md.Title, md.Artist) } - logrus.Printf("%s%s (%s), %s, time remaining=%.0fs/%.0fs, volume=%0.2f, muted=%t\n", usefulID, castApplication.DisplayName, castMedia.PlayerState, metadata, castMedia.CurrentTime, castMedia.Media.Duration, castVolume.Level, castVolume.Muted) + outputInfo("%s%s (%s), %s, time remaining=%.0fs/%.0fs, volume=%0.2f, muted=%t\n", usefulID, castApplication.DisplayName, castMedia.PlayerState, metadata, castMedia.CurrentTime, castMedia.Media.Duration, castVolume.Level, castVolume.Muted) } }, } diff --git a/cmd/stop.go b/cmd/stop.go index ce4fa6aa..eccfb53e 100644 --- a/cmd/stop.go +++ b/cmd/stop.go @@ -15,7 +15,6 @@ package cmd import ( - log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -26,11 +25,10 @@ var stopCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { app, err := castApplication(cmd, args) if err != nil { - log.Printf("unable to get cast application: %v\n", err) - return + exit("unable to get cast application: %v\n", err) } if err := app.Stop(); err != nil { - log.Printf("unable to stop casting: %v\n", err) + exit("unable to stop casting: %v\n", err) } }, } diff --git a/cmd/transcode.go b/cmd/transcode.go index 6b2061a9..126b217d 100644 --- a/cmd/transcode.go +++ b/cmd/transcode.go @@ -15,7 +15,6 @@ package cmd import ( - "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/vishen/go-chromecast/ui" ) @@ -28,11 +27,10 @@ var transcodeCmd = &cobra.Command{ locally and serve the output of the transcoding operation to the chromecast. This command requires the program or script to write the media content to stdout. The transcoded media content-type is required as well`, - RunE: func(cmd *cobra.Command, args []string) error { + Run: func(cmd *cobra.Command, args []string) { app, err := castApplication(cmd, args) if err != nil { - logrus.Printf("unable to get cast application: %v\n", err) - return nil + exit("unable to get cast application: %v\n", err) } contentType, _ := cmd.Flags().GetString("content-type") @@ -42,22 +40,22 @@ The transcoded media content-type is required as well`, if runWithUI { go func() { if err := app.Transcode(command, contentType); err != nil { - logrus.WithError(err).Fatal("unable to load media") + exit("unable to load media: %v\n", err) } }() ccui, err := ui.NewUserInterface(app) if err != nil { - logrus.WithError(err).Fatal("unable to prepare a new user-interface") + exit("unable to prepare a new user-interface: %v\n", err) + } + if err := ccui.Run(); err != nil { + exit("unable to run ui: %v\n", err) } - return ccui.Run() } if err := app.Transcode(command, contentType); err != nil { - logrus.Printf("unable to transcode media: %v\n", err) - return nil + exit("unable to transcode media: %v\n", err) } - return nil }, } diff --git a/cmd/tts.go b/cmd/tts.go index ded96c72..aefef992 100644 --- a/cmd/tts.go +++ b/cmd/tts.go @@ -18,7 +18,6 @@ import ( "io/ioutil" "os" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/vishen/go-chromecast/tts" ) @@ -30,57 +29,51 @@ var ttsCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { if len(args) != 1 || args[0] == "" { - logrus.Printf("expected exactly one argument to convert to speech\n") + exit("expected exactly one argument to convert to speech\n") return } googleServiceAccount, _ := cmd.Flags().GetString("google-service-account") if googleServiceAccount == "" { - logrus.Printf("--google-service-account is required\n") + exit("--google-service-account is required\n") return } - + languageCode, _ := cmd.Flags().GetString("language-code") voiceName, _ := cmd.Flags().GetString("voice-name") - speakingRate, _ := cmd.Flags().GetFloat32("speaking-rate") - pitch, _ := cmd.Flags().GetFloat32("pitch") + speakingRate, _ := cmd.Flags().GetFloat32("speaking-rate") + pitch, _ := cmd.Flags().GetFloat32("pitch") b, err := ioutil.ReadFile(googleServiceAccount) if err != nil { - logrus.Printf("unable to open google service account file: %v\n", err) - return + exit("unable to open google service account file: %v\n", err) } app, err := castApplication(cmd, args) if err != nil { - logrus.Printf("unable to get cast application: %v\n", err) - return + exit("unable to get cast application: %v\n", err) } data, err := tts.Create(args[0], b, languageCode, voiceName, speakingRate, pitch) if err != nil { - logrus.Println(err) - return + exit("unable to create tts: %v\n", err) } f, err := ioutil.TempFile("", "go-chromecast-tts") if err != nil { - logrus.Printf("unable to create temp file: %v", err) - return + exit("unable to create temp file: %v\n", err) } defer os.Remove(f.Name()) if _, err := f.Write(data); err != nil { - logrus.Printf("unable to write to temp file: %v\n", err) - return + exit("unable to write to temp file: %v\n", err) } if err := f.Close(); err != nil { - logrus.Printf("unable to close temp file: %v\n", err) - return + exit("unable to close temp file: %v\n", err) } if err := app.Load(f.Name(), "audio/mp3", false, false, false); err != nil { - logrus.Printf("unable to load media to device: %v\n", err) + exit("unable to load media to device: %v\n", err) } }, } diff --git a/cmd/ui.go b/cmd/ui.go index 1f432f93..b8f8a435 100644 --- a/cmd/ui.go +++ b/cmd/ui.go @@ -15,7 +15,6 @@ package cmd import ( - "github.com/sirupsen/logrus" "github.com/vishen/go-chromecast/ui" "github.com/spf13/cobra" @@ -28,17 +27,17 @@ var uiCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { app, err := castApplication(cmd, args) if err != nil { - logrus.Printf("unable to get cast application: %v\n", err) + exit("unable to get cast application: %v\n", err) return } ccui, err := ui.NewUserInterface(app) if err != nil { - logrus.Printf("unable to prepare a new user-interface: %v\n", err) + exit("unable to prepare a new user-interface: %v\n", err) } if err := ccui.Run(); err != nil { - logrus.Printf("unable to start the user-interface: %v\n", err) + exit("unable to start the user-interface: %v\n", err) } }, } diff --git a/cmd/unmute.go b/cmd/unmute.go index d14d2a77..0da0ab79 100644 --- a/cmd/unmute.go +++ b/cmd/unmute.go @@ -15,7 +15,6 @@ package cmd import ( - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -26,12 +25,11 @@ var unmuteCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { app, err := castApplication(cmd, args) if err != nil { - logrus.Printf("unable to get cast application: %v\n", err) + exit("unable to get cast application: %v\n", err) return } if err := app.SetMuted(false); err != nil { - logrus.Printf("unable to unmute cast application: %v\n", err) - return + exit("unable to unmute cast application: %v\n", err) } }, } diff --git a/cmd/unpause.go b/cmd/unpause.go index 20251f3b..f9788443 100644 --- a/cmd/unpause.go +++ b/cmd/unpause.go @@ -15,7 +15,6 @@ package cmd import ( - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -26,11 +25,11 @@ var unpauseCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { app, err := castApplication(cmd, args) if err != nil { - logrus.Printf("unable to get cast application: %v\n", err) + exit("unable to get cast application: %v\n", err) return } if err := app.Unpause(); err != nil { - logrus.Printf("unable to pause cast application: %v\n", err) + exit("unable to pause cast application: %v\n", err) } }, } diff --git a/cmd/utils.go b/cmd/utils.go index 471796c5..5eb21bef 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "fmt" + "log" "net" "os" "sort" @@ -12,8 +13,6 @@ import ( "strings" "time" - log "github.com/sirupsen/logrus" - "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/vishen/go-chromecast/application" @@ -23,7 +22,6 @@ import ( func init() { log.SetOutput(os.Stdout) - log.SetLevel(log.DebugLevel) } var ( @@ -117,14 +115,14 @@ func castApplication(cmd *cobra.Command, args []string) (*application.Applicatio } cachedEntryJson, _ := json.Marshal(cachedEntry) if err := cache.Save(getCacheKey(cachedEntry.UUID), cachedEntryJson); err != nil { - log.Debug("Failed to save UUID cache entry") + fmt.Printf("Failed to save UUID cache entry\n") } if err := cache.Save(getCacheKey(cachedEntry.Name), cachedEntryJson); err != nil { - log.Debug("Failed to save name cache entry") + fmt.Printf("Failed to save name cache entry\n") } } if debug { - log.Printf("using device name=%s addr=%s port=%d uuid=%s\n", entry.GetName(), entry.GetAddr(), entry.GetPort(), entry.GetUUID()) + fmt.Printf("using device name=%s addr=%s port=%d uuid=%s\n", entry.GetName(), entry.GetAddr(), entry.GetPort(), entry.GetUUID()) } } else { p, err := strconv.Atoi(port) @@ -142,10 +140,10 @@ func castApplication(cmd *cobra.Command, args []string) (*application.Applicatio // an error, this is to make sure that if the device gets a new // ipaddress we will invalidate the cache. if err := cache.Save(getCacheKey(entry.GetUUID()), []byte{}); err != nil { - log.Debug("Failed to save UUID cache entry") + fmt.Printf("Failed to save UUID cache entry: %v\n", err) } if err := cache.Save(getCacheKey(entry.GetName()), []byte{}); err != nil { - log.Debug("Failed to save name cache entry") + fmt.Printf("Failed to save name cache entry: %v\n", err) } return nil, err } @@ -202,16 +200,16 @@ func findCastDNS(iface *net.Interface, dnsTimeoutSeconds int, device, deviceName // Always return entries in deterministic order. sort.Slice(foundEntries, func(i, j int) bool { return foundEntries[i].DeviceName < foundEntries[j].DeviceName }) - log.Printf("Found %d cast dns entries, select one:\n", len(foundEntries)) + fmt.Printf("Found %d cast dns entries, select one:\n", len(foundEntries)) for i, d := range foundEntries { - log.Printf("%d) device=%q device_name=%q address=\"%s:%d\" uuid=%q\n", i+1, d.Device, d.DeviceName, d.AddrV4, d.Port, d.UUID) + fmt.Printf("%d) device=%q device_name=%q address=\"%s:%d\" uuid=%q\n", i+1, d.Device, d.DeviceName, d.AddrV4, d.Port, d.UUID) } reader := bufio.NewReader(os.Stdin) for { - log.Printf("Enter selection: ") + fmt.Printf("Enter selection: ") text, err := reader.ReadString('\n') if err != nil { - log.Printf("error reading console: %v\n", err) + fmt.Printf("error reading console: %v\n", err) continue } i, err := strconv.Atoi(strings.TrimSpace(text)) @@ -223,3 +221,37 @@ func findCastDNS(iface *net.Interface, dnsTimeoutSeconds int, device, deviceName return foundEntries[i-1], nil } } + +func outputError(msg string, args ...interface{}) { + output(output_Error, msg, args...) +} + +func outputInfo(msg string, args ...interface{}) { + output(output_Info, msg, args...) +} + +func exit(msg string, args ...interface{}) { + outputError(msg, args...) + os.Exit(1) +} + +type outputLevel int + +const ( + output_Info outputLevel = iota + output_Error +) + +func output(t outputLevel, msg string, args ...interface{}) { + switch t { + case output_Error: + fmt.Printf("%serror%s: ", RED, NC) + } + + fmt.Printf(msg, args...) +} + +const ( + RED = "\033[0;31m" + NC = "\033[0m" // No Color +) diff --git a/cmd/volume.go b/cmd/volume.go index 7e14a974..9837873e 100644 --- a/cmd/volume.go +++ b/cmd/volume.go @@ -17,7 +17,6 @@ package cmd import ( "strconv" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -29,29 +28,25 @@ var volumeCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { app, err := castApplication(cmd, args) if err != nil { - logrus.Printf("unable to get cast application: %v\n", err) - return + exit("unable to get cast application: %v\n", err) } if len(args) == 1 && args[0] != "" { newVolume, err := strconv.ParseFloat(args[0], 32) if err != nil { - logrus.Printf("invalid volume: %v\n", err) - return + exit("invalid volume: %v\n", err) } if err = app.SetVolume(float32(newVolume)); err != nil { - logrus.Printf("failed to set volume: %v\n", err) - return + exit("failed to set volume: %v\n", err) } } if err = app.Update(); err != nil { - logrus.Printf("unable to update cast info: %v\n", err) - return + exit("unable to update cast info: %v\n", err) } _, _, castVolume := app.Status() - logrus.Printf("%0.2f\n", castVolume.Level) + outputInfo("%0.2f\n", castVolume.Level) }, } diff --git a/cmd/watch.go b/cmd/watch.go index 3669f80c..fd5bce47 100644 --- a/cmd/watch.go +++ b/cmd/watch.go @@ -24,7 +24,6 @@ import ( "github.com/buger/jsonparser" "github.com/spf13/cobra" - log "github.com/sirupsen/logrus" "github.com/vishen/go-chromecast/application" pb "github.com/vishen/go-chromecast/cast/proto" ) @@ -47,7 +46,7 @@ var watchCmd = &cobra.Command{ retry := false app, err := castApplication(cmd, args) if err != nil { - log.WithError(err).Info("unable to get cast application") + outputError("unable to get cast application: %v", err) time.Sleep(time.Second * 10) continue } @@ -55,7 +54,7 @@ var watchCmd = &cobra.Command{ go func() { for { if err := app.Update(); err != nil { - log.WithError(err).Info("unable to update cast application") + outputError("unable to update cast application: %v", err) retry = true close(done) return @@ -91,13 +90,13 @@ var watchCmd = &cobra.Command{ "payload": payload, }) case outputNormal: - log.Infof("CHROMECAST BROADCAST MESSAGE: type=%s proto=%s (namespace=%s) %s -> %s | %s\n", messageType, protocolVersion, namespace, sourceID, destID, payload) + outputInfo("CHROMECAST BROADCAST MESSAGE: type=%s proto=%s (namespace=%s) %s -> %s | %s\n", messageType, protocolVersion, namespace, sourceID, destID, payload) } }) <-done if retry { // Sleep a little bit in-between retries - log.Infoln("attempting a retry...") + outputInfo("attempting a retry...\n") time.Sleep(time.Second * 10) } } @@ -123,11 +122,11 @@ func outputStatus(app *application.Application, outputType outputType) { }) case outputNormal: if castApplication == nil { - log.Infof("Idle, volume=%0.2f muted=%t\n", castVolume.Level, castVolume.Muted) + outputInfo("Idle, volume=%0.2f muted=%t\n", castVolume.Level, castVolume.Muted) } else if castApplication.IsIdleScreen { - log.Infof("Idle (%s), volume=%0.2f muted=%t\n", castApplication.DisplayName, castVolume.Level, castVolume.Muted) + outputInfo("Idle (%s), volume=%0.2f muted=%t\n", castApplication.DisplayName, castVolume.Level, castVolume.Muted) } else if castMedia == nil { - log.Infof("Idle (%s), volume=%0.2f muted=%t\n", castApplication.DisplayName, castVolume.Level, castVolume.Muted) + outputInfo("Idle (%s), volume=%0.2f muted=%t\n", castApplication.DisplayName, castVolume.Level, castVolume.Muted) } else { metadata := "unknown" if castMedia.Media.Metadata.Title != "" { @@ -138,7 +137,7 @@ func outputStatus(app *application.Application, outputType outputType) { case "x-youtube/video": metadata = fmt.Sprintf("id=\"%s\", %s", castMedia.Media.ContentId, metadata) } - log.Infof(">> %s (%s), %s, time remaining=%.2fs/%.2fs, volume=%0.2f, muted=%t\n", castApplication.DisplayName, castMedia.PlayerState, metadata, castMedia.CurrentTime, castMedia.Media.Duration, castVolume.Level, castVolume.Muted) + outputInfo(">> %s (%s), %s, time remaining=%.2fs/%.2fs, volume=%0.2f, muted=%t\n", castApplication.DisplayName, castMedia.PlayerState, metadata, castMedia.CurrentTime, castMedia.Media.Duration, castVolume.Level, castVolume.Muted) } } }