Skip to content

Commit

Permalink
Standardise command output
Browse files Browse the repository at this point in the history
  • Loading branch information
vishen committed Aug 8, 2021
1 parent 5eab24c commit 59e712c
Show file tree
Hide file tree
Showing 25 changed files with 167 additions and 207 deletions.
6 changes: 4 additions & 2 deletions cmd/httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
}
},
}

Expand Down
22 changes: 9 additions & 13 deletions cmd/load-app.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
package cmd

import (
"fmt"

"github.com/vishen/go-chromecast/ui"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -31,38 +28,37 @@ 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:
runWithUI, _ := cmd.Flags().GetBool("with-ui")
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
},
}

Expand Down
22 changes: 9 additions & 13 deletions cmd/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
package cmd

import (
"fmt"

"github.com/vishen/go-chromecast/ui"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -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")
Expand All @@ -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
},
}

Expand Down
13 changes: 5 additions & 8 deletions cmd/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"net"
"time"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
castdns "github.com/vishen/go-chromecast/dns"
)
Expand All @@ -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
},
}

Expand Down
5 changes: 2 additions & 3 deletions cmd/mute.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package cmd

import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -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
}
},
Expand Down
6 changes: 2 additions & 4 deletions cmd/next.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package cmd

import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -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)
}
},
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package cmd

import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -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
}
},
Expand Down
40 changes: 17 additions & 23 deletions cmd/playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package cmd

import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -25,7 +24,6 @@ import (
"strings"
"time"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/vishen/go-chromecast/ui"
)
Expand All @@ -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")
Expand All @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
},
}

Expand Down
5 changes: 2 additions & 3 deletions cmd/previous.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package cmd

import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -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)
}
},
}
Expand Down
Loading

0 comments on commit 59e712c

Please sign in to comment.