summaryrefslogtreecommitdiffstats
path: root/commands/list.go
diff options
context:
space:
mode:
Diffstat (limited to 'commands/list.go')
-rw-r--r--commands/list.go279
1 files changed, 118 insertions, 161 deletions
diff --git a/commands/list.go b/commands/list.go
index 4b62c91c5..2f2e29887 100644
--- a/commands/list.go
+++ b/commands/list.go
@@ -1,4 +1,4 @@
-// Copyright 2019 The Hugo Authors. All rights reserved.
+// Copyright 2023 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -14,197 +14,154 @@
package commands
import (
+ "context"
"encoding/csv"
- "os"
- "strconv"
- "strings"
"time"
+ "github.com/bep/simplecobra"
+ "github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/hugolib"
+ "github.com/gohugoio/hugo/resources/page"
"github.com/gohugoio/hugo/resources/resource"
"github.com/spf13/cobra"
- jww "github.com/spf13/jwalterweatherman"
)
-var _ cmder = (*listCmd)(nil)
+// newListCommand creates a new list command and its subcommands.
+func newListCommand() *listCommand {
-type listCmd struct {
- *baseBuilderCmd
-}
-
-func (lc *listCmd) buildSites(config map[string]any) (*hugolib.HugoSites, error) {
- cfgInit := func(c *commandeer) error {
- for key, value := range config {
- c.Set(key, value)
+ list := func(cd *simplecobra.Commandeer, r *rootCommand, createRecord func(page.Page) []string, opts ...any) error {
+ bcfg := hugolib.BuildCfg{SkipRender: true}
+ cfg := config.New()
+ for i := 0; i < len(opts); i += 2 {
+ cfg.Set(opts[i].(string), opts[i+1])
+ }
+ h, err := r.Build(cd, bcfg, cfg)
+ if err != nil {
+ return err
}
- return nil
- }
-
- c, err := initializeConfig(true, true, false, &lc.hugoBuilderCommon, lc, cfgInit)
- if err != nil {
- return nil, err
- }
-
- sites, err := hugolib.NewHugoSites(*c.DepsCfg)
- if err != nil {
- return nil, newSystemError("Error creating sites", err)
- }
-
- if err := sites.Build(hugolib.BuildCfg{SkipRender: true}); err != nil {
- return nil, newSystemError("Error Processing Source Content", err)
- }
- return sites, nil
-}
+ writer := csv.NewWriter(r.Out)
+ defer writer.Flush()
-func (b *commandsBuilder) newListCmd() *listCmd {
- cc := &listCmd{}
+ for _, p := range h.Pages() {
+ if record := createRecord(p); record != nil {
+ if err := writer.Write(record); err != nil {
+ return err
+ }
+ if err != nil {
+ return err
+ }
+ }
+ }
- cmd := &cobra.Command{
- Use: "list",
- Short: "Listing out various types of content",
- Long: `Listing out various types of content.
+ return nil
-List requires a subcommand, e.g. ` + "`hugo list drafts`.",
- RunE: nil,
}
- cmd.AddCommand(
- &cobra.Command{
- Use: "drafts",
- Short: "List all drafts",
- Long: `List all of the drafts in your content directory.`,
- RunE: func(cmd *cobra.Command, args []string) error {
- sites, err := cc.buildSites(map[string]any{"buildDrafts": true})
- if err != nil {
- return newSystemError("Error building sites", err)
- }
+ return &listCommand{
+ commands: []simplecobra.Commander{
+ &simpleCommand{
+ name: "drafts",
+ 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 {
+ if !p.Draft() || p.File().IsZero() {
+ return nil
+ }
+ return []string{
+ p.File().Path(),
+ p.PublishDate().Format(time.RFC3339)}
- for _, p := range sites.Pages() {
- if p.Draft() {
- jww.FEEDBACK.Println(strings.TrimPrefix(p.File().Filename(), sites.WorkingDir+string(os.PathSeparator)))
}
- }
-
- return nil
+ return list(cd, r, createRecord, "buildDrafts", true)
+ },
},
- },
- &cobra.Command{
- Use: "future",
- 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.`,
- RunE: func(cmd *cobra.Command, args []string) error {
- sites, err := cc.buildSites(map[string]any{"buildFuture": true})
- if err != nil {
- return newSystemError("Error building sites", err)
- }
-
- if err != nil {
- return newSystemError("Error building sites", err)
- }
-
- writer := csv.NewWriter(os.Stdout)
- defer writer.Flush()
+ &simpleCommand{
+ name: "future",
+ 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 {
+ if !resource.IsFuture(p) || p.File().IsZero() {
+ return nil
+ }
+ return []string{
+ p.File().Path(),
+ p.PublishDate().Format(time.RFC3339),
+ }
- for _, p := range sites.Pages() {
- if resource.IsFuture(p) {
- err := writer.Write([]string{
- strings.TrimPrefix(p.File().Filename(), sites.WorkingDir+string(os.PathSeparator)),
+ }
+ return list(cd, r, createRecord, "buildFuture", true)
+ },
+ },
+ &simpleCommand{
+ name: "expired",
+ 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 {
+ if !resource.IsExpired(p) || p.File().IsZero() {
+ return nil
+ }
+ return []string{
+ p.File().Path(),
p.PublishDate().Format(time.RFC3339),
- })
- if err != nil {
- return newSystemError("Error writing future posts to stdout", err)
}
+
}
- }
+ return list(cd, r, createRecord, "buildExpired", true)
+ },
+ },
+ &simpleCommand{
+ name: "all",
+ 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),
+ }
- return nil
+ }
+ return list(cd, r, createRecord, "buildDrafts", true, "buildFuture", true, "buildExpired", true)
+ },
},
},
- &cobra.Command{
- Use: "expired",
- Short: "List all posts already expired",
- Long: `List all of the posts in your content directory which has already expired.`,
- RunE: func(cmd *cobra.Command, args []string) error {
- sites, err := cc.buildSites(map[string]any{"buildExpired": true})
- if err != nil {
- return newSystemError("Error building sites", err)
- }
+ }
- if err != nil {
- return newSystemError("Error building sites", err)
- }
+}
- writer := csv.NewWriter(os.Stdout)
- defer writer.Flush()
-
- for _, p := range sites.Pages() {
- if resource.IsExpired(p) {
- err := writer.Write([]string{
- strings.TrimPrefix(p.File().Filename(), sites.WorkingDir+string(os.PathSeparator)),
- p.ExpiryDate().Format(time.RFC3339),
- })
- if err != nil {
- return newSystemError("Error writing expired posts to stdout", err)
- }
- }
- }
+type listCommand struct {
+ commands []simplecobra.Commander
+}
- return nil
- },
- },
- &cobra.Command{
- Use: "all",
- Short: "List all posts",
- Long: `List all of the posts in your content directory, include drafts, future and expired pages.`,
- RunE: func(cmd *cobra.Command, args []string) error {
- sites, err := cc.buildSites(map[string]any{
- "buildExpired": true,
- "buildDrafts": true,
- "buildFuture": true,
- })
- if err != nil {
- return newSystemError("Error building sites", err)
- }
+func (c *listCommand) Commands() []simplecobra.Commander {
+ return c.commands
+}
- writer := csv.NewWriter(os.Stdout)
- defer writer.Flush()
-
- writer.Write([]string{
- "path",
- "slug",
- "title",
- "date",
- "expiryDate",
- "publishDate",
- "draft",
- "permalink",
- })
- for _, p := range sites.Pages() {
- if !p.IsPage() {
- continue
- }
- err := writer.Write([]string{
- strings.TrimPrefix(p.File().Filename(), sites.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(),
- })
- if err != nil {
- return newSystemError("Error writing posts to stdout", err)
- }
- }
+func (c *listCommand) Name() string {
+ return "list"
+}
- return nil
- },
- },
- )
+func (c *listCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args []string) error {
+ // Do nothing.
+ return nil
+}
+
+func (c *listCommand) WithCobraCommand(cmd *cobra.Command) error {
+ cmd.Short = "Listing out various types of content"
+ cmd.Long = `Listing out various types of content.
- cc.baseBuilderCmd = b.newBuilderBasicCmd(cmd)
+List requires a subcommand, e.g. hugo list drafts`
+
+ return nil
+}
- return cc
+func (c *listCommand) Init(cd, runner *simplecobra.Commandeer) error {
+ return nil
}