github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/commands/list.go (about)

     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]interface{}) (*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]interface{}{"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]interface{}{"buildFuture": true})
    98  				if err != nil {
    99  					return newSystemError("Error building sites", err)
   100  				}
   101  
   102  				writer := csv.NewWriter(os.Stdout)
   103  				defer writer.Flush()
   104  
   105  				for _, p := range sites.Pages() {
   106  					if resource.IsFuture(p) {
   107  						err := writer.Write([]string{
   108  							strings.TrimPrefix(p.File().Filename(), sites.WorkingDir+string(os.PathSeparator)),
   109  							p.PublishDate().Format(time.RFC3339),
   110  						})
   111  						if err != nil {
   112  							return newSystemError("Error writing future posts to stdout", err)
   113  						}
   114  					}
   115  				}
   116  
   117  				return nil
   118  			},
   119  		},
   120  		&cobra.Command{
   121  			Use:   "expired",
   122  			Short: "List all posts already expired",
   123  			Long:  `List all of the posts in your content directory which has already expired.`,
   124  			RunE: func(cmd *cobra.Command, args []string) error {
   125  				sites, err := cc.buildSites(map[string]interface{}{"buildExpired": true})
   126  				if err != nil {
   127  					return newSystemError("Error building sites", err)
   128  				}
   129  
   130  				writer := csv.NewWriter(os.Stdout)
   131  				defer writer.Flush()
   132  
   133  				for _, p := range sites.Pages() {
   134  					if resource.IsExpired(p) {
   135  						err := writer.Write([]string{
   136  							strings.TrimPrefix(p.File().Filename(), sites.WorkingDir+string(os.PathSeparator)),
   137  							p.ExpiryDate().Format(time.RFC3339),
   138  						})
   139  						if err != nil {
   140  							return newSystemError("Error writing expired posts to stdout", err)
   141  						}
   142  					}
   143  				}
   144  
   145  				return nil
   146  			},
   147  		},
   148  		&cobra.Command{
   149  			Use:   "all",
   150  			Short: "List all posts",
   151  			Long:  `List all of the posts in your content directory, include drafts, future and expired pages.`,
   152  			RunE: func(cmd *cobra.Command, args []string) error {
   153  				sites, err := cc.buildSites(map[string]interface{}{
   154  					"buildExpired": true,
   155  					"buildDrafts":  true,
   156  					"buildFuture":  true,
   157  				})
   158  				if err != nil {
   159  					return newSystemError("Error building sites", err)
   160  				}
   161  
   162  				writer := csv.NewWriter(os.Stdout)
   163  				defer writer.Flush()
   164  
   165  				writer.Write([]string{
   166  					"path",
   167  					"slug",
   168  					"title",
   169  					"date",
   170  					"expiryDate",
   171  					"publishDate",
   172  					"draft",
   173  					"permalink",
   174  				})
   175  				for _, p := range sites.Pages() {
   176  					if !p.IsPage() {
   177  						continue
   178  					}
   179  					err := writer.Write([]string{
   180  						strings.TrimPrefix(p.File().Filename(), sites.WorkingDir+string(os.PathSeparator)),
   181  						p.Slug(),
   182  						p.Title(),
   183  						p.Date().Format(time.RFC3339),
   184  						p.ExpiryDate().Format(time.RFC3339),
   185  						p.PublishDate().Format(time.RFC3339),
   186  						strconv.FormatBool(p.Draft()),
   187  						p.Permalink(),
   188  					})
   189  					if err != nil {
   190  						return newSystemError("Error writing posts to stdout", err)
   191  					}
   192  				}
   193  
   194  				return nil
   195  			},
   196  		},
   197  	)
   198  
   199  	cc.baseBuilderCmd = b.newBuilderBasicCmd(cmd)
   200  
   201  	return cc
   202  }