list.go (5528B)
1 // Copyright 2019 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 "encoding/csv"
18 "os"
19 "strconv"
20 "strings"
21 "time"
22
23 "github.com/gohugoio/hugo/hugolib"
24 "github.com/gohugoio/hugo/resources/resource"
25 "github.com/spf13/cobra"
26 jww "github.com/spf13/jwalterweatherman"
27 )
28
29 var _ cmder = (*listCmd)(nil)
30
31 type listCmd struct {
32 *baseBuilderCmd
33 }
34
35 func (lc *listCmd) buildSites(config map[string]any) (*hugolib.HugoSites, error) {
36 cfgInit := func(c *commandeer) error {
37 for key, value := range config {
38 c.Set(key, value)
39 }
40 return nil
41 }
42
43 c, err := initializeConfig(true, true, false, &lc.hugoBuilderCommon, lc, cfgInit)
44 if err != nil {
45 return nil, err
46 }
47
48 sites, err := hugolib.NewHugoSites(*c.DepsCfg)
49 if err != nil {
50 return nil, newSystemError("Error creating sites", err)
51 }
52
53 if err := sites.Build(hugolib.BuildCfg{SkipRender: true}); err != nil {
54 return nil, newSystemError("Error Processing Source Content", err)
55 }
56
57 return sites, nil
58 }
59
60 func (b *commandsBuilder) newListCmd() *listCmd {
61 cc := &listCmd{}
62
63 cmd := &cobra.Command{
64 Use: "list",
65 Short: "Listing out various types of content",
66 Long: `Listing out various types of content.
67
68 List requires a subcommand, e.g. ` + "`hugo list drafts`.",
69 RunE: nil,
70 }
71
72 cmd.AddCommand(
73 &cobra.Command{
74 Use: "drafts",
75 Short: "List all drafts",
76 Long: `List all of the drafts in your content directory.`,
77 RunE: func(cmd *cobra.Command, args []string) error {
78 sites, err := cc.buildSites(map[string]any{"buildDrafts": true})
79 if err != nil {
80 return newSystemError("Error building sites", err)
81 }
82
83 for _, p := range sites.Pages() {
84 if p.Draft() {
85 jww.FEEDBACK.Println(strings.TrimPrefix(p.File().Filename(), sites.WorkingDir+string(os.PathSeparator)))
86 }
87 }
88
89 return nil
90 },
91 },
92 &cobra.Command{
93 Use: "future",
94 Short: "List all posts dated in the future",
95 Long: `List all of the posts in your content directory which will be posted in the future.`,
96 RunE: func(cmd *cobra.Command, args []string) error {
97 sites, err := cc.buildSites(map[string]any{"buildFuture": true})
98 if err != nil {
99 return newSystemError("Error building sites", err)
100 }
101
102 if err != nil {
103 return newSystemError("Error building sites", err)
104 }
105
106 writer := csv.NewWriter(os.Stdout)
107 defer writer.Flush()
108
109 for _, p := range sites.Pages() {
110 if resource.IsFuture(p) {
111 err := writer.Write([]string{
112 strings.TrimPrefix(p.File().Filename(), sites.WorkingDir+string(os.PathSeparator)),
113 p.PublishDate().Format(time.RFC3339),
114 })
115 if err != nil {
116 return newSystemError("Error writing future posts to stdout", err)
117 }
118 }
119 }
120
121 return nil
122 },
123 },
124 &cobra.Command{
125 Use: "expired",
126 Short: "List all posts already expired",
127 Long: `List all of the posts in your content directory which has already expired.`,
128 RunE: func(cmd *cobra.Command, args []string) error {
129 sites, err := cc.buildSites(map[string]any{"buildExpired": true})
130 if err != nil {
131 return newSystemError("Error building sites", err)
132 }
133
134 if err != nil {
135 return newSystemError("Error building sites", err)
136 }
137
138 writer := csv.NewWriter(os.Stdout)
139 defer writer.Flush()
140
141 for _, p := range sites.Pages() {
142 if resource.IsExpired(p) {
143 err := writer.Write([]string{
144 strings.TrimPrefix(p.File().Filename(), sites.WorkingDir+string(os.PathSeparator)),
145 p.ExpiryDate().Format(time.RFC3339),
146 })
147 if err != nil {
148 return newSystemError("Error writing expired posts to stdout", err)
149 }
150 }
151 }
152
153 return nil
154 },
155 },
156 &cobra.Command{
157 Use: "all",
158 Short: "List all posts",
159 Long: `List all of the posts in your content directory, include drafts, future and expired pages.`,
160 RunE: func(cmd *cobra.Command, args []string) error {
161 sites, err := cc.buildSites(map[string]any{
162 "buildExpired": true,
163 "buildDrafts": true,
164 "buildFuture": true,
165 })
166 if err != nil {
167 return newSystemError("Error building sites", err)
168 }
169
170 writer := csv.NewWriter(os.Stdout)
171 defer writer.Flush()
172
173 writer.Write([]string{
174 "path",
175 "slug",
176 "title",
177 "date",
178 "expiryDate",
179 "publishDate",
180 "draft",
181 "permalink",
182 })
183 for _, p := range sites.Pages() {
184 if !p.IsPage() {
185 continue
186 }
187 err := writer.Write([]string{
188 strings.TrimPrefix(p.File().Filename(), sites.WorkingDir+string(os.PathSeparator)),
189 p.Slug(),
190 p.Title(),
191 p.Date().Format(time.RFC3339),
192 p.ExpiryDate().Format(time.RFC3339),
193 p.PublishDate().Format(time.RFC3339),
194 strconv.FormatBool(p.Draft()),
195 p.Permalink(),
196 })
197 if err != nil {
198 return newSystemError("Error writing posts to stdout", err)
199 }
200 }
201
202 return nil
203 },
204 },
205 )
206
207 cc.baseBuilderCmd = b.newBuilderBasicCmd(cmd)
208
209 return cc
210 }