gitee.com/mirrors/Hugo-Go@v0.47.1/commands/list.go (about)

     1  // Copyright 2015 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  	"path/filepath"
    18  
    19  	"github.com/gohugoio/hugo/hugolib"
    20  	"github.com/spf13/cobra"
    21  	jww "github.com/spf13/jwalterweatherman"
    22  )
    23  
    24  var _ cmder = (*listCmd)(nil)
    25  
    26  type listCmd struct {
    27  	hugoBuilderCommon
    28  	*baseCmd
    29  }
    30  
    31  func newListCmd() *listCmd {
    32  	cc := &listCmd{}
    33  
    34  	cc.baseCmd = newBaseCmd(&cobra.Command{
    35  		Use:   "list",
    36  		Short: "Listing out various types of content",
    37  		Long: `Listing out various types of content.
    38  
    39  List requires a subcommand, e.g. ` + "`hugo list drafts`.",
    40  		RunE: nil,
    41  	})
    42  
    43  	cc.cmd.AddCommand(
    44  		&cobra.Command{
    45  			Use:   "drafts",
    46  			Short: "List all drafts",
    47  			Long:  `List all of the drafts in your content directory.`,
    48  			RunE: func(cmd *cobra.Command, args []string) error {
    49  				cfgInit := func(c *commandeer) error {
    50  					c.Set("buildDrafts", true)
    51  					return nil
    52  				}
    53  				c, err := initializeConfig(true, false, &cc.hugoBuilderCommon, cc, cfgInit)
    54  				if err != nil {
    55  					return err
    56  				}
    57  
    58  				sites, err := hugolib.NewHugoSites(*c.DepsCfg)
    59  
    60  				if err != nil {
    61  					return newSystemError("Error creating sites", err)
    62  				}
    63  
    64  				if err := sites.Build(hugolib.BuildCfg{SkipRender: true}); err != nil {
    65  					return newSystemError("Error Processing Source Content", err)
    66  				}
    67  
    68  				for _, p := range sites.Pages() {
    69  					if p.IsDraft() {
    70  						jww.FEEDBACK.Println(filepath.Join(p.File.Dir(), p.File.LogicalName()))
    71  					}
    72  
    73  				}
    74  
    75  				return nil
    76  
    77  			},
    78  		},
    79  		&cobra.Command{
    80  			Use:   "future",
    81  			Short: "List all posts dated in the future",
    82  			Long: `List all of the posts in your content directory which will be
    83  posted in the future.`,
    84  			RunE: func(cmd *cobra.Command, args []string) error {
    85  				cfgInit := func(c *commandeer) error {
    86  					c.Set("buildFuture", true)
    87  					return nil
    88  				}
    89  				c, err := initializeConfig(true, false, &cc.hugoBuilderCommon, cc, cfgInit)
    90  				if err != nil {
    91  					return err
    92  				}
    93  
    94  				sites, err := hugolib.NewHugoSites(*c.DepsCfg)
    95  
    96  				if err != nil {
    97  					return newSystemError("Error creating sites", err)
    98  				}
    99  
   100  				if err := sites.Build(hugolib.BuildCfg{SkipRender: true}); err != nil {
   101  					return newSystemError("Error Processing Source Content", err)
   102  				}
   103  
   104  				for _, p := range sites.Pages() {
   105  					if p.IsFuture() {
   106  						jww.FEEDBACK.Println(filepath.Join(p.File.Dir(), p.File.LogicalName()))
   107  					}
   108  
   109  				}
   110  
   111  				return nil
   112  
   113  			},
   114  		},
   115  		&cobra.Command{
   116  			Use:   "expired",
   117  			Short: "List all posts already expired",
   118  			Long: `List all of the posts in your content directory which has already
   119  expired.`,
   120  			RunE: func(cmd *cobra.Command, args []string) error {
   121  				cfgInit := func(c *commandeer) error {
   122  					c.Set("buildExpired", true)
   123  					return nil
   124  				}
   125  				c, err := initializeConfig(true, false, &cc.hugoBuilderCommon, cc, cfgInit)
   126  				if err != nil {
   127  					return err
   128  				}
   129  
   130  				sites, err := hugolib.NewHugoSites(*c.DepsCfg)
   131  
   132  				if err != nil {
   133  					return newSystemError("Error creating sites", err)
   134  				}
   135  
   136  				if err := sites.Build(hugolib.BuildCfg{SkipRender: true}); err != nil {
   137  					return newSystemError("Error Processing Source Content", err)
   138  				}
   139  
   140  				for _, p := range sites.Pages() {
   141  					if p.IsExpired() {
   142  						jww.FEEDBACK.Println(filepath.Join(p.File.Dir(), p.File.LogicalName()))
   143  					}
   144  
   145  				}
   146  
   147  				return nil
   148  
   149  			},
   150  		},
   151  	)
   152  
   153  	cc.cmd.PersistentFlags().StringVarP(&cc.source, "source", "s", "", "filesystem path to read files relative from")
   154  	cc.cmd.PersistentFlags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{})
   155  
   156  	return cc
   157  }