summaryrefslogtreecommitdiffstats
path: root/commands/list.go
blob: b90a357f8ef616872099d788f6e8e32cb131295d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright 2024 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.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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 (
	"context"
	"encoding/csv"
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"time"

	"github.com/bep/simplecobra"
	"github.com/gohugoio/hugo/hugolib"
	"github.com/gohugoio/hugo/resources/page"
	"github.com/gohugoio/hugo/resources/resource"
	"github.com/spf13/cobra"
)

// newListCommand creates a new list command and its subcommands.
func newListCommand() *listCommand {
	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 := flagsToCfg(cd, nil)
		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
		}

		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 shouldInclude(p) {
				record := createRecord(h.Conf.BaseConfig().WorkingDir, p)
				if err := writer.Write(record); err != nil {
					return err
				}
				if err != nil {
					return err
				}
			}
		}

		return nil
	}

	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 {
					shouldInclude := func(p page.Page) bool {
						if !p.Draft() || p.File() == nil {
							return false
						}
						return true
					}
					return list(cd, r, shouldInclude,
						"buildDrafts", true,
						"buildFuture", true,
						"buildExpired", true,
					)
				},
				withc: func(cmd *cobra.Command, r *rootCommand) {
					cmd.ValidArgsFunction = cobra.NoFileCompletions
				},
			},
			&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 {
					shouldInclude := func(p page.Page) bool {
						if !resource.IsFuture(p) || p.File() == nil {
							return false
						}
						return true
					}
					return list(cd, r, shouldInclude,
						"buildFuture", true,
						"buildDrafts", true,
					)
				},
				withc: func(cmd *cobra.Command, r *rootCommand) {
					cmd.ValidArgsFunction = cobra.NoFileCompletions
				},
			},
			&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 {
					shouldInclude := func(p page.Page) bool {
						if !resource.IsExpired(p) || p.File() == nil {
							return false
						}
						return true
					}
					return list(cd, r, shouldInclude,
						"buildExpired", true,
						"buildDrafts", true,
					)
				},
				withc: func(cmd *cobra.Command, r *rootCommand) {
					cmd.ValidArgsFunction = cobra.NoFileCompletions
				},
			},
			&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 {
					shouldInclude := func(p page.Page) bool {
						return p.File() != nil
					}
					return list(cd, r, shouldInclude, "buildDrafts", true, "buildFuture", true, "buildExpired", true)
				},
				withc: func(cmd *cobra.Command, r *rootCommand) {
					cmd.ValidArgsFunction = cobra.NoFileCompletions
				},
			},
		},
	}
}

type listCommand struct {
	commands []simplecobra.Commander
}

func (c *listCommand) Commands() []simplecobra.Commander {
	return c.commands
}

func (c *listCommand) Name() string {
	return "list"
}

func (c *listCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args []string) error {
	// Do nothing.
	return nil
}

func (c *listCommand) Init(cd *simplecobra.Commandeer) error {
	cmd := cd.CobraCommand
	cmd.Short = "Listing out various types of content"
	cmd.Long = `Listing out various types of content.

List requires a subcommand, e.g. hugo list drafts`

	cmd.RunE = nil
	return nil
}

func (c *listCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
	return nil
}