]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
commands: Make all list commands list what 'all' did before
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 22 May 2023 07:49:58 +0000 (09:49 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 22 May 2023 10:27:19 +0000 (12:27 +0200)
Also, always include the CSV header.

Updates #10953

commands/list.go
testscripts/commands/list.txt

index 05739c7a976743bd06f56b3aadc70683adf1bd2d..6458df875ecdc2bc1b2375386e464212f8470ee3 100644 (file)
@@ -16,6 +16,10 @@ package commands
 import (
        "context"
        "encoding/csv"
+       "os"
+       "path/filepath"
+       "strconv"
+       "strings"
        "time"
 
        "github.com/bep/simplecobra"
@@ -28,7 +32,20 @@ import (
 // newListCommand creates a new list command and its subcommands.
 func newListCommand() *listCommand {
 
-       list := func(cd *simplecobra.Commandeer, r *rootCommand, createRecord func(page.Page) []string, opts ...any) error {
+       createRecord := func(workingDir string, p page.Page) []string {
+               return []string{
+                       filepath.ToSlash(strings.TrimPrefix(p.File().Filename(), workingDir+string(os.PathSeparator))),
+                       p.Slug(),
+                       p.Title(),
+                       p.Date().Format(time.RFC3339),
+                       p.ExpiryDate().Format(time.RFC3339),
+                       p.PublishDate().Format(time.RFC3339),
+                       strconv.FormatBool(p.Draft()),
+                       p.Permalink(),
+               }
+       }
+
+       list := func(cd *simplecobra.Commandeer, r *rootCommand, shouldInclude func(page.Page) bool, opts ...any) error {
                bcfg := hugolib.BuildCfg{SkipRender: true}
                cfg := config.New()
                for i := 0; i < len(opts); i += 2 {
@@ -42,8 +59,20 @@ func newListCommand() *listCommand {
                writer := csv.NewWriter(r.Out)
                defer writer.Flush()
 
+               writer.Write([]string{
+                       "path",
+                       "slug",
+                       "title",
+                       "date",
+                       "expiryDate",
+                       "publishDate",
+                       "draft",
+                       "permalink",
+               })
+
                for _, p := range h.Pages() {
-                       if record := createRecord(p); record != nil {
+                       if shouldInclude(p) {
+                               record := createRecord(h.Conf.BaseConfig().WorkingDir, p)
                                if err := writer.Write(record); err != nil {
                                        return err
                                }
@@ -64,16 +93,14 @@ func newListCommand() *listCommand {
                                short: "List all drafts",
                                long:  `List all of the drafts in your content directory.`,
                                run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
-                                       createRecord := func(p page.Page) []string {
+                                       shouldInclude := func(p page.Page) bool {
                                                if !p.Draft() || p.File().IsZero() {
-                                                       return nil
+                                                       return false
                                                }
-                                               return []string{
-                                                       p.File().Path(),
-                                                       p.PublishDate().Format(time.RFC3339)}
+                                               return true
 
                                        }
-                                       return list(cd, r, createRecord,
+                                       return list(cd, r, shouldInclude,
                                                "buildDrafts", true,
                                                "buildFuture", true,
                                                "buildExpired", true,
@@ -85,17 +112,14 @@ func newListCommand() *listCommand {
                                short: "List all posts dated in the future",
                                long:  `List all of the posts in your content directory which will be posted in the future.`,
                                run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
-                                       createRecord := func(p page.Page) []string {
+                                       shouldInclude := func(p page.Page) bool {
                                                if !resource.IsFuture(p) || p.File().IsZero() {
-                                                       return nil
-                                               }
-                                               return []string{
-                                                       p.File().Path(),
-                                                       p.PublishDate().Format(time.RFC3339),
+                                                       return false
                                                }
+                                               return true
 
                                        }
-                                       return list(cd, r, createRecord,
+                                       return list(cd, r, shouldInclude,
                                                "buildFuture", true,
                                                "buildDrafts", true,
                                        )
@@ -106,17 +130,13 @@ func newListCommand() *listCommand {
                                short: "List all posts already expired",
                                long:  `List all of the posts in your content directory which has already expired.`,
                                run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
-                                       createRecord := func(p page.Page) []string {
+                                       shouldInclude := func(p page.Page) bool {
                                                if !resource.IsExpired(p) || p.File().IsZero() {
-                                                       return nil
+                                                       return false
                                                }
-                                               return []string{
-                                                       p.File().Path(),
-                                                       p.PublishDate().Format(time.RFC3339),
-                                               }
-
+                                               return true
                                        }
-                                       return list(cd, r, createRecord,
+                                       return list(cd, r, shouldInclude,
                                                "buildExpired", true,
                                                "buildDrafts", true,
                                        )
@@ -127,17 +147,10 @@ func newListCommand() *listCommand {
                                short: "List all posts",
                                long:  `List all of the posts in your content directory, include drafts, future and expired pages.`,
                                run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
-                                       createRecord := func(p page.Page) []string {
-                                               if p.File().IsZero() {
-                                                       return nil
-                                               }
-                                               return []string{
-                                                       p.File().Path(),
-                                                       p.PublishDate().Format(time.RFC3339),
-                                               }
-
+                                       shouldInclude := func(p page.Page) bool {
+                                               return !p.File().IsZero()
                                        }
-                                       return list(cd, r, createRecord, "buildDrafts", true, "buildFuture", true, "buildExpired", true)
+                                       return list(cd, r, shouldInclude, "buildDrafts", true, "buildFuture", true, "buildExpired", true)
                                },
                        },
                },
index 2e64389fa7eae1372100ad32e14aa3a7c60fa320..42d74cec7502ccb1876dc197ea070908995019ca 100644 (file)
@@ -2,32 +2,43 @@
 
 hugo list drafts
 ! stderr .
-stdout 'draft.md,2019-01-01T00:00:00Z'
-stdout 'draftexpired.md,2018-01-01T00:00:00Z'
-stdout 'draftfuture.md,2030-01-01T00:00:00Z'
+stdout 'path,slug,title,date,expiryDate,publishDate,draft,permalink'
+stdout 'content/draft.md,draft,The Draft,2019-01-01T00:00:00Z,2032-01-01T00:00:00Z,2018-01-01T00:00:00Z,true,https://example.org/draft/'
+stdout 'draftexpired.md'
+stdout 'draftfuture.md'
+! stdout '/expired.md'
 
 hugo list future
-stdout 'future.md,2030-01-01T00:00:00Z'
-stdout 'draftfuture.md,2030-01-01T00:00:00Z'
+stdout 'path,slug,title,date,expiryDate,publishDate,draft,permalink'
+stdout 'future.md'
+stdout 'draftfuture.md'
+! stdout 'expired.md'
 
 hugo list expired
-stdout 'expired.md,2018-01-01T00:00:00Z'
-stdout 'draftexpired.md,2018-01-01T00:00:00Z'
+stdout 'path,slug,title,date,expiryDate,publishDate,draft,permalink'
+stdout 'expired.md'
+stdout 'draftexpired.md'
+! stdout 'future.md'
 
 hugo list all
-stdout 'future.md,2030-01-01T00:00:00Z'
-stdout 'draft.md,2019-01-01T00:00:00Z'
-stdout 'expired.md,2018-01-01T00:00:00Z'
-stdout 'draftexpired.md,2018-01-01T00:00:00Z'
-stdout 'draftfuture.md,2030-01-01T00:00:00Z'
+stdout 'path,slug,title,date,expiryDate,publishDate,draft,permalink'
+stdout 'future.md'
+stdout 'draft.md'
+stdout 'expired.md'
+stdout 'draftexpired.md'
+stdout 'draftfuture.md'
 
 -- hugo.toml --
 baseURL = "https://example.org/"
 disableKinds = ["taxonomy", "term"]
 -- content/draft.md --
 ---
+title: "The Draft"
+slug: "draft"
 draft: true
 date: 2019-01-01
+expiryDate: 2032-01-01
+publishDate: 2018-01-01
 ---
 -- content/expired.md --
 ---