github.com/gohugoio/hugo@v0.88.1/commands/new.go (about) 1 // Copyright 2018 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 "bytes" 18 "os" 19 "path/filepath" 20 "strings" 21 22 "github.com/gohugoio/hugo/create" 23 "github.com/gohugoio/hugo/helpers" 24 "github.com/gohugoio/hugo/hugolib" 25 "github.com/spf13/afero" 26 "github.com/spf13/cobra" 27 jww "github.com/spf13/jwalterweatherman" 28 ) 29 30 var _ cmder = (*newCmd)(nil) 31 32 type newCmd struct { 33 contentEditor string 34 contentType string 35 36 *baseBuilderCmd 37 } 38 39 func (b *commandsBuilder) newNewCmd() *newCmd { 40 cmd := &cobra.Command{ 41 Use: "new [path]", 42 Short: "Create new content for your site", 43 Long: `Create a new content file and automatically set the date and title. 44 It will guess which kind of file to create based on the path provided. 45 46 You can also specify the kind with ` + "`-k KIND`" + `. 47 48 If archetypes are provided in your theme or site, they will be used. 49 50 Ensure you run this within the root directory of your site.`, 51 } 52 53 cc := &newCmd{baseBuilderCmd: b.newBuilderCmd(cmd)} 54 55 cmd.Flags().StringVarP(&cc.contentType, "kind", "k", "", "content type to create") 56 cmd.Flags().StringVar(&cc.contentEditor, "editor", "", "edit new content with this editor, if provided") 57 58 cmd.AddCommand(b.newNewSiteCmd().getCommand()) 59 cmd.AddCommand(b.newNewThemeCmd().getCommand()) 60 61 cmd.RunE = cc.newContent 62 63 return cc 64 } 65 66 func (n *newCmd) newContent(cmd *cobra.Command, args []string) error { 67 cfgInit := func(c *commandeer) error { 68 if cmd.Flags().Changed("editor") { 69 c.Set("newContentEditor", n.contentEditor) 70 } 71 return nil 72 } 73 74 c, err := initializeConfig(true, true, false, &n.hugoBuilderCommon, n, cfgInit) 75 if err != nil { 76 return err 77 } 78 79 if len(args) < 1 { 80 return newUserError("path needs to be provided") 81 } 82 83 createPath := args[0] 84 85 var kind string 86 87 createPath, kind = newContentPathSection(c.hugo(), createPath) 88 89 if n.contentType != "" { 90 kind = n.contentType 91 } 92 93 return create.NewContent(c.hugo(), kind, createPath) 94 } 95 96 func mkdir(x ...string) { 97 p := filepath.Join(x...) 98 99 err := os.MkdirAll(p, 0777) // before umask 100 if err != nil { 101 jww.FATAL.Fatalln(err) 102 } 103 } 104 105 func touchFile(fs afero.Fs, x ...string) { 106 inpath := filepath.Join(x...) 107 mkdir(filepath.Dir(inpath)) 108 err := helpers.WriteToDisk(inpath, bytes.NewReader([]byte{}), fs) 109 if err != nil { 110 jww.FATAL.Fatalln(err) 111 } 112 } 113 114 func newContentPathSection(h *hugolib.HugoSites, path string) (string, string) { 115 // Forward slashes is used in all examples. Convert if needed. 116 // Issue #1133 117 createpath := filepath.FromSlash(path) 118 119 if h != nil { 120 for _, dir := range h.BaseFs.Content.Dirs { 121 createpath = strings.TrimPrefix(createpath, dir.Meta().Filename) 122 } 123 } 124 125 var section string 126 // assume the first directory is the section (kind) 127 if strings.Contains(createpath[1:], helpers.FilePathSeparator) { 128 parts := strings.Split(strings.TrimPrefix(createpath, helpers.FilePathSeparator), helpers.FilePathSeparator) 129 if len(parts) > 0 { 130 section = parts[0] 131 } 132 133 } 134 135 return createpath, section 136 }