Debian Codegraph @

commands/list.go

raw
1 // Copyright 2024 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package commands
15
16 import (
17 "context"
18 "encoding/csv"
19 "os"
20 "path/filepath"
21 "strconv"
22 "strings"
23 "time"
24
25 "github.com/bep/simplecobra"
26 "github.com/gohugoio/hugo/hugolib"
27 "github.com/gohugoio/hugo/resources/page"
28 "github.com/gohugoio/hugo/resources/resource"
29 "github.com/spf13/cobra"
30 )
31
32 // newListCommand creates a new list command and its subcommands.
33 func newListCommand() *listCommand {
34 createRecord := func(workingDir string, p page.Page) []string {
35 return []string{
36 filepath.ToSlash(strings.TrimPrefix(p.File().Filename(), workingDir+string(os.PathSeparator))),
37 p.Slug(),
38 p.Title(),
39 p.Date().Format(time.RFC3339),
40 p.ExpiryDate().Format(time.RFC3339),
41 p.PublishDate().Format(time.RFC3339),
42 strconv.FormatBool(p.Draft()),
43 p.Permalink(),
44 p.Kind(),
45 p.Section(),
46 }
47 }
48
49 list := func(cd *simplecobra.Commandeer, r *rootCommand, shouldInclude func(page.Page) bool, opts ...any) error {
50 bcfg := hugolib.BuildCfg{SkipRender: true}
51 cfg := flagsToCfg(cd, nil)
52 for i := 0; i < len(opts); i += 2 {
53 cfg.Set(opts[i].(string), opts[i+1])
54 }
55 h, err := r.Build(cd, bcfg, cfg)
56 if err != nil {
57 return err
58 }
59
60 writer := csv.NewWriter(r.StdOut)
61 defer writer.Flush()
62
63 writer.Write([]string{
64 "path",
65 "slug",
66 "title",
67 "date",
68 "expiryDate",
69 "publishDate",
70 "draft",
71 "permalink",
72 "kind",
73 "section",
74 })
75
76 for _, p := range h.Pages() {
77 if shouldInclude(p) {
78 record := createRecord(h.Conf.BaseConfig().WorkingDir, p)
79 if err := writer.Write(record); err != nil {
80 return err
81 }
82 }
83 }
84
85 return nil
86 }
87
88 return &listCommand{
89 commands: []simplecobra.Commander{
90 &simpleCommand{
91 name: "drafts",
92 short: "List draft content",
93 long: `List draft content.`,
94 run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
95 shouldInclude := func(p page.Page) bool {
96 if !p.Draft() || p.File() == nil {
97 return false
98 }
99 return true
100 }
101 return list(cd, r, shouldInclude,
102 "buildDrafts", true,
103 "buildFuture", true,
104 "buildExpired", true,
105 )
106 },
107 withc: func(cmd *cobra.Command, r *rootCommand) {
108 cmd.ValidArgsFunction = cobra.NoFileCompletions
109 },
110 },
111 &simpleCommand{
112 name: "future",
113 short: "List future content",
114 long: `List content with a future publication date.`,
115 run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
116 shouldInclude := func(p page.Page) bool {
117 if !resource.IsFuture(p) || p.File() == nil {
118 return false
119 }
120 return true
121 }
122 return list(cd, r, shouldInclude,
123 "buildFuture", true,
124 "buildDrafts", true,
125 )
126 },
127 withc: func(cmd *cobra.Command, r *rootCommand) {
128 cmd.ValidArgsFunction = cobra.NoFileCompletions
129 },
130 },
131 &simpleCommand{
132 name: "expired",
133 short: "List expired content",
134 long: `List content with a past expiration date.`,
135 run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
136 shouldInclude := func(p page.Page) bool {
137 if !resource.IsExpired(p) || p.File() == nil {
138 return false
139 }
140 return true
141 }
142 return list(cd, r, shouldInclude,
143 "buildExpired", true,
144 "buildDrafts", true,
145 )
146 },
147 withc: func(cmd *cobra.Command, r *rootCommand) {
148 cmd.ValidArgsFunction = cobra.NoFileCompletions
149 },
150 },
151 &simpleCommand{
152 name: "all",
153 short: "List all content",
154 long: `List all content including draft, future, and expired.`,
155 run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
156 shouldInclude := func(p page.Page) bool {
157 return p.File() != nil
158 }
159 return list(cd, r, shouldInclude, "buildDrafts", true, "buildFuture", true, "buildExpired", true)
160 },
161 withc: func(cmd *cobra.Command, r *rootCommand) {
162 cmd.ValidArgsFunction = cobra.NoFileCompletions
163 },
164 },
165 &simpleCommand{
166 name: "published",
167 short: "List published content",
168 long: `List content that is not draft, future, or expired.`,
169 run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
170 shouldInclude := func(p page.Page) bool {
171 return !p.Draft() && !resource.IsFuture(p) && !resource.IsExpired(p) && p.File() != nil
172 }
173 return list(cd, r, shouldInclude)
174 },
175 withc: func(cmd *cobra.Command, r *rootCommand) {
176 cmd.ValidArgsFunction = cobra.NoFileCompletions
177 },
178 },
179 },
180 }
181 }
182
183 type listCommand struct {
184 commands []simplecobra.Commander
185 }
186
187 func (c *listCommand) Commands() []simplecobra.Commander {
188 return c.commands
189 }
190
191 func (c *listCommand) Name() string {
192 return "list"
193 }
194
195 func (c *listCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args []string) error {
196 // Do nothing.
197 return nil
198 }
199
200 func (c *listCommand) Init(cd *simplecobra.Commandeer) error {
201 cmd := cd.CobraCommand
202 cmd.Short = "List content"
203 cmd.Long = `List content.
204
205 List requires a subcommand, e.g. hugo list drafts`
206
207 cmd.RunE = nil
208 return nil
209 }
210
211 func (c *listCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
212 return nil
213 }