new.go (3370B)
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 return create.NewContent(c.hugo(), n.contentType, args[0]) 84 } 85 86 func mkdir(x ...string) { 87 p := filepath.Join(x...) 88 89 err := os.MkdirAll(p, 0777) // before umask 90 if err != nil { 91 jww.FATAL.Fatalln(err) 92 } 93 } 94 95 func touchFile(fs afero.Fs, x ...string) { 96 inpath := filepath.Join(x...) 97 mkdir(filepath.Dir(inpath)) 98 err := helpers.WriteToDisk(inpath, bytes.NewReader([]byte{}), fs) 99 if err != nil { 100 jww.FATAL.Fatalln(err) 101 } 102 } 103 104 func newContentPathSection(h *hugolib.HugoSites, path string) (string, string) { 105 // Forward slashes is used in all examples. Convert if needed. 106 // Issue #1133 107 createpath := filepath.FromSlash(path) 108 109 if h != nil { 110 for _, dir := range h.BaseFs.Content.Dirs { 111 createpath = strings.TrimPrefix(createpath, dir.Meta().Filename) 112 } 113 } 114 115 var section string 116 // assume the first directory is the section (kind) 117 if strings.Contains(createpath[1:], helpers.FilePathSeparator) { 118 parts := strings.Split(strings.TrimPrefix(createpath, helpers.FilePathSeparator), helpers.FilePathSeparator) 119 if len(parts) > 0 { 120 section = parts[0] 121 } 122 123 } 124 125 return createpath, section 126 }