Skip to content

Commit 59e712c

Browse files
committed
Standardise command output
1 parent 5eab24c commit 59e712c

25 files changed

+167
-207
lines changed

cmd/httpserver.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var httpserverCmd = &cobra.Command{
2525
Short: "Start the HTTP server",
2626
Long: `Start the HTTP server which provides an HTTP
2727
api to control chromecast devices on a network.`,
28-
RunE: func(cmd *cobra.Command, args []string) error {
28+
Run: func(cmd *cobra.Command, args []string) {
2929

3030
addr, _ := cmd.Flags().GetString("http-addr")
3131
port, _ := cmd.Flags().GetString("http-port")
@@ -35,7 +35,9 @@ api to control chromecast devices on a network.`,
3535
verbose, _ := cmd.Flags().GetBool("verbose")
3636
debug, _ := cmd.Flags().GetBool("debug")
3737

38-
return http.NewHandler(verbose || debug).Serve(addr + ":" + port)
38+
if err := http.NewHandler(verbose || debug).Serve(addr + ":" + port); err != nil {
39+
exit("unable to run http server: %v\n", err)
40+
}
3941
},
4042
}
4143

cmd/load-app.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@
1515
package cmd
1616

1717
import (
18-
"fmt"
19-
2018
"github.com/vishen/go-chromecast/ui"
2119

22-
log "github.com/sirupsen/logrus"
2320
"github.com/spf13/cobra"
2421
)
2522

@@ -31,38 +28,37 @@ var loadAppCmd = &cobra.Command{
3128
the chromecast receiver app to be specified. An older list can be found
3229
here https://gist.github.com/jloutsenhizer/8855258.
3330
`,
34-
RunE: func(cmd *cobra.Command, args []string) error {
31+
Run: func(cmd *cobra.Command, args []string) {
3532
if len(args) != 2 {
36-
return fmt.Errorf("requires exactly two arguments")
33+
exit("requires exactly two arguments\n")
3734
}
3835
app, err := castApplication(cmd, args)
3936
if err != nil {
40-
log.WithError(err).Info("unable to get cast application")
41-
return nil
37+
exit("unable to get cast application: %v\n", err)
4238
}
4339

4440
// Optionally run a UI when playing this media:
4541
runWithUI, _ := cmd.Flags().GetBool("with-ui")
4642
if runWithUI {
4743
go func() {
4844
if err := app.LoadApp(args[0], args[1]); err != nil {
49-
log.WithError(err).Fatal("unable to load media")
45+
exit("unable to load media: %v\n", err)
5046
}
5147
}()
5248

5349
ccui, err := ui.NewUserInterface(app)
5450
if err != nil {
55-
log.WithError(err).Fatal("unable to prepare a new user-interface")
51+
exit("unable to prepare a new user-interface: %v\n", err)
52+
}
53+
if err := ccui.Run(); err != nil {
54+
exit("unable to run ui: %v\n", err)
5655
}
57-
return ccui.Run()
5856
}
5957

6058
// Otherwise just run in CLI mode:
6159
if err := app.LoadApp(args[0], args[1]); err != nil {
62-
log.WithError(err).Info("unable to load media")
63-
return nil
60+
exit("unable to load media: %v\n", err)
6461
}
65-
return nil
6662
},
6763
}
6864

cmd/load.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@
1515
package cmd
1616

1717
import (
18-
"fmt"
19-
2018
"github.com/vishen/go-chromecast/ui"
2119

22-
log "github.com/sirupsen/logrus"
2320
"github.com/spf13/cobra"
2421
)
2522

@@ -34,14 +31,13 @@ chromecast if it is a local file, otherwise it will load the url.
3431
If the media file is an unplayable media type by the chromecast, this
3532
will attempt to transcode the media file to mp4 using ffmpeg. This requires
3633
that ffmpeg is installed.`,
37-
RunE: func(cmd *cobra.Command, args []string) error {
34+
Run: func(cmd *cobra.Command, args []string) {
3835
if len(args) != 1 {
39-
return fmt.Errorf("requires exactly one argument, should be the media file to load")
36+
exit("requires exactly one argument, should be the media file to load\n")
4037
}
4138
app, err := castApplication(cmd, args)
4239
if err != nil {
43-
log.WithError(err).Println("unable to get cast application")
44-
return nil
40+
exit("unable to get cast application: %v\n", err)
4541
}
4642

4743
contentType, _ := cmd.Flags().GetString("content-type")
@@ -53,23 +49,23 @@ that ffmpeg is installed.`,
5349
if runWithUI {
5450
go func() {
5551
if err := app.Load(args[0], contentType, transcode, detach, false); err != nil {
56-
log.WithError(err).Fatal("unable to load media")
52+
exit("unable to load media: %v\n", err)
5753
}
5854
}()
5955

6056
ccui, err := ui.NewUserInterface(app)
6157
if err != nil {
62-
log.WithError(err).Fatal("unable to prepare a new user-interface")
58+
exit("unable to prepare a new user-interface: %v\n", err)
59+
}
60+
if err := ccui.Run(); err != nil {
61+
exit("unable to run ui: %v\n", err)
6362
}
64-
return ccui.Run()
6563
}
6664

6765
// Otherwise just run in CLI mode:
6866
if err := app.Load(args[0], contentType, transcode, detach, false); err != nil {
69-
log.WithError(err).Println("unable to load media")
70-
return nil
67+
exit("unable to load media: %v\n", err)
7168
}
72-
return nil
7369
},
7470
}
7571

cmd/ls.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"net"
2020
"time"
2121

22-
log "github.com/sirupsen/logrus"
2322
"github.com/spf13/cobra"
2423
castdns "github.com/vishen/go-chromecast/dns"
2524
)
@@ -28,32 +27,30 @@ import (
2827
var lsCmd = &cobra.Command{
2928
Use: "ls",
3029
Short: "List devices",
31-
RunE: func(cmd *cobra.Command, args []string) error {
30+
Run: func(cmd *cobra.Command, args []string) {
3231
ifaceName, _ := cmd.Flags().GetString("iface")
3332
dnsTimeoutSeconds, _ := cmd.Flags().GetInt("dns-timeout")
3433
var iface *net.Interface
3534
var err error
3635
if ifaceName != "" {
3736
if iface, err = net.InterfaceByName(ifaceName); err != nil {
38-
log.Fatalf("unable to find interface %q: %v", ifaceName, err)
37+
exit("unable to find interface %q: %v\n", ifaceName, err)
3938
}
4039
}
4140
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(dnsTimeoutSeconds))
4241
defer cancel()
4342
castEntryChan, err := castdns.DiscoverCastDNSEntries(ctx, iface)
4443
if err != nil {
45-
log.WithError(err).Error("unable to discover chromecast devices")
46-
return nil
44+
exit("unable to discover chromecast devices: %v\n", err)
4745
}
4846
i := 1
4947
for d := range castEntryChan {
50-
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)
48+
outputInfo("%d) device=%q device_name=%q address=\"%s:%d\" uuid=%q\n", i, d.Device, d.DeviceName, d.AddrV4, d.Port, d.UUID)
5149
i++
5250
}
5351
if i == 1 {
54-
log.Error("no cast devices found on network\n")
52+
outputError("no cast devices found on network\n")
5553
}
56-
return nil
5754
},
5855
}
5956

