Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

video-ids output format for list cmd #177

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 16 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 errUnhandledFormat(outputFormat)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the flag is handled by addFormatFlag which prints 4 formats. For info cmd, this new format is not implemented. Maybe when info cmd accepts multiple URLs we can extract all video IDs the same way as list cmd does in this PR?

./youtubedr-dev info <videoID> --format video-ids
unhandled output format: video-ids
./youtubedr-dev list <playlistID> --format video-ids
JTRqppn4vNo lhbytXY9gEs 7Y7m3uMRNak GjhRMetsok0 fePN2EFK5bU 7sX44EkgTds sAb-ogxZIAI kv-8SeTfT0k CHvhcM_Rvlo -075jrjea4Q ZgHrrkG175M KK58lRTSn5I iIfgwENiL3o VNNXO4DIHUo

}
writer(w)
return nil
case outputFormatJSON:
encoder := json.NewEncoder(w)
Expand All @@ -58,3 +65,9 @@ type errInvalidFormat string
func (err errInvalidFormat) Error() string {
return fmt.Sprintf("invalid output format: %s", outputFormat)
}

type errUnhandledFormat string

func (err errUnhandledFormat) Error() string {
return fmt.Sprintf("unhandled output format: %s", outputFormat)
}