diff options
author | Ruslan Nasonov <rus.nasonov@gmail.com> | 2019-05-04 21:21:21 +0700 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-05-25 00:09:51 +0200 |
commit | 5b4b8bb3c1ecb30e7a38ed44eb795f1d972cd320 (patch) | |
tree | 7c8bf6446788bff324e83137e3ec543fcc29aec9 /commands/list.go | |
parent | 2278b0eb02ccdd3c2d4358d39074767d33fecb71 (diff) |
commands: Create new 'hugo list all' command
New:
- command `hugo list all`, return all posts meta in csv format
Refactoring:
- move common parts in commands/list.go to function `buildSites`
- change way to detect path to content
See #5904
Diffstat (limited to 'commands/list.go')
-rw-r--r-- | commands/list.go | 151 |
1 files changed, 94 insertions, 57 deletions
diff --git a/commands/list.go b/commands/list.go index bdf34663f..f233ce62c 100644 --- a/commands/list.go +++ b/commands/list.go @@ -16,7 +16,8 @@ package commands import ( "encoding/csv" "os" - "path/filepath" + "strconv" + "strings" "time" "github.com/gohugoio/hugo/hugolib" @@ -32,6 +33,32 @@ type listCmd struct { *baseCmd } +func (lc *listCmd) buildSites(config map[string]interface{}) (*hugolib.HugoSites, error) { + cfgInit := func(c *commandeer) error { + for key, value := range config { + c.Set(key, value) + } + return nil + } + + c, err := initializeConfig(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 +} + func newListCmd() *listCmd { cc := &listCmd{} @@ -50,59 +77,30 @@ List requires a subcommand, e.g. ` + "`hugo list drafts`.", Short: "List all drafts", Long: `List all of the drafts in your content directory.`, RunE: func(cmd *cobra.Command, args []string) error { - cfgInit := func(c *commandeer) error { - c.Set("buildDrafts", true) - return nil - } - c, err := initializeConfig(true, false, &cc.hugoBuilderCommon, cc, cfgInit) - if err != nil { - return err - } - - sites, err := hugolib.NewHugoSites(*c.DepsCfg) + sites, err := cc.buildSites(map[string]interface{}{"buildDrafts": true}) if err != nil { - return newSystemError("Error creating sites", err) - } - - if err := sites.Build(hugolib.BuildCfg{SkipRender: true}); err != nil { - return newSystemError("Error Processing Source Content", err) + return newSystemError("Error building sites", err) } for _, p := range sites.Pages() { if p.Draft() { - jww.FEEDBACK.Println(filepath.Join(p.File().Dir(), p.File().LogicalName())) + jww.FEEDBACK.Println(strings.TrimPrefix(p.File().Filename(), sites.WorkingDir+string(os.PathSeparator))) } - } return nil - }, }, &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.`, + 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 { - cfgInit := func(c *commandeer) error { - c.Set("buildFuture", true) - return nil - } - c, err := initializeConfig(true, false, &cc.hugoBuilderCommon, cc, cfgInit) - if err != nil { - return err - } - - sites, err := hugolib.NewHugoSites(*c.DepsCfg) + sites, err := cc.buildSites(map[string]interface{}{"buildFuture": true}) if err != nil { - return newSystemError("Error creating sites", err) - } - - if err := sites.Build(hugolib.BuildCfg{SkipRender: true}); err != nil { - return newSystemError("Error Processing Source Content", err) + return newSystemError("Error building sites", err) } writer := csv.NewWriter(os.Stdout) @@ -110,7 +108,10 @@ posted in the future.`, for _, p := range sites.Pages() { if resource.IsFuture(p) { - err := writer.Write([]string{filepath.Join(p.File().Dir(), p.File().LogicalName()), p.PublishDate().Format(time.RFC3339)}) + err := writer.Write([]string{ + strings.TrimPrefix(p.File().Filename(), sites.WorkingDir+string(os.PathSeparator)), + p.PublishDate().Format(time.RFC3339), + }) if err != nil { return newSystemError("Error writing future posts to stdout", err) } @@ -118,32 +119,17 @@ posted in the future.`, } return nil - }, }, &cobra.Command{ Use: "expired", Short: "List all posts already expired", - Long: `List all of the posts in your content directory which has already -expired.`, + Long: `List all of the posts in your content directory which has already expired.`, RunE: func(cmd *cobra.Command, args []string) error { - cfgInit := func(c *commandeer) error { - c.Set("buildExpired", true) - return nil - } - c, err := initializeConfig(true, false, &cc.hugoBuilderCommon, cc, cfgInit) - if err != nil { - return err - } - - sites, err := hugolib.NewHugoSites(*c.DepsCfg) + sites, err := cc.buildSites(map[string]interface{}{"buildExpired": true}) if err != nil { - return newSystemError("Error creating sites", err) - } - - if err := sites.Build(hugolib.BuildCfg{SkipRender: true}); err != nil { - return newSystemError("Error Processing Source Content", err) + return newSystemError("Error building sites", err) } writer := csv.NewWriter(os.Stdout) @@ -151,16 +137,67 @@ expired.`, for _, p := range sites.Pages() { if resource.IsExpired(p) { - err := writer.Write([]string{filepath.Join(p.File().Dir(), p.File().LogicalName()), p.ExpiryDate().Format(time.RFC3339)}) + 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) } - } } 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]interface{}{ + "buildExpired": true, + "buildDrafts": true, + "buildFuture": true, + }) + + if err != nil { + return newSystemError("Error building sites", err) + } + + 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) + } + } + + return nil }, }, ) |