Skip to content

Commit

Permalink
add video-ids output format for list cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
itscaro committed Apr 21, 2021
1 parent aeec2da commit 3c1d282
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
6 changes: 4 additions & 2 deletions cmd/youtubedr/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ var infoCmd = &cobra.Command{
})
}

exitOnError(writeOutput(os.Stdout, &videoInfo, func(w io.Writer) {
writeInfoOutput(w, &videoInfo)
exitOnError(writeOutput(os.Stdout, &videoInfo, map[string]outputWriter{
outputFormatPlain: func(w io.Writer) {
writeInfoOutput(w, &videoInfo)
},
}))
},
}
Expand Down
18 changes: 16 additions & 2 deletions cmd/youtubedr/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"strings"

"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -41,13 +42,26 @@ var (
})
}

exitOnError(writeOutput(os.Stdout, &playlistInfo, func(w io.Writer) {
writePlaylistOutput(w, &playlistInfo)
exitOnError(writeOutput(os.Stdout, &playlistInfo, map[string]outputWriter{
outputFormatPlain: func(w io.Writer) {
writePlaylistOutput(w, &playlistInfo)
},
outputVideoIds: func(w io.Writer) {
writePlaylistVideoIdsOutput(w, &playlistInfo)
},
}))
},
}
)

func writePlaylistVideoIdsOutput(w io.Writer, info *PlaylistInfo) {
var ids []string
for _, v := range info.Videos {
ids = append(ids, v.ID)
}
fmt.Println(strings.Join(ids, " "))
}

func writePlaylistOutput(w io.Writer, info *PlaylistInfo) {
fmt.Println("Title: ", info.Title)
fmt.Println("Author: ", info.Author)
Expand Down
13 changes: 10 additions & 3 deletions cmd/youtubedr/output_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import (
var outputFormat string

const (
outputVideoIds = "video-ids"
outputFormatPlain = "plain"
outputFormatJSON = "json"
outputFormatXML = "xml"
)

var outputFormats = []string{outputFormatPlain, outputFormatJSON, outputFormatXML}
var outputFormats = []string{outputFormatPlain, outputFormatJSON, outputFormatXML, outputVideoIds}

func addFormatFlag(flagSet *pflag.FlagSet) {
flagSet.StringVarP(&outputFormat, "format", "f", outputFormatPlain, "The output format ("+strings.Join(outputFormats, "/")+")")
Expand All @@ -37,10 +38,16 @@ func checkOutputFormat() error {

type outputWriter func(w io.Writer)

func writeOutput(w io.Writer, v interface{}, plainWriter outputWriter) error {
func writeOutput(w io.Writer, v interface{}, writers map[string]outputWriter) error {
switch outputFormat {
case outputVideoIds:
fallthrough
case outputFormatPlain:
plainWriter(w)
writer, ok := writers[outputFormat]
if !ok {
return errInvalidFormat(outputFormat)
}
writer(w)
return nil
case outputFormatJSON:
encoder := json.NewEncoder(w)
Expand Down

0 comments on commit 3c1d282

Please sign in to comment.