2023-01-04 12:24:36 -05:00
|
|
|
// Copyright 2023 The Hugo Authors. All rights reserved.
|
2014-11-19 16:24:30 -05:00
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-11-19 16:24:30 -05:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-11-19 16:24:30 -05:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2023-01-04 12:24:36 -05:00
|
|
|
"context"
|
2019-02-20 20:34:32 -05:00
|
|
|
"encoding/csv"
|
2023-05-22 03:49:58 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-02-20 20:34:32 -05:00
|
|
|
"time"
|
2014-11-19 16:24:30 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
"github.com/bep/simplecobra"
|
|
|
|
"github.com/gohugoio/hugo/config"
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/hugolib"
|
2023-01-04 12:24:36 -05:00
|
|
|
"github.com/gohugoio/hugo/resources/page"
|
2019-01-02 06:33:26 -05:00
|
|
|
"github.com/gohugoio/hugo/resources/resource"
|
2014-11-19 16:24:30 -05:00
|
|
|
)
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
// newListCommand creates a new list command and its subcommands.
|
|
|
|
func newListCommand() *listCommand {
|
2015-08-04 05:15:12 -04:00
|
|
|
|
2023-05-22 03:49:58 -04:00
|
|
|
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 {
|
2023-01-04 12:24:36 -05:00
|
|
|
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
|
2019-05-04 10:21:21 -04:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
writer := csv.NewWriter(r.Out)
|
|
|
|
defer writer.Flush()
|
2019-05-04 10:21:21 -04:00
|
|
|
|
2023-05-22 03:49:58 -04:00
|
|
|
writer.Write([]string{
|
|
|
|
"path",
|
|
|
|
"slug",
|
|
|
|
"title",
|
|
|
|
"date",
|
|
|
|
"expiryDate",
|
|
|
|
"publishDate",
|
|
|
|
"draft",
|
|
|
|
"permalink",
|
|
|
|
})
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
for _, p := range h.Pages() {
|
2023-05-22 03:49:58 -04:00
|
|
|
if shouldInclude(p) {
|
|
|
|
record := createRecord(h.Conf.BaseConfig().WorkingDir, p)
|
2023-01-04 12:24:36 -05:00
|
|
|
if err := writer.Write(record); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-19 16:24:30 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
return nil
|
2014-11-19 16:24:30 -05:00
|
|
|
|
2020-01-31 03:09:11 -05:00
|
|
|
}
|
2018-04-09 14:42:08 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
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 {
|
2023-05-22 03:49:58 -04:00
|
|
|
shouldInclude := func(p page.Page) bool {
|
2023-01-04 12:24:36 -05:00
|
|
|
if !p.Draft() || p.File().IsZero() {
|
2023-05-22 03:49:58 -04:00
|
|
|
return false
|
2023-01-04 12:24:36 -05:00
|
|
|
}
|
2023-05-22 03:49:58 -04:00
|
|
|
return true
|
2018-04-09 14:42:08 -04:00
|
|
|
|
|
|
|
}
|
2023-05-22 03:49:58 -04:00
|
|
|
return list(cd, r, shouldInclude,
|
2023-05-21 04:16:25 -04:00
|
|
|
"buildDrafts", true,
|
|
|
|
"buildFuture", true,
|
|
|
|
"buildExpired", true,
|
|
|
|
)
|
2023-01-04 12:24:36 -05:00
|
|
|
},
|
2018-04-09 14:42:08 -04:00
|
|
|
},
|
2023-01-04 12:24:36 -05:00
|
|
|
&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 {
|
2023-05-22 03:49:58 -04:00
|
|
|
shouldInclude := func(p page.Page) bool {
|
2023-01-04 12:24:36 -05:00
|
|
|
if !resource.IsFuture(p) || p.File().IsZero() {
|
2023-05-22 03:49:58 -04:00
|
|
|
return false
|
2023-01-04 12:24:36 -05:00
|
|
|
}
|
2023-05-22 03:49:58 -04:00
|
|
|
return true
|
2019-02-20 20:34:32 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
}
|
2023-05-22 03:49:58 -04:00
|
|
|
return list(cd, r, shouldInclude,
|
2023-05-21 04:16:25 -04:00
|
|
|
"buildFuture", true,
|
|
|
|
"buildDrafts", true,
|
|
|
|
)
|
2023-01-04 12:24:36 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&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 {
|
2023-05-22 03:49:58 -04:00
|
|
|
shouldInclude := func(p page.Page) bool {
|
2023-01-04 12:24:36 -05:00
|
|
|
if !resource.IsExpired(p) || p.File().IsZero() {
|
2023-05-22 03:49:58 -04:00
|
|
|
return false
|
2023-01-04 12:24:36 -05:00
|
|
|
}
|
2023-05-22 03:49:58 -04:00
|
|
|
return true
|
2018-04-09 14:42:08 -04:00
|
|
|
}
|
2023-05-22 03:49:58 -04:00
|
|
|
return list(cd, r, shouldInclude,
|
2023-05-21 04:16:25 -04:00
|
|
|
"buildExpired", true,
|
|
|
|
"buildDrafts", true,
|
|
|
|
)
|
2023-01-04 12:24:36 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&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 {
|
2023-05-22 03:49:58 -04:00
|
|
|
shouldInclude := func(p page.Page) bool {
|
|
|
|
return !p.File().IsZero()
|
2023-01-04 12:24:36 -05:00
|
|
|
}
|
2023-05-22 03:49:58 -04:00
|
|
|
return list(cd, r, shouldInclude, "buildDrafts", true, "buildFuture", true, "buildExpired", true)
|
2023-01-04 12:24:36 -05:00
|
|
|
},
|
2018-04-09 14:42:08 -04:00
|
|
|
},
|
|
|
|
},
|
2023-01-04 12:24:36 -05:00
|
|
|
}
|
2016-08-05 10:11:03 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
}
|
2022-04-26 13:57:04 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
type listCommand struct {
|
|
|
|
commands []simplecobra.Commander
|
|
|
|
}
|
2016-05-11 10:11:23 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *listCommand) Commands() []simplecobra.Commander {
|
|
|
|
return c.commands
|
|
|
|
}
|
2019-05-04 10:21:21 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *listCommand) Name() string {
|
|
|
|
return "list"
|
|
|
|
}
|
2019-05-04 10:21:21 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *listCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args []string) error {
|
|
|
|
// Do nothing.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-17 12:45:23 -04:00
|
|
|
func (c *listCommand) Init(cd *simplecobra.Commandeer) error {
|
|
|
|
cmd := cd.CobraCommand
|
2023-01-04 12:24:36 -05:00
|
|
|
cmd.Short = "Listing out various types of content"
|
|
|
|
cmd.Long = `Listing out various types of content.
|
2016-05-11 10:11:23 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
List requires a subcommand, e.g. hugo list drafts`
|
|
|
|
|
2023-06-26 14:20:24 -04:00
|
|
|
cmd.RunE = nil
|
2023-01-04 12:24:36 -05:00
|
|
|
return nil
|
|
|
|
}
|
2016-05-11 10:11:23 -04:00
|
|
|
|
2023-05-17 12:45:23 -04:00
|
|
|
func (c *listCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
|
2023-01-04 12:24:36 -05:00
|
|
|
return nil
|
2016-05-11 10:11:23 -04:00
|
|
|
}
|