github.com/jason-dour/hugo@v0.63.3/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(newNewSiteCmd().getCommand()) 59 cmd.AddCommand(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, false, &n.hugoBuilderCommon, n, cfgInit) 75 76 if err != nil { 77 return err 78 } 79 80 if len(args) < 1 { 81 return newUserError("path needs to be provided") 82 } 83 84 createPath := args[0] 85 86 var kind string 87 88 createPath, kind = newContentPathSection(c.hugo(), createPath) 89 90 if n.contentType != "" { 91 kind = n.contentType 92 } 93 94 return create.NewContent(c.hugo(), kind, createPath) 95 } 96 97 func mkdir(x ...string) { 98 p := filepath.Join(x...) 99 100 err := os.MkdirAll(p, 0777) // before umask 101 if err != nil { 102 jww.FATAL.Fatalln(err) 103 } 104 } 105 106 func touchFile(fs afero.Fs, x ...string) { 107 inpath := filepath.Join(x...) 108 mkdir(filepath.Dir(inpath)) 109 err := helpers.WriteToDisk(inpath, bytes.NewReader([]byte{}), fs) 110 if err != nil { 111 jww.FATAL.Fatalln(err) 112 } 113 } 114 115 func newContentPathSection(h *hugolib.HugoSites, path string) (string, string) { 116 // Forward slashes is used in all examples. Convert if needed. 117 // Issue #1133 118 createpath := filepath.FromSlash(path) 119 120 if h != nil { 121 for _, dir := range h.BaseFs.Content.Dirs { 122 createpath = strings.TrimPrefix(createpath, dir.Meta().Filename()) 123 } 124 } 125 126 var section string 127 // assume the first directory is the section (kind) 128 if strings.Contains(createpath[1:], helpers.FilePathSeparator) { 129 parts := strings.Split(strings.TrimPrefix(createpath, helpers.FilePathSeparator), helpers.FilePathSeparator) 130 if len(parts) > 0 { 131 section = parts[0] 132 } 133 134 } 135 136 return createpath, section 137 }