Skip to content

Commit

Permalink
feat(ls): Walk file tree when a dir is given
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Sep 3, 2024
1 parent bf64343 commit 2c09977
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
24 changes: 18 additions & 6 deletions cmd/ls/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package ls

import (
"fmt"
"io/fs"
"path/filepath"
"text/tabwriter"
"time"

"github.com/dustin/go-humanize"
"github.com/gabe565/ascii-movie/internal/movie"
"github.com/gabe565/ascii-movie/internal/util"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
Expand All @@ -29,14 +32,23 @@ func NewCommand() *cobra.Command {
func run(cmd *cobra.Command, args []string) error {
movieInfos := make([]movie.Info, 0, len(args))

if len(args) > 0 {
if len(args) != 0 {
for _, arg := range args {
movieInfo, err := movie.GetInfo(nil, arg)
if err != nil {
log.Warn().Err(err).Str("path", arg).Msg("Failed to get movie info")
continue
if err := filepath.WalkDir(arg, func(path string, d fs.DirEntry, err error) error {
if err != nil || d.IsDir() || !util.HasMovieExt(path) {
return err
}

movieInfo, err := movie.GetInfo(nil, path)
if err != nil {
log.Warn().Err(err).Str("path", arg).Msg("Failed to get movie info")
return nil
}
movieInfos = append(movieInfos, movieInfo)
return nil
}); err != nil {
return err
}
movieInfos = append(movieInfos, movieInfo)
}
} else {
var err error
Expand Down
9 changes: 9 additions & 0 deletions internal/util/filepath.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package util

import (
"strings"
)

func HasMovieExt(path string) bool {
return strings.HasSuffix(path, ".txt") || strings.HasSuffix(path, ".txt.gz")
}

0 comments on commit 2c09977

Please sign in to comment.