cmd/mute.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package cmd
1616

1717
import (
18-
"github.com/sirupsen/logrus"
1918
"github.com/spf13/cobra"
2019
)
2120

@@ -26,11 +25,11 @@ var muteCmd = &cobra.Command{
2625
Run: func(cmd *cobra.Command, args []string) {
2726
app, err := castApplication(cmd, args)
2827
if err != nil {
29-
logrus.Printf("unable to get cast application: %v\n", err)
28+
exit("unable to get cast application: %v\n", err)
3029
return
3130
}
3231
if err := app.SetMuted(true); err != nil {
33-
logrus.Printf("unable to mute cast application: %v\n", err)
32+
exit("unable to mute cast application: %v\n", err)
3433
return
3534
}
3635
},

cmd/next.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package cmd
1616

1717
import (
18-
"github.com/sirupsen/logrus"
1918
"github.com/spf13/cobra"
2019
)
2120

@@ -26,11 +25,10 @@ var nextCmd = &cobra.Command{
2625
Run: func(cmd *cobra.Command, args []string) {
2726
app, err := castApplication(cmd, args)
2827
if err != nil {
29-
logrus.Printf("unable to get cast application: %v\n", err)
30-
return
28+
exit("unable to get cast application: %v\n", err)
3129
}
3230
if err := app.Next(); err != nil {
33-
logrus.Printf("unable to play next media: %v\n", err)
31+
exit("unable to play next media: %v\n", err)
3432
}
3533
},
3634
}

cmd/pause.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package cmd
1616

1717
import (
18-
"github.com/sirupsen/logrus"
1918
"github.com/spf13/cobra"
2019
)
2120

@@ -26,11 +25,11 @@ var pauseCmd = &cobra.Command{
2625
Run: func(cmd *cobra.Command, args []string) {
2726
app, err := castApplication(cmd, args)
2827
if err != nil {
29-
logrus.Printf("unable to get cast application: %v\n", err)
28+
exit("unable to get cast application: %v\n", err)
3029
return
3130
}
3231
if err := app.Pause(); err != nil {
33-
logrus.Printf("unable to pause cast application: %v\n", err)
32+
exit("unable to pause cast application: %v\n", err)
3433
return
3534
}
3635
},

cmd/playlist.go

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package cmd
1616

1717
import (
1818
"bufio"
19-
"fmt"
2019
"io/ioutil"
2120
"os"
2221
"path/filepath"
@@ -25,7 +24,6 @@ import (
2524
"strings"
2625
"time"
2726

28-
"github.com/sirupsen/logrus"
2927
"github.com/spf13/cobra"
3028
"github.com/vishen/go-chromecast/ui"
3129
)
@@ -46,21 +44,18 @@ chromecast.
4644
If the media file is an unplayable media type by the chromecast, this
4745
will attempt to transcode the media file to mp4 using ffmpeg. This requires
4846
that ffmpeg is installed.`,
49-
RunE: func(cmd *cobra.Command, args []string) error {
47+
Run: func(cmd *cobra.Command, args []string) {
5048
if len(args) != 1 {
51-
return fmt.Errorf("requires exactly one argument, should be the folder to play media from")
49+
exit("requires exactly one argument, should be the folder to play media from\n")
5250
}
5351
if fileInfo, err := os.Stat(args[0]); err != nil {
54-
logrus.Printf("unable to find %q: %v\n", args[0], err)
55-
return nil
52+
exit("unable to find %q: %v\n", args[0], err)
5653
} else if !fileInfo.Mode().IsDir() {
57-
logrus.Printf("%q is not a directory\n", args[0])
58-
return nil
54+
exit("%q is not a directory\n", args[0])
5955
}
6056
app, err := castApplication(cmd, args)
6157
if err != nil {
62-
logrus.Printf("unable to get cast application: %v\n", err)
63-
return nil
58+
exit("unable to get cast application: %v\n", err)
6459
}
6560

6661
contentType, _ := cmd.Flags().GetString("content-type")
@@ -70,8 +65,7 @@ that ffmpeg is installed.`,
7065
selection, _ := cmd.Flags().GetBool("select")
7166
files, err := ioutil.ReadDir(args[0])
7267
if err != nil {
73-
logrus.Printf("unable to list files from %q: %v", args[0], err)
74-
return nil
68+
exit("unable to list files from %q: %v\n", args[0], err)
7569
}
7670
filesToPlay := make([]mediaFile, 0, len(files))
7771
for _, f := range files {
@@ -144,21 +138,21 @@ that ffmpeg is installed.`,
144138

145139
indexToPlayFrom := 0
146140
if selection {
147-
logrus.Println("Will play the following items, select where to start from:")
141+
outputInfo("Will play the following items, select where to start from:")
148142
for i, f := range filenames {
149143
lastPlayed := "never"
150144
if lp, ok := app.PlayedItems()[f]; ok {
151145
t := time.Unix(lp.Started, 0)
152146
lastPlayed = t.String()
153147
}
154-
logrus.Printf("%d) %s: last played %q\n", i+1, f, lastPlayed)
148+
outputInfo("%d) %s: last played %q\n", i+1, f, lastPlayed)
155149
}
156150
reader := bufio.NewReader(os.Stdin)
157151
for {
158-
logrus.Printf("Enter selection: ")
152+
outputInfo("Enter selection: ")
159153
text, err := reader.ReadString('\n')
160154
if err != nil {
161-
logrus.Printf("error reading console: %v\n", err)
155+
outputError("reading console: %v\n", err)
162156
continue
163157
}
164158
i, err := strconv.Atoi(strings.TrimSpace(text))
@@ -199,29 +193,29 @@ that ffmpeg is installed.`,
199193
for _, f := range filenames[indexToPlayFrom:] {
200194
s += "- " + f + "\n"
201195
}
202-
logrus.Print(s)
196+
outputInfo(s)
203197

204198
// Optionally run a UI when playing this media:
205199
runWithUI, _ := cmd.Flags().GetBool("with-ui")
206200
if runWithUI {
207201
go func() {
208202
if err := app.QueueLoad(filenames[indexToPlayFrom:], contentType, transcode); err != nil {
209-
logrus.WithError(err).Fatal("unable to play playlist on cast application")
203+
exit("unable to play playlist on cast application: %v\n", err)
210204
}
211205
}()
212206

213207
ccui, err := ui.NewUserInterface(app)
214208
if err != nil {
215-
logrus.WithError(err).Fatal("unable to prepare a new user-interface")
209+
exit("unable to prepare a new user-interface: %v\n", err)
210+
}
211+
if err := ccui.Run(); err != nil {
212+
exit("unable to run ui: %v\n", err)
216213
}
217-
return ccui.Run()
218214
}
219215

220216
if err := app.QueueLoad(filenames[indexToPlayFrom:], contentType, transcode); err != nil {
221-
logrus.Printf("unable to play playlist on cast application: %v\n", err)
222-
return nil
217+
exit("unable to play playlist on cast application: %v\n", err)
223218
}
224-
return nil
225219
},
226220
}
227221

cmd/previous.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package cmd
1616

1717
import (
18-
"github.com/sirupsen/logrus"
1918
"github.com/spf13/cobra"
2019
)
2120

@@ -26,11 +25,11 @@ var previousCmd = &cobra.Command{
2625
Run: func(cmd *cobra.Command, args []string) {
2726
app, err := castApplication(cmd, args)
2827
if err != nil {
29-
logrus.Printf("unable to get cast application: %v\n", err)
28+
exit("unable to get cast application: %v\n", err)
3029
return
3130
}
3231
if err := app.Previous(); err != nil {
33-
logrus.Printf("unable to play previous media: %v\n", err)
32+
exit("unable to play previous media: %v\n", err)
3433
}
3534
},
3635
}

0 commit comments

Comments
 (0